生活随笔
收集整理的這篇文章主要介紹了
单向循环链表C语言实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://blog.csdn.net/morixinguan/article/details/51771633
我們都知道,單向鏈表最后指向為NULL,也就是為空,那單向循環鏈表就是不指向為NULL了,指向頭節點,所以下面這個程序運行結果就是,你將會看到遍歷鏈表的時候就是一個死循環,因為它不指向為NULL,也是周而復始的執行。串成了一個環型。
? ? ?
[cpp]?view plaincopy print?
#include?<stdio.h>?? #include?<stdlib.h>?? typedef?struct?node?? {?? ????char?name[20];?? ????struct?node?*link;?? }student;?? ?? student?*?creat(int?n)?????? {?? ?????? ????student?*p,*h,*s;??????? ????int?i;?? ????if((h=(student?*)malloc(sizeof(student)))==NULL)??? ????{?? ????????printf("不能分配內存空間!");?? ????????exit(0);?? ????}?? ????h->name[0]='\0';??????? ????h->link=NULL;?????????? ????p=h;?????????????????? ????for(i=0;i<n;i++)?? ????{?? ????????if((s=?(student?*)?malloc(sizeof(student)))==NULL)??? ????????{?? ????????????printf("不能分配內存空間!");?? ????????????exit(0);?? ????????}?? ????????p->link=s;??? ????????printf("請輸入第%d個人的姓名",i+1);?? ?????????? ????????scanf("%s",s->name);?? ????????s->link=NULL;?? ????????p=s;?? ????}?? ?????? ????p->link=h;??? ????return(h);?? }?? ?? int?main(void)?? {?? ????int?number;??? ????student?*head;??? ????student?*p;?? ????printf("請輸入相應的人數:\n");?? ????scanf("%d",&number);?? ????head=creat(number);??? ????p=head;?? ????while(p->link)?? ????{?? ????????printf("%s\n",p->name);?? ????????p=p->link;?? ????}?? ?? ????return?0?;?? }??
運行結果:
總結
以上是生活随笔為你收集整理的单向循环链表C语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。