1,循环单链表的建立:其和线性链表的操作基本一致,区别在于循环条件不是p->next是否存在,而是p->next是否等于头结点。
代码如下:
#include "stdafx.h"
#include<stdio.h>
#include <malloc.h>
#define SIZE 100
#define INCREMENT_SIZE 10
typedef struct LNode
{
int data;
LNode *next;
}LNode,*LinkList;
//creat a LinkList
bool creatLinklist(LinkList&L,int n)
{
LinkList p,q,t,s;
L=(LNode*)malloc(n*sizeof(LNode));
if(!L)
return false;
q=L;
for(int i=1;i<=n;i++)
{
p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
L->next=p;
L=p;
}
L=q;
p->next=L; //最后一个结点的指针域指向头结点。
return true;
}
void main()
{
LinkList Llist,p,t;
int k;
int len;
int elemet;
int position;
printf("input the number of LoopLinkList to be created:");
scanf("%d",&k);
creatLinklist(Llist,k);
printf("\n");
printf("output the new LinkList:\n");
p=Llist->next;
while(p!=Llist) //判断条件变成了是否等于头结点
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
free(Llist);
}
运行结果如下: