循环链表
之前讲的单链表只能索引后继结点,不能索引前驱结点,这样的话若不从头结点出发的话,就无法访问到全部结点。
如何解决单链表出现这种缺点呢,我们将单链表中终端结点的指针由空指针改为指向头结点,这样就使整个单链表形成一个环,这种头尾相接的单链表为单循环链表,简称为循环链表。循环链表不一定有头结点。
循环链表的存储结构的定义如下。
typedef struct CLinkList
{
int data;
struct CLinkList* next;
}node;
<span style="font-size:18px;">typedef struct CLinkList
{
int data;
struct CLinkList* next;
}node;
void CLinkList_init(node **pNode)//参数表示指向链表的第一个结点
{
int item = 5;
node *temp;
node *target;
printf("输入结点的值,输入0完成初始化\n");
while(1)
{
scanf("%d",&item);
fflush(stdin);
if(item==0)return;
if(*pNode==NULL)//循环链表只有一个结点
{
*pNode = (node*)malloc(sizeof(struct CLinkList));
if(!(*pNode)) exit(0);
(*pNode)->data = item;
(*pNode)->next = *pNode;
}
else
{
for(target = *pNode;target->next!=(*pNode);target = target->next);//执行完这个循环,表示target指向的是终端结点
temp = (node*)malloc(sizeof(struct CLinkList));//生成一个新结点
if(!temp) exit(0);
temp->data = item;
temp->next = *pNode;
target->next = temp;
}
}
cout<<*pNode<<endl;
}</span>
在循环链表的第i个位置插入新的结点,代码如下:void CLinkList_insert(node **pNode,int i)//参数分别为第一个结点 插入的位置
{
int item;
node *temp;
node *target;
printf("输入要插入的值:");
scanf("%d",&item);
if(i==1)
{
temp = (node*)malloc(sizeof(struct CLinkList));//生成一个新结点
if(!temp)exit(0);
temp->data = item;
for(target = *pNode;target->next!=(*pNode);target = target->next);//执行完这个循环,表示target指向的是终端结点
target->next = temp;
temp->next = *pNode;
*pNode = temp;
}
else
{
target = *pNode;
for(int j = 1;j < i - 1;j++)target = target->next;//经过循环后 target指向插入位置的前一个结点
temp = (node*)malloc(sizeof(struct CLinkList));//生成一个新结点
if(!temp)exit(0);
temp->data = item;
temp->next = target->next;
target->next = temp;
}
}
删除循环链表中的第i个结点的代码如下void CLinkList_delete(node **pNode,int i)//删除循环链表的第i个结点
{
node *temp;
node *target;
if(i==1)
{
for(target = *pNode;target->next !=(*pNode);target = target->next);
temp = *pNode;
target->next = (temp)->next;
*pNode = (temp)->next;
free(temp);
}
else
{
target = *pNode;
for(int j = 1;j < i-1;j++)
{
target = target->next;//循环后target指向要删除位置的前一个位置
}
temp = target->next;
target->next = temp->next;
free(temp);
}
}
寻找循环链表中某一元素对应的位置int search(node *pNode ,int elem)
{
node *temp;
int j = 1;
for(temp = pNode;temp->data != elem && temp->next != pNode;++j)
{
temp = temp->next;
}
if(temp->next == pNode && temp->data != elem )return 0;
else return j;
}
基于循环链表总的代码如下:
typedef struct CLinkList
{
int data;
struct CLinkList* next;
}node;
void CLinkList_insert(node **pNode,int i)//参数分别为第一个结点 插入的位置
{
int item;
node *temp;
node *target;
printf("输入要插入的值:");
scanf("%d",&item);
if(i==1)
{
temp = (node*)malloc(sizeof(struct CLinkList));//生成一个新结点
if(!temp)exit(0);
temp->data = item;
for(target = *pNode;target->next!=(*pNode);target = target->next);//执行完这个循环,表示target指向的是终端结点
target->next = temp;
temp->next = *pNode;
*pNode = temp;
}
else
{
target = *pNode;
for(int j = 1;j < i - 1;j++)target = target->next;//经过循环后 target指向插入位置的前一个结点
temp = (node*)malloc(sizeof(struct CLinkList));//生成一个新结点
if(!temp)exit(0);
temp->data = item;
temp->next = target->next;
target->next = temp;
}
}
void CLinkList_delete(node **pNode,int i)//删除循环链表的第i个结点
{
node *temp;
node *target;
if(i==1)
{
for(target = *pNode;target->next !=(*pNode);target = target->next);
temp = *pNode;
target->next = (temp)->next;
*pNode = (temp)->next;
free(temp);
}
else
{
target = *pNode;
for(int j = 1;j < i-1;j++)
{
target = target->next;//循环后target指向要删除位置的前一个位置
}
temp = target->next;
target->next = temp->next;
free(temp);
}
}
void CLinkList_init(node **pNode)//参数表示指向链表的第一个结点
{
int item;
node *temp;
node *target;
printf("输入结点的值,输入0完成初始化\n");
while(1)
{
scanf("%d",&item);
//fflush(stdin);
if(item==0)return;
if(*pNode==NULL)//循环链表只有一个结点
{
*pNode = (node*)malloc(sizeof(struct CLinkList));
if(!(*pNode)) exit(0);
(*pNode)->data = item;
(*pNode)->next = *pNode;
}
else
{
for(target = *pNode;target->next!=(*pNode);target = target->next);//执行完这个循环,表示target指向的是终端结点
temp = (node*)malloc(sizeof(struct CLinkList));//生成一个新结点
if(!temp) exit(0);
temp->data = item;
temp->next = *pNode;
target->next = temp;
}
}
// cout<<*pNode<<endl;
}
int search(node *pNode ,int elem)
{
node *temp;
int j = 1;
for(temp = pNode;temp->data != elem && temp->next != pNode;++j)
{
temp = temp->next;
}
if(temp->next == pNode && temp->data != elem )return 0;
else return j;
}
void main()
{
node *pNode = NULL;
node *target;
CLinkList_init(&pNode);
target = pNode;
for(;target->next!=(pNode);target = target->next)
{
cout<<target->data<<endl;
}
cout<<target->data<<endl;
CLinkList_insert(&pNode,3) ;
printf("在位置3插入值后:\n") ;
target = pNode;
for(;target->next!=(pNode);target = target->next)
{
cout<<target->data<<endl;
}
cout<<target->data<<endl;
CLinkList_delete(&pNode,2) ;
printf("删除第2个结点后:\n") ;
target = pNode;
for(;target->next!=(pNode);target = target->next)
{
cout<<target->data<<endl;
}
cout<<target->data<<endl;
printf("元素5所在位置:%d\n",search(pNode,5)) ;
return ;
}