数据结构基础单向链表

单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表。

相关代码讲解如下:

#includestdio.h#includemalloc.h//声明structLNode类型structLNode{intdata;structLNode*next;};/*自定义函数create(),实现创建一个链表,将此函数定义为指针类型,使其返回值为指针值,返回值指向一个structLNode类型数据,实际上是返回链表的头指针*/structLNode*create(intn){inti;structLNode*head,*p1,*p2;//头指针,两个移动控制指针inta;head=NULL;//空头指针printf("输入整数:\n");for(i=n;i0;--i){p1=(structLNode*)malloc(sizeof(structLNode));//给链表分配空间scanf("%d",a);//输入数据p1-data=a;//数据域赋值if(head==NULL)//指定头结点{head=p1;p2=p1;}else{p2-next=p1;//指定后继指针p2=p1;//这两个其实是如影随形,前进一个,后一个立刻跟上了}}p2-next=NULL;//链表结束returnhead;}/*在main()函数中调用自定义函数create(),实现创建链表,并将链表中数据输出*/intmain(){intn;structLNode*q;printf("输入你想创建的节点个数:");scanf("%d",n);q=create(n);printf("结果是:\n");while(q){printf("%d",q-data);q=q-next;//输出链表};printf("\n");}                







































白癜风的民间偏方
甲氧沙林价格



转载请注明:http://www.92nongye.com/zyjs/zyjs/204621741.html