(线性结构)循环链表的初始化,增删减除结点。新手的了解

1.查找rear指针为O(1) 那么开始节点就为rear->next->next,也是O(1)   //大家可以想象从最后一个结点开始有什么好处
2.head->next ==head 判断是否空表

//链表的定义
typedef struct CLinkList 
{
    ElemType data;
    struct CLinkList;
 } node ;
 
 1 //插入结点   
 2  void ds_insert (node **pNode,int i)
 3  {
 4     node *temp;
 5     node *target;
 6     node * p;
 7     ElemType item;
 8     int j=1;
 9     printf("输入要插入结点的值:");
10     scanf ("%d",&item);
11 
12 //插入的节点为头节点  
13     if (i==1)
14     {
15         temp=(*node)malloc(sizeoof (struct CLinkList));
16         
17         if (!temp)
18            exit (0);
19            
20            temp->date =item;
21            
22                for(target = (*pNode); target->next !=(*pNode); target =target->next);//如果next不是指向头,不断循环
23                
24                temp->next = (*pNode);
25                target -> next = temp;
26                *pNode = temp;  //插入的结点为头节点,不要漏写 
27            
28      } 
29      
30      else
31      {
32          target =*pNode;
33         
34         for (;j<(i-1);++j)   //循环到要插入的位置 
35         {
36             target =target->next;
37          } 
38          temp =(node*)malloc(sizeoof (struct CLinkList));
39          
40          if (!temp)
41              exit(0);
42              
43              temp -> data = item;
44              
45              p=target -> next;        //p先存放要插入位置节点的后一个元素的地址 
46              target -> next = temp;   //当前结点指向新插入的节点 
47              temp -> next = p;        //新节点指向本来要插入位置元素的后一个结点 
48      } 
49  }

 1 //删除节点
 2  void  ds_detele (node **pNode,int i)
 3  {
 4      node  *target;
 5      node  *temp;
 6      int j=1;
 7      if (i==1)
 8      {
 9            for(target = (*pNode); target->next !=(*pNode); target =target->next);//如果next不是指向头,不断循环
10      
11         temp = *pNode; 
12         
13         *pNode =(*pNode)->next;  //因为删除第一个节点,所以把第二个节点赋值给第一个节点,*pNode始终指向第一个节点, 
14         target->next =*pNode;    //把末节点指向首节点 
15         
16         free(temp);             // 释放原来的第一个节点 
17      }
18      else
19      {
20          target=*pNode;
21          
22          for (;j<i-1;++j)   //找出要删除的节点 的前一个节点 
23            {
24                target =target->next;
25            }
26     
27     
28         temp=target->next;  
29         target->next=temp->next;  // 这两句等于target=target->next->next 
30         
31         free(temp);
32      }
33  }

图片是自己整理思路写出来的,比较丑,以后写好点

 1 //返回节点的所在的位置
 2 int ds_search(node *pNode,int elem) 
 3 {
 4     node *target;
 5     int i=1;
 6     for (target =pNode;target->datadate !=elem && target->next!=pNode;++i)
 7     {
 8         target =target->next;
 9      } 
10      
11      if (target->next ==pNode)
12           return 0;
13           
14      else
15           return i; 
16 }

返回节点比较简单,大家看看就会

 1 //初始化循环链表 
 2 void ds_init (node **pNode)
 3 {
 4     int item;
 5     node *temp;
 6     node *target;
 7     printf("输入节点的值,输入0完成初始化
");
 8     
 9     while (1)
10     {
11         scanf ("%d",&item);
12         fflush(stdin);  //清除缓冲区,为了不影响下个数的输入 
13         
14         if(item==0)
15            return ;
16          
17          if(*pNode==NULL)
18          {   
19           //循环链表只有一个节点
20           *pNode =(node*)malloc(sizeof (struct CLinkList));
21            
22            if(!*pNode)   //分配空间失败,错误退出 
23               exit (0);
24               
25               (*pNode)->data = item ;
26               (*pNode)->next = *pNode;    //让尾节点指向头节点 
27           } 
28     
29          else 
30          {
31              //找到next指向的第一个结点的结点 
32              for(target = (*pNode); target->next !=(*pNode); target =target->next);//如果next不是指向头,不断循环
33              
34              temp=(*node)malloc (size(struct CLinkList));
35              
36              if (!temp)
37                 exit(0);
38             
39             temp->data =item ;
40             temp->next =*pNode;
41             target->next=temp; 
42              
43               
44          }
45     
46 }

感觉不太难,大家琢磨琢磨就会,大家可以写个解决约瑟夫问题的小程序,挺好玩了。

原文地址:https://www.cnblogs.com/biyongyao/p/5506819.html