三九宝宝网宝宝教育教学论文

简单的C语言链表问题

02月12日 编辑 39baobao.com

#include

#include

#include

#define N 2

struct node

{

char name[20];

char phone[10];

struct node *link;

};

struct node * creat(int n)

{

struct node *p,*h,*s;

int i;

if((h=(struct node *)malloc(sizeof(struct node)))==NULL)

{

printf("Cannot assignment memory space!");

exit(0);

}

h->name[0]='\0';

h->phone[0]='\0';

h->link=NULL;

p=h;

for (i=0;i

{

if ((s=(struct node *)malloc(sizeof(struct node)))==NULL)

{

printf("Cannot assignment memory space!");

exit(0);

}

p->link=s;

s->name[0]='\0';

s->phone[0]='\0';

printf("please input [%d] people's name and phone \n",i+1);

scanf("%s,%s",s->name,s->phone);

s->link=NULL;

p=s;

}

return(h);

}

void display(struct node *s)

{

while(s->link!=NULL)

{

printf("%s %s\t",s->name,s->phone);

s=s->link;

}

printf("%s %s\t",s->name,s->phone);

}

void main()

{

struct node *head,*p1;

head=creat(N);

display(head);

}

推荐阅读
图文推荐