#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
typedef struct node *linklist;
void *sort(linklist head,int x)
{
linklist p,rear,q;
p=head;
rear=(linklist)malloc(sizeof(linklist));
rear->data=x;
rear->next=NULL;
while(head->next!=NULL)
{
q=head->next;
if(q->data>=x){
rear->next=head->next;
head->next=rear;
free(rear);
return p;
}
head=head->next;
}
head->next=rear;
rear->next=NULL;
free(rear);
return p;
}
void print(linklist head)
{
int flag=1;
head=head->next;
while(head!=NULL)
{
if(flag==1){
printf("%d",head->data);
flag=0;
}
else{
printf("->%d",head->data);
}
head=head->next;
}
free(head);
}
int main()
{
linklist head1,head2;
head1=(linklist)malloc(sizeof(linklist));
head1->next = NULL;
head2=(linklist)malloc(sizeof(linklist));
head2->next = NULL;
int x;
char ch;
do
{
scanf("%d",&x);
if(x>0){
head1=sort(head1,x);
}
else{
head2=sort(head2,x);
}
}while((ch=getchar())!= '\n');
print(head1);
printf("\n");
print(head2);
printf("\n");
return 0;
}
你没有考虑到假如插入的数据小于第一个节点的情况。还有程序要求最后摧毁两个链表你也没有做到