鱼C论坛

 找回密码
 立即注册
查看: 1161|回复: 0

[技术交流] 循环链表

[复制链接]
发表于 2019-7-29 14:57:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言:对于单链表,由于每个结点只存储了向后的指针,到了尾部标识就停止了向后链的操作。也就是说,按照这样的方式,只能索引后继结点不能索引前驱节点所以,不从头结点出发,就无法访问全部结点
解决方法:只需要将单链表中的终端结点的指针端由空指针改为指向头结点,使形成一个环
注意:这里并不是说循环链表就一定要有一个头结点
其实循环链表和单链表的主要差异就是循环判断的空链表的条件上,原来判断head->next是否为null,现在是head->next是否等于head
初始化循环链表:
  1. /*初始化循环链表*/
  2. void ds_init(node **pNode)
  3. {
  4.     int item;
  5.     node *temp;
  6.     node *target;

  7.     printf("输入结点的值,输入0完成初始化\n");

  8.         while(1)
  9.         {
  10.         scanf("%d", &item);
  11.         fflush(stdin);

  12.                 if(item == 0)
  13.             return;

  14.         if((*pNode) == NULL)
  15.                 { /*循环链表中只有一个结点*/
  16.                         *pNode = (node*)malloc(sizeof(struct CLinkList));
  17.                        
  18.                         if(!(*pNode))
  19.                                 exit(0);
  20.                        
  21.                         (*pNode)->data = item;
  22.                         (*pNode)->next = *pNode;
  23.                 }
  24.         else
  25.                 {
  26.             /*找到next指向第一个结点的结点*/
  27.             for(target = (*pNode); target->next != (*pNode); target = target->next)
  28.                                 ;

  29.             /*生成一个新的结点*/
  30.             temp = (node *)malloc(sizeof(struct CLinkList));

  31.                         if(!temp)
  32.                                 exit(0);

  33.                         temp->data = item;
  34.             temp->next = *pNode;
  35.             target->next = temp;
  36.         }
  37.     }
  38. }
复制代码

链表存储结构的定义
  1. /*链表存储结构的定义*/
  2. typedef struct CLinkList
  3. {
  4.     int data;
  5.     struct CLinkList *next;
  6. }node;

  7. /*插入结点*/
  8. /*参数:链表的第一个结点,插入的位置*/
  9. void ds_insert(node **pNode , int i)
  10. {
  11.     node *temp;
  12.     node *target;
  13.     node *p;
  14.     int item;
  15.     int j = 1;

  16.     printf("输入要插入结点的值:");
  17.     scanf("%d", &item);

  18.     if(i == 1)
  19.         { //新插入的结点作为第一个结点
  20.         temp = (node *)malloc(sizeof(struct CLinkList));

  21.                 if(!temp)
  22.             exit(0);

  23.                 temp->data = item;

  24.         /*寻找到最后一个结点*/
  25.         for(target = (*pNode); target->next != (*pNode); target = target->next)
  26.                         ;

  27.                 temp->next = (*pNode);
  28.         target->next = temp;
  29.         *pNode = temp;
  30.     }
  31.     else
  32.         {
  33.         target = *pNode;

  34.                 for( ; j < (i-1); ++j )
  35.                 {
  36.                         target = target->next;
  37.                 }  
  38.                
  39.                 // target指向第三个元素的
  40.                
  41.                 temp = (node *)malloc(sizeof(struct CLinkList));

  42.                 if(!temp)
  43.             exit(0);

  44.                 temp->data = item;
  45.                
  46.         p = target->next;
  47.         target->next = temp;
  48.         temp->next = p;
  49.     }
  50. }
复制代码

返回链表所在的位置:
  1. /*返回结点所在位置*/
  2. int ds_search(node *pNode, int elem)
  3. {
  4.     node *target;
  5.     int i = 1;

  6.     for(target = pNode; target->data != elem && target->next != pNode; ++i)
  7.         {
  8.                 target = target->next;
  9.         }
  10.        
  11.         if(target->next == pNode) /*表中不存在该元素*/
  12.         return 0;
  13.     else
  14.         return i;
  15. }
复制代码

删除结点:
  1. /*删除结点*/
  2. void ds_delete(node **pNode, int i)
  3. {
  4.     node *target;
  5.     node *temp;
  6.     int j = 1;

  7.     if(i == 1)
  8.         { //删除的是第一个结点
  9.         /*找到最后一个结点*/
  10.         for(target = *pNode; target->next != *pNode;target = target->next)
  11.                         ;

  12.                 temp = *pNode;
  13.         *pNode = (*pNode)->next;
  14.         target->next = *pNode;
  15.         free(temp);
  16.     }
  17.     else
  18.         {
  19.         target = *pNode;

  20.                 for( ; j < i-1; ++j)
  21.                 {
  22.                         target = target->next;
  23.                 }
  24.                
  25.                 temp = target->next;
  26.         target->next = temp->next;
  27.         free(temp);
  28.     }
  29. }
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 14:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表