有谁能帮我看看这个两个链表哪里不一样。一个可以运行另一个不可以。。
//---------------第一个-----------------------#include "stdio.h"
struct ab
{
int num;
int age;
struct ab *next;
};
//----------------------------new---------------------------------------
struct ab *kk(int n)
{
struct ab *head,*pf,*pb;
int t;
for(t=0;t<n;t++)
{
pb=(struct ab *)malloc(sizeof(struct ab));
printf("input two number:\n");
scanf("%d%d",&pb->num,&pb->age);
if(t==0)
pf=head=pb;
else
pf->next=pb;
pb->next=0;
pf=pb;
}
return (head);
}
//------------------------------delete-------------------------------------
struct ab *del(struct ab *k,int d)
{
struct ab *pf,*pk;
if(k==0)
{
printf("empty list!");
goto end;
}
pk=k;
while(pk->num!=d&&pk->next!=0)
{
pf=pk;
pk=pk->next;
}
if(pk->num==d)
{
if(pk==k)
k=pk->next;
else
pf->next=pk->next;
}
else
free(pk);
printf("the number not been found!");
end:
return(k);
}
//---------------------------printf------------------------------------------
void print(struct ab *b)
{
printf("Number\t\tAge\n");
while(b!=0)
{
printf("%d\t\t%d\n",b->num,b->age);
b=b->next;
}
}
//----------------------------main()----------------------------------------------
void main()
{
int n,b;
int c,d;
printf("input something:\n");
scanf("%d",&n);
b=kk(n);
print(b);
printf("input the number you want to delete:\n);
scanf("%d",&d);
c=del(b,d);
print(b);
}
//---------------------第二个-------------------------------------------------
#define NULL 0
#define TYPE struct stu
#define LEN sizeof(struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE * creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE *)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
TYPE * delete(TYPE * head,int num)
{
TYPE *pf,*pb;
if(head==NULL)
{
printf("\nempty list!\n");
goto end;
}
pb=head;
while (pb->num!=num && pb->next!=NULL)
{
pf=pb;
pb=pb->next;
}
if(pb->num==num)
{
if(pb==head)
head=pb->next;
else pf->next=pb->next;
printf("The node is deleted\n"); }
else
free(pb);
printf("The node not been found!\n");
end:
return head;
}
//------------------------------------------------
void print(TYPE * head)
{
printf("Number\t\tAge\n");
while(head!=NULL)
{
printf("%d\t\t%d\n",head->num,head->age);
head=head->next;
}
}
//--------------------------------------------------------
main()
{
TYPE * head,*pnum;
int n,num;
printf("input number of node: ");
scanf("%d",&n);
head=creat(n);
print(head);
rintf("Input the deleted number: ");
scanf("%d",&num);
head=delete(head,num);
print(head);
}
//第一个不能运行,第二个就可以运行。。。高手们来帮帮我吧。
[[it] 本帖最后由 355023722 于 2008-5-28 22:14 编辑 [/it]]
[[it] 本帖最后由 355023722 于 2008-5-28 23:25 编辑 [/it]]