单链表问题 c高手请进
李万鹏 2009-10-05 10:55:00 编写一个程序用链表实现:将一个升序整数数列的重复数据去掉
编好后,结果不对啊,求助c高手
#include<stdio.h>
#include<stdlib.h>
struct link
{
int number;
struct link *next;
};
struct link* creat()
{
struct link *p1,*p2;
struct link *head;
int n=0;
p1=p2=(struct link*)malloc(sizeof(struct link));
scanf("%d",&p2->number);
head=NULL;
while(p2->number!=0)
{
n++;
if(n==1)
head=p1;
else
{
p1->next=p2;
p1=p2;
}
p2=(struct link *)malloc(sizeof(struct link));
scanf("%d",&p2->number);
}
p1->next=NULL;
return head;
}
/struct link *del(struct link *head)
{
struct link *p1,*p2;
if(head==NULL)
printf("link is empty!");
p1=p2=head;
if(p1->next==NULL)
printf("needn't sort!");
else
{
while(p1->next!=NULL)
{
p1->next=p2;
if(p1->number==p2->number)
{
struct link *p;
p1->next=p2->next;
p=p2;
free(p);
p1=p1->next;
}
else
p1=p2;
}
}
return head;
}
void print(struct link *head)
{
struct link *p1;
if(head==NULL)
printf("link is empty!");
else
{
head=p1;
while(p1!=NULL)
{
printf("%d",p1->number);
p1=p1->next;
}
}
}
int main()
{
struct link *head;
head=creat();
head=del(head)
print(head);
system("pause");
return 0;
}