Each node in a doubly-linked list contains two pointer fields, a pointer field containing the memory address of its successor, and a memory address stored by a pointer to its domain precursor node.
Description of the type describing the junction of the doubly linked list:
The type description of the doubly linked list describes typedef int ELEMTYPE;TYPEDEF struct NODE{ELEMTYPE data;struct node *prior,*next;} Dulnode,*dulinklist;
Of The prior domain holds the storage address of its predecessor node, and the next field stores the storage address of its successor node.
The two-way list has two features: one is the ability to search for a node in two directions, which makes certain operations (such as insertions and deletions) of the list simpler, and the second is the ability to traverse the entire doubly linked list regardless of whether the front or back chain is used.
The operation of a doubly linked list is essentially the same as a single-linked list.
1. Head interpolation creates a doubly linked list of lead nodes create_dlinklistf (int n)
Head interpolation creates a doubly linked list of lead nodes dulinklist create_dlinklistf (int n) {dulinklist L,p;int i = n-1; Elemtype x;//new head node L = (dulinklist) malloc (sizeof (Dulnode)); L->prior = NULL; L->next = null;//joins the first node scanf ("%d", &x);p = (dulinklist) malloc (sizeof (Dulnode));p->data = x; L->next = P;p->prior = L;p->next = null;//joins 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. The tail interpolation method to create the leading node of the doubly linked list create_dlinklistr (int n)
The tail interpolation method creates a doubly linked list of leading nodes dulinklist create_dlinklistr (int n) {dulinklist L,p,lastnode;int i = n-1; Elemtype x;//new head node L = (dulinklist) malloc (sizeof (Dulnode)); L->prior = NULL; L->next = null;//joins 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;//joins other nodes while (i > 0) {scanf ("%d", &x);p = (dulinklist) m Alloc (sizeof (Dulnode));p->data = X;lastnode->next = P;p->prior = Lastnode;p->next = Null;lastnode = p;i--;} return L;}
3. Insert new node Insert_dlinklistbefore (dulinklist P,elemtype x) before specifying a node
Inserts a new node void Insert_dlinklistbefore (Dulinklist p,elemtype x) {dulinklist newnode;//to infer the legitimacy of the node before the node p before it is specified: if (p-> Prior = = NULL) printf ("node is illegal.") The node 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 a new node after the specified node Insert_dlinklistafter (dulinklist p,elemtype x)
Inserts a new node after the specified node void insert_dlinklistafter (Dulinklist p,elemtype x) {dulinklist Newnode;newnode = (dulinklist) malloc ( sizeof (Dulnode)); newnode->data = x;//When the insertion position is after the last node if (P->next = = NULL) {P->next = Newnode;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)
Deletes the specified node void delete_dlinklist (Dulinklist p) {//assumes that 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. Post-chain output bidirectional linked list print_dlinklistn (Dulinklist L)
Post-chain output bidirectional list void Print_dlinklistn (Dulinklist p) {while (P! = NULL) {printf ("%d\t", p->data);p = P->next;} printf ("\ n");}
7. Forward Chain output bidirectional link list print_dlinklistp (dulinklist p)
Front chain output bidirectional list void print_dlinklistp (Dulinklist p) {while (P! = NULL) {printf ("%d\t", p->data);p = P-prior;} printf ("\ n");}
As for the other operations of the doubly linked list. such as positioning. and single-linked list operation is similar, no longer repeat.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Data structure (c reach)-------doubly linked list