Single-cycle linked list and cyclic linked list

Source: Internet
Author: User

Single-cycle linked list and cyclic linked list

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 typedef struct Node {5 int data; 6 struct Node * next; 7} Node, * LinkList; 8 9 void InitialList (LinkList * L) {10 * L = (LinkList) malloc (sizeof (struct Node); 11 if (! (* L) {12 printf ("malloc failed! \ N "); 13 exit (1); 14} 15 (* L)-> next = (* L); 16} 17 void ClearList (LinkList * L) {18 LinkList q, p = (* L)-> next; 19 while (p! = * L) {20 q = p-> next; 21 free (p); 22 p = q; 23} 24 (* L)-> next = * L; 25} 26 void DestroyList (LinkList * L) {27 LinkList q, p = * L; 28 while (p-> next! = * L) {29 q = p-> next; 30 free (p); 31 p = q; 32} 33 free (* L); 34 * L = NULL; 35} 36 int IsEmpty (LinkList L) {37 if (L-> next = L) return 1; 38 else return 0; 39} 40 int Length_SCL (LinkList L) {41 int length = 0; 42 LinkList cursor = L; 43 while (cursor-> next! = L) {44 length ++; 45 cursor = cursor-> next; 46} 47 return length; 48} 49 void GetElement_SCL (LinkList L, int position) {50 int length = Length_SCL (L); 51 if (position <1 | position> length) {52 printf ("Error: getElemet: position error! \ N "); 53} 54 else {55 int count = 0; 56 while (count <position) {57 L = L-> next; 58 count + = 1; 59} 60 printf ("getElement in position % d is: % d \ n", position, L-> data); 61} 62} 63 int ElementLocate_SCL (LinkList L, int value) {64 // return the following table based on the given value. If 0 is returned, 65 int index = 1; 66 LinkList p = L-> next; 67 while (p! = L) {68 if (p-> data = value) {69 return index; 70} 71 p = p-> next; 72 index + = 1; 73} 74 return 0; 75} 76 void PriorElement_SCL (LinkList L, int current_element) {77 if (! ElementLocate_SCL (L, current_element) 78 printf ("% d is not belong to this List! \ N ", current_element); 79 else {80 if (ElementLocate_SCL (L, current_element) = 1) {81 LinkList p = L; 82 while (p-> next! = L) 83 p = p-> next; 84 printf ("PriorElement: before % d is % d \ n", current_element, p-> data ); 85} 86 else {87 LinkList p = L-> next; 88 LinkList q = L; 89 while (p! = L) {90 if (p-> data = current_element) {91 printf ("PriorElement: before % d is % d \ n", current_element, q-> data ); 92 break; 93} 94 q = p; 95 p = p-> next; 96} 97} 98} 99} 100 void NextElement_SCL (LinkList L, int current_element) {101 if (! ElementLocate_SCL (L, current_element) 102 printf ("% d is not belong to this list! \ N "); 103 else {104 if (ElementLocate_SCL (L, current_element) = Length_SCL (L) 105 printf (" NextElement: next % d is % d \ n ", current_element, L-> next-> data); 106 else {107 LinkList p = L; 108 LinkList q = L-> next; 109 while (q! = L) {110 if (p-> data = current_element) {111 printf ("NextElement: next % d is % d \ n", current_element, q-> data ); 112 break; 113} 114 p = q; 115 q = q-> next; 116} 117} 118} 119 void InsertList_SCL (LinkList * L, int position, int value) {121 int length = Length_SCL (* L); 122 if (position <1 | position> length + 1) {123 printf ("Error: insertList: position \ n "); 124} 125 else {126 LinkList s = (LinkList) malloc (sizeof (Struct Node); 127 s-> data = value; 128 int count = 0; 129 LinkList p = * L; 130 while (count <position-1) {131 p = p-> next; 132 count ++; 133} 134 s-> next = p-> next; 135 p-> next = s; 136 printf ("inserted % d before index % d \ n", value, position); 137} 138} 139 void DeleteList_SCL (LinkList * L, int position) {140 int length = Length_SCL (* L); 141 if (position <1 | position> length) {142 printf ("Error: deleteList: posi Tion! \ N "); 143} 144 else {145 int count = 0, value; 146 LinkList p = * L; 147 while (count <position-1) {148 p = p-> next; 149 count ++; 150} 151 value = p-> next-> data; 152 LinkList temp = p-> next; 153 p-> next = p-> next; 154 free (temp); 155 printf ("deleted % d position is % d \ n", value, position ); 156} 157} 158 void TailCreate_SCL (LinkList * L, int count) {159 LinkList tail = * L, s; 160 int I = 0; 161 for (I = 0; I <count; I ++) {162 printf ("please enter % d element:", I + 1); 163 s = (LinkList) malloc (sizeof (struct Node )); 164 scanf ("% d", & (s-> data); 165 s-> next = tail-> next; 166 tail-> next = s; 167 tail = s; 168} 169} 170 void Display_SCL (LinkList L) {171 printf ("------ display executed! ---------- \ N "); 172 int length = Length_SCL (L); 173 L = L-> next; 174 int I; 175 for (I = 1; I <= length; I ++) {176 printf ("% d", L-> data); 177 L = L-> next; 178} 179 printf ("\ n "); 180} 181/* will output 1, 2, 3, undefined number (data at the header node) in an endless loop ), the next of the 182 * cyclic linked list End Node in this file points to the header node rather than the first node. 183 * to check whether the "ring" is retained after the operation is performed, 184 **/185 void Display_SCL_1 (LinkList L) {186 while (L-> next! = NULL) {187 printf ("% d", L-> data); 188 L = L-> next; 189} 190} 191 int main () {192 LinkList L; 193 InitialList (& L); 194 TailCreate_SCL (& L, 3); 195 Display_SCL (L); 196 // DestroyList (& L); 197 ClearList (& L ); 198 if (IsEmpty (L) 199 printf ("List is Empty! \ N "); 200 else201 printf (" there is element in List \ n "); 202 int r1 = Length_SCL (L ); 203 printf ("the length of list is % d \ n", r1); 204 Display_SCL (L); 205 TailCreate_SCL (& L, 5); 206 GetElement_SCL (L, 4); 207 GetElement_SCL (L, 6); 208 GetElement_SCL (L, 1); 209 GetElement_SCL (L, 5); 210 211 if (ElementLocate_SCL (L, 1 )) 212 printf ("the position of 1 is % d \ n", ElementLocate_SCL (L, 1); 213 else214 printf ("there is no this el Ement is this list! \ N "); 215 if (ElementLocate_SCL (L, 8) 216 printf (" the position of 8 is % d \ n ", ElementLocate_SCL (L, 8 )); 217 else218 printf ("there is no this element is this list! \ N "); 219 if (ElementLocate_SCL (L, 9) 220 printf (" the position of 9 is % d \ n ", ElementLocate_SCL (L, 9 )); 221 else222 printf ("there is no this element is this list! \ N "); 223 if (ElementLocate_SCL (L, 5) 224 printf (" the position of 5 is % d \ n ", ElementLocate_SCL (L, 5 )); 225 else226 printf ("there is no this element is this list! \ N "); 227 228 // test PriorElement229 printf (" \ n ++ + test priorElement ++ \ n "); 230 priorelement_check (L, 1); 231 priorelement_check (L, 2); 232 priorelement_check (L, 6); 233 priorelement_check (L, 9); 234 priorelement_check (L, 5 ); 235 // test NextElement236 printf ("\ n ++ + test nextElement ++ \ n"); 237 NextElement_SCL (L, 5 ); 238 nextelement_check (L, 1); 239 nextelement_check (L, 2); 240 nextelement_check (L, 17 ); 241 printf ("\ n ++ test InsertList ++ \ n"); 242 InsertList_SCL (& L, 2,888 ); 243 Display_SCL (L); 244 InsertList_SCL (& L, 1,666); 245 Display_SCL (L); 246 InsertList_SCL (& L, 8,999); 247 Display_SCL (L ); 248 InsertList_SCL (& L, 249); 250 Display_SCL (L); // Display_SCL_1 (L ); 251 printf ("\ n ++ test DeleteList ++ \ n"); 252 DeleteList_SCL (& L, 5 ); 253 Display_SCL (L); 254 DeleteList_SCL (& L, 8); 255 Display_SCL (L); 256 DeleteList_SCL (& L, 1); 257 Display_SCL (L ); 258 // Display_SCL_1 (L); 259 return 0; 260}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.