# Include <iostream> using namespace STD; struct linknode {int value; linknode * Next; linknode * pre;}; linknode * createdoubleroundlinklist () {linknode * head = (linknode *) malloc (sizeof (linknode); head-> next = head; head-> pre = head; head-> value = 0; return head;} bool insertelem (INT POs, int value, linknode * head) {If (Pos <= 0) return 0; linknode * P = head; Int J = 0; while (p-> next! = Head & J <pos-1) // any element located to the pos-1, that is, the position of the element to be inserted before, in fact, the book is written to the POs position, but it cannot be inserted at the end {P = p-> next; j ++;} If (j = pos-1) {linknode * newnode = (linknode *) malloc (sizeof (linknode); newnode-> value = value; newnode-> next = p-> next; P-> next = newnode; newnode-> pre = P; newnode-> next-> pre = newnode; // you do not need to specify a boundary at the end of the linked list, its next is also not empty head-> value ++; return 1;} return 0;} bool deleelem (INT POs, linknode * head) {If (Pos <= 0) Return 0; linknode * P = head; Int J = 0; while (p-> next! = Head & J <POS) {P = p-> next; j ++;} If (j = POS) // you can locate the POs element here, that is, the location of the element to be deleted. Here, j = POS is used to determine the validity of the element, the reason why P = head is not used is the same as that of the cyclic single-chain table {P-> pre-> next = p-> next; P-> next-> pre = p-> pre; // P-> next will not be empty Delete (p); head-> value --; return 1;} return 0;} void clearlinklist (linknode * head) {linknode * P = head-> next; while (P! = Head) {linknode * q = p-> next; deleelem (1, head); // Delete the first one each time, delete p = Q one by one ;}} void destroylinklist (linknode * head) {clearlinklist (head); Delete head; head = 0;} void output (linknode * head) {If (Head = NULL) {cout <"Link List dose not exist" <Endl; return;} If (Head-> value = 0) {cout <"Link List is empty" <Endl; return;} linknode * P = head-> next; while (P! = Head) {cout <p-> value <""; P = p-> next;} cout <Endl;} void main () {int Len = 12; linknode * l = createdoubleroundlinklist (); int V; For (INT I = 0; I <Len; I ++) {v = rand () % 100; cout <v <""; insertelem (1, V, L); // insert each time at the end of the linked list, the input order is the opposite to the order in the linked list} cout <Endl; output (l); destroylinklist (l); // output (l); L = createdoubleroundlinklist (); for (INT I = 0; I <Len; I ++) {v = rand () % 100; cout <v <""; insertelem (L-> value + 1, V, L); // insert each time at the end of the linked list to make the input sequence consistent with the sequence in the linked list} cout <Endl; output (l); insertelem (3,999, L); output (l); insertelem (100,999, L); output (l); insertelem (L-> value, 9999, l); output (l); insertelem (L-> value +, L); output (l); insertelem (, L); output (L ); deleelem (1, L); output (l); deleelem (100, L); output (l); deleelem (7, L); output (L ); deleelem (L-> value, L); output (l); deleelem (L-> value + 1, L); output (l); clearlinklist (L ); output (l); insertelem (1, 2, L); output (l); deleelem (L-> value, L); output (l); CIN> Len ;}
Circular Double-chain table of linear table