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

C语言链表问题

02月12日 编辑 39baobao.com

我给你改,留个联系方式好给你。现在公司活比较忙。

给你改了一下,感觉你的链表好象设计的时候就有问题

你看看我给你改的吧.

#include#include #define NULL 0 #define LEN sizeof(struct stu)//测算结构体所占的字节数 struct stu { int data; struct stu *next; }; struct stu *creat_and_init_link(int n)//创建链表并且初始化 { struct stu *head,*p,*q; int i; head=p=q=(struct stu*)malloc(LEN); for(i=0;i { p->next=q; p=q; p->data=i+1; q=(struct stu*)malloc(LEN); } p->next=NULL;//尾结点指向空。 return head; } void print(struct stu *head)//输出链表里面的内容 { struct stu *p; p=head; if(head!=NULL) { while(p!=NULL) { printf("the value of the list is %d\n",p->data); p=p->next; } } } int main(int argc, char *argv[]) { int number; struct stu *head; printf("please enter the length of the list\n"); scanf("%d",&number); head=creat_and_init_link(number); print(head); return 0; }

推荐阅读
图文推荐