一道单链表的题,求高手帮我解决.
内容:在计算机上先输入一串正整数的序列。请编写一个程序,首先用链接表存储该序列。然后执行删除操作,即先从链表中找出最小的结点,删除它。然后再在剩余的链表中,找出最小的结点,再删除之。直至表空为止。我编写的是:
#include "stdio.h"
#include "malloc.h"
# define LEN sizeof(struct number)
struct number
{long num;
struct number *next;};
int n,j;
struct number *creat(void)
{struct number *head;
struct number *p1,*p2;
n=0;
p1=p2=(struct number*)malloc(LEN);
printf("Please input positive numbers\n");
scanf("%ld",&p1->num);
head=NULL;
while(p1->num!=0)
{ n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct number*)malloc(LEN);
scanf("%ld",&p1->num); }
p2->next=NULL;
j=n;
return(head);}
void print(struct number *head)
{struct number *p;
printf("\nNow ,there are %d numbers\n ", n);
p=head;
if(head!=NULL)
do {printf("%ld\t",p->num);
p=p->next;}
while(p!=NULL);}
struct number *del(struct number *head,long min)
{struct number *p1,*p2;
if(head==NULL)
printf("\nNo number\n");
p1=head;
min=p1->num;
while(p1->next!=NULL)
{p2=p1;
p1=p2->next;
if(min>=p1->num)
min=p1->num;}
if(p1==head) head=p1->next;
else p2->next=p1->next;
free(p1);
printf("delete:%ld\n",min);
n=n-1;
return(head);}
main()
{struct number *head;
int i;
long min;
head=creat();
print(head);
for(i=1;i<=j;i++)
{ del(head,min);
print(head);}
getch();
}
在运行出了的结果中不是我想要的结果,有那位高手看到了的话,帮我看看