1. 程式人生 > >線性表->鏈式存儲->循環鏈表

線性表->鏈式存儲->循環鏈表

next 一個 lse create open 元素 class ext linklist

文字描述

  循環鏈表是另一種形式的鏈式存儲結構。它的特點是表中最後一個結點的指針域指向頭結點,整個鏈表形成一個環。由此,從表中任一結點出發均可找到表中其他結點。

示意圖

技術分享圖片

算法分析

插入、刪除、查找等同單鏈表。

代碼實現

技術分享圖片
  1 //
  2 // Created by lady on 19-1-27.
  3 //
  4 
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 
  8 //線性表的單向循環鏈表存儲結構
  9 typedef struct ElemType{
 10     char data[10];
11 }ElemType; 12 typedef struct CNode{ 13 ElemType e; 14 struct CNode *next; 15 }CNode; 16 typedef struct { 17 CNode *head;//指向第一個指針 18 CNode *tail;//指向最後一個指針 19 int length;//表示鏈表數據元素個數 20 }CLinkNode, *CLinkList; 21 22 //構造一個空的線性表 23 static int InitList_CL(CLinkList *L) 24 {
25 (*L) = (CLinkList)malloc(sizeof(CLinkNode)); 26 (*L)->length = 0; 27 (*L)->head = NULL; 28 (*L)->tail = NULL; 29 } 30 31 //銷毀線性表 32 static int Destory_CL(CLinkList *L) 33 { 34 if(L == NULL || (*L) == NULL){ 35 return -1; 36 } 37 CNode *p = (*L)->head;
38 CNode *q; 39 while(p){ 40 q = p; 41 p = p->next; 42 free(q); 43 if(p == (*L)->head) 44 break; 45 } 46 free(*L); 47 (*L) = NULL; 48 } 49 50 //返回1表示空, 0表示非空 51 static int ListEmpty_CL(CLinkList L) 52 { 53 return L->length ? 0: 1; 54 } 55 56 //返回線性表L中的數據元素個數 57 static int ListLength_CL(CLinkList L) 58 { 59 return L->length; 60 } 61 62 //在線性表末尾插入數據元素e 63 static int ListInsert_CL(CLinkList *L,ElemType e) 64 { 65 CNode *new = (CNode *)malloc(sizeof(CNode)); 66 if(new == NULL){ 67 return -1; 68 } 69 CNode *p; 70 new ->e = e; 71 new->next = NULL; 72 if(ListEmpty_CL(*L)){ 73 (*L)->length += 1; 74 new->next = new; 75 (*L)->head = new; 76 (*L)->tail = new; 77 }else{ 78 (*L)->length +=1; 79 new->next = (*L)->head; 80 (*L)->tail->next = new; 81 (*L)->tail = new; 82 } 83 return 0; 84 } 85 86 //刪除L中的第loc個數據元素,並將被刪元素的值存放在e中 87 static int ListDelete_CL(CLinkList *L, int loc, ElemType *e) 88 { 89 CNode *p = (*L)->head; 90 CNode *q = NULL; 91 int i = 1; 92 int f = 0; 93 while(p){ 94 if(i == loc){ 95 f = 1; 96 break; 97 } 98 i += 1; 99 q = p; 100 p = p->next; 101 if(p == (*L)->head){ 102 f = -1; 103 break; 104 } 105 } 106 if(f<1) 107 return -1; 108 (*L)->length -= 1; 109 (*e) = p->e; 110 if((*L)->length == 0){ 111 free(p); 112 (*L)->head = NULL; 113 (*L)->tail = NULL; 114 } 115 if(q == NULL){ 116 q = p->next; 117 free(p); 118 (*L)->tail->next = q; 119 }else{ 120 q->next = p->next; 121 free(p); 122 } 123 return 0; 124 } 125 126 //依次對L的每個數據元素調用函數fun 127 static int ListTraverse_CL(CLinkList L, int (*fun)(ElemType,int), char note[]) 128 { 129 printf("遍歷循環鏈表%s:", note); 130 CNode *p = L->head; 131 int i = 0; 132 while(p){ 133 if(fun(p->e, ++i)){ 134 return -1; 135 } 136 p = p->next; 137 if(p == L->head) 138 break; 139 } 140 printf("\n"); 141 return 0; 142 } 143 144 static int print(ElemType e, int loc) 145 { 146 printf("%3d=%-10s", loc, e.data); 147 return 0; 148 } 149 150 //創建一個長度為n的鏈表 151 static int CreateList_CL(CLinkList *L, int n, char note[]) 152 { 153 printf("創建一個長度為%d的循環鏈表%s!\n", n, note); 154 InitList_CL(L); 155 ElemType e; 156 int i = 0; 157 for(i=0; i<n; i++){ 158 printf("輸入第%d個元素:", i+1); 159 scanf("%s[^\\n]", e.data); 160 ListInsert_CL(L, e); 161 } 162 } 163 164 int main(int argc, char *argv[]) 165 { 166 CLinkList L; 167 ElemType e; 168 int location = 0; 169 170 CreateList_CL(&L, 5, "L:"); 171 ListTraverse_CL(L, print, "L"); 172 173 printf("輸入刪除元素的位置:"); 174 scanf("%d", &location); 175 ListDelete_CL(&L, location, &e); 176 printf("位於%d的元素%s已經從循環鏈表中被刪除!\n", location, e.data); 177 ListTraverse_CL(L, print, "L"); 178 179 Destory_CL(&L); 180 return 0; 181 }
循環鏈表

代碼運行

/home/lady/CLionProjects/untitled/cmake-build-debug/untitled
創建一個長度為5的循環鏈表L:!
輸入第1個元素:WU
輸入第2個元素:ZHENG
輸入第3個元素:WANG
輸入第4個元素:SUN
輸入第5個元素:LI
遍歷循環鏈表L:  1=WU          2=ZHENG       3=WANG        4=SUN         5=LI        
輸入刪除元素的位置:2
位於2的元素ZHENG已經從循環鏈表中被刪除!
遍歷循環鏈表L:  1=WU          2=WANG        3=SUN         4=LI        

Process finished with exit code 0

線性表->鏈式存儲->循環鏈表