Two-way cyclic linked list-C language version, cycle-C language version
Source File: # include <stdio. h> # include <string. h> # include <malloc. h> typedef int Elemtype; # include "Delist. h "int main () {Dlnode head = NULL; instruction (head); return 0;} header file section: typedef struct DLnode {Elemtype data; struct DLnode * prior; // The Declaration definition of the node struct DLnode * next;} DLnode, * Dlnode; void Init_Dlist (Dlnode & head) // bidirectional cyclic initialization {head = (Dlnode) malloc (sizeof (DLnode); head-> prior = head; head-> next = head;} int Empty_Dlist (Dlnode hea D) // two-way null determination {if (head-> prior = head & head-> next = head) return 1; return 0 ;} void Insert_Dlist (DLnode * head, Elemtype e) // header Insertion Method-bidirectional (new) {Dlnode p = NULL; p = (DLnode *) malloc (sizeof (DLnode )); // because it is a two-way cyclic linked list, there is no two-way problem if (! P) {printf ("Sorry, no more memory units have been allocated !!! \ N "); return;} p-> data = e; p-> next = head-> next; p-> next-> prior = p; p-> prior = head; // printf ("* % d * \ n", head-> next); head-> next = p;} int Length_Dlist (Dlnode head) {DLnode * p = NULL; int len = 0; if (Empty_Dlist (head) return 0; p = head-> next; while (p! = Head) {len ++; p = p-> next;} return len;} int Delete_Dlist (DLnode * head, Elemtype where) // Delete {DLnode * p = NULL; int I = 1; p = head-> next; if (Empty_Dlist (head) {printf ("sorry, the linked list is empty and cannot be deleted !!! \ N "); return 0;} if (where> Length_Dlist (head) | where <0) {printf (" sorry, the location you deleted is invalid, please enter it again !!! \ N "); return 0;} while (I <where) {p = p-> next; I ++ ;} p-> prior-> next = p-> prior-> next; p-> prior-> next-> prior = p-> prior; printf ("deleted successfully !!! \ N "); return p-> data;} void Insearch_Dlist (Dlnode head, Elemtype e) // query by element {DLnode * p = NULL; int len = 1; if (Empty_Dlist (head) {printf ("sorry, the linked list is empty and cannot be searched !!! \ N "); return;} p = head-> next; while (p! = Head) {if (p-> data = e) {printf ("the element you are looking for is located at % d of the linked list. \ n ", len); return;} p = p-> next; len ++;} printf (" sorry, the element you are looking for does not exist. Please enter it again !!! \ N "); return;} void Modify_Dlist (DLnode * head, Elemtype where, Elemtype e) // modify by position {DLnode * p = NULL; int len = 1; p = head-> next; while (len <where) {p = p-> next; len ++;} p-> data = e; printf ("modified successfully! \ N "); return;} void Print_Dlist (Dlnode head) // print operation {Dlnode p = head-> next; if (Empty_Dlist (head) {printf (" sorry, the linked list is empty and cannot be printed !!! \ N "); return;} while (head! = P) {printf ("% d", p-> data); p = p-> next;} printf ("\ n"); return ;} void Destory_Dlist (Dlnode head) // destroy the clear operation {Dlnode p = head-> next; while (p! = Head) {p-> prior-> next = p-> next; p-> next-> prior = p-> prior; p = head-> next ;} printf ("destroyed successfully! \ N ");} void instruction (Dlnode head) // function {int n, m, t, a, B, len1, index; printf ("\ t \ t1, initial operation \ n"); printf ("\ t \ t2, new operation \ n "); // Why cannot I define the head pointer here? Because after each function call, the head pointer is reinitialized with printf ("\ t \ t3, delete operation \ n "); printf ("\ t \ t4, search operation \ n"); printf ("\ t \ t5, modify operation \ n "); printf ("\ t \ t6, destruction operation \ n"); printf ("\ t \ t7, length operation \ n "); printf ("\ t \ t8, print operation \ n"); printf ("\ t \ t9, exit program \ n "); printf ("Enter the command you need to complete: \ n"); do {scanf ("% d", & n ); if (n <1 | n> 9) printf ("sorry, the command number you entered It is invalid. Please enter it again !!! \ N ") ;}while (n <1 | n> 9); switch (n) {case 1: Init_Dlist (head ); // initialize printf ("two-way linked list initialization has been completed. Enter the number of elements to add! \ N "); scanf (" % d ", & n); while (n --) {int x; scanf (" % d ", & x); Insert_Dlist (head, x);} printf ("Table creation completed! \ N "); break; case 2: // Add operation if (! Head) {printf ("sorry, please complete initialization before making this selection !!! \ N "); break;} printf (" Enter the number of elements you want to add! \ N "); scanf (" % d ", & n); while (n --) {int x; scanf (" % d ", & x); Insert_Dlist (head, x);} printf ("added successfully! \ N "); break; case 3: printf (" Enter the location of the node you want to delete: \ n "); scanf (" % d ", & n ); delete_Dlist (head, n); // Delete the break; case 4: printf ("Enter the element you want to search for: \ n"); scanf ("% d ", & m); Insearch_Dlist (head, m); // query operation break; case 5: if (Empty_Dlist (head) {printf ("sorry, the linked list is empty, the modification operation cannot be completed !!! \ N "); break;} printf (" Enter the queue location of the element you want to change: \ n "); // modify the do {scanf (" % d ", & a); if (a <1 | a> Length_Dlist (head) printf ("sorry, the element you entered is not in the region. Please enter it again !!! \ N ") ;}while (a <1 | a> Length_Dlist (head); printf (" Enter the modified value: \ n "); scanf ("% d", & B); Modify_Dlist (head, a, B); break; case 6: Destory_Dlist (head); // destroy operation break; case 7: len1 = Length_Dlist (head); // return the chain length operation printf ("the length of the current chain queue is % d \ n", len1); break; case 8: print_Dlist (head); // print operation break; case 9: // exit Operation return; default: instruction (head); break;} instruction (head );}