Data structure (C implementation) ------- two-way linked list
Each node in a two-way linked list contains two pointer fields. one pointer field stores the storage address of its successor node, and the other pointer field stores the storage address of its predecessor node.
Type description of the two-way linked list node:
// Type description of two-way linked list typedef int ElemType; typedef struct node {ElemType data; struct node * prior, * next;} DuLNode, * DuLinkList;
The prior domain stores the storage address of its precursor node, and the next domain stores the storage address of its successor node.
Two-way linked list has two features: one is that you can search for a node from two directions, which makes some operations (such as insertion and deletion) of the linked list easier; second, the whole two-way linked list can be traversed no matter whether the front or back chain is used.
The operations of a two-way linked list are basically the same as those of a single-chain table;
1. Create a two-way linked list Create_DLinkListF (int n) of the lead node by using the header Insertion Method)
// Create a two-way linked list DuLinkList Create_DLinkListF (int n) {DuLinkList L, p; int I = n-1; ElemType x; // create a new header node L = (DuLinkList) malloc (sizeof (DuLNode); L-> prior = NULL; L-> next = NULL; // Add the first node scanf ("% d", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; l-> next = p; p-> prior = L; p-> next = NULL; // Add other nodes while (I> 0) {scanf ("% d ", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; p-> next = L-> next; l-> next-> prior = p; p-> prior = L; L-> next = p; I --;} return L ;}
2. Create a two-way linked list Create_DLinkListR (int n) with the lead node by means of the End Plug)
// Create a two-way linked list DuLinkList Create_DLinkListR (int n) {DuLinkList L, p, lastNode; int I = n-1; ElemType x; // create a new header node L = (DuLinkList) malloc (sizeof (DuLNode); L-> prior = NULL; L-> next = NULL; // Add the first node scanf ("% d", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; l-> next = p; p-> prior = L; p-> next = NULL; lastNode = p; // Add other nodes while (I> 0) {scanf ("% d", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; lastNode-> next = p; p-> prior = lastNode; p-> next = NULL; lastNode = p; I --;} return L ;}
3. Insert the new node Insert_DLinkListBefore (DuLinkList p, ElemType x) before the specified node)
// Insert the new node void Insert_DLinkListBefore (DuLinkList p, ElemType x) {DuLinkList newNode before the specified node; // determine the legality of the node before node p: if (p-> prior = NULL) printf ("the node is invalid and cannot be inserted before the node \ n"); else {newNode = (DuLinkList) malloc (sizeof (DuLNode); newNode-> data = x; newNode-> next = p; p-> prior-> next = newNode; newNode-> prior = p-> prior; p-> prior = newNode ;}}
4. Insert the new node Insert_DLinkListAfter (DuLinkList p, ElemType x) after the specified node)
// Insert the new void Insert_DLinkListAfter (DuLinkList p, ElemType x) {DuLinkList newNode; newNode = (DuLinkList) malloc (sizeof (DuLNode) after the specified node )); newNode-> data = x; // if (p-> next = NULL) {p-> next = newNode when the insertion position is after the last node; newNode-> prior = p; newNode-> next = NULL;} else {newNode-> next = p-> next; p-> next-> prior = newNode; p-> next = newNode; newNode-> prior = p ;}}
5. Delete the specified node Delete_DLinkList (DuLinkList p)
// Delete the specified node void Delete_DLinkList (DuLinkList p) {// if the last element is deleted, if (p-> next = NULL) p-> prior-> next = NULL; else {p-> prior-> next = p-> next; p-> next-> prior = p-> prior ;} free (p );}
6. Two-way linked list Print_DLinkListN (DuLinkList L)
// The back-link outputs the two-way linked list void Print_DLinkListN (DuLinkList p) {while (p! = NULL) {printf ("% d \ t", p-> data); p = p-> next;} printf ("\ n ");}
7. Print_DLinkListP (DuLinkList p)
// The frontend chain outputs the two-way linked list void Print_DLinkListP (DuLinkList p) {while (p! = NULL) {printf ("% d \ t", p-> data); p = p-prior;} printf ("\ n ");}
As for other operations of a two-way linked list, such as positioning, similar to the operations of a single-chain table, we will not repeat them here.