http://blog.csdn.net/morixinguan/article/details/51771633
我们都知道,单向链表最后指向为NULL,也就是为空,那单向循环链表就是不指向为NULL了,指向头节点,所以下面这个程序运行结果就是,你将会看到遍历链表的时候就是一个死循环,因为它不指向为NULL,也是周而复始的执行。串成了一个环型。
- #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 ;
- }
运行结果:
![](https://img-blog.csdn.net/20160627220240790?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)