Algorithm 2---List 3---doubly linked list

Source: Internet
Author: User

1 Two-way chain List and realize 1.1 bidirectional link list in the double (to) list, there are two different links in the chain, that is, each node in addition to the next field to store the subsequent node address, but also add a pointer to its direct forward direction of the field prior. Doubly linked lists are easier to find, especially for large data traversal.   Note: The ① double-linked list header is only determined by head pointer.   It is convenient to ② the double-linked list with the lead node.  ③ links the head node and the tail node to the double (to) loop list. 1.2 Structure description and insert delete operation form Description: typedef struct dlistnode{DataType data; (data type changes as you want) struct Dlistnode *PR  Ior,*next;   }dlistnode;  typedef dlistnode *dlinklist; Dlinklist Head; Because of the symmetry of the double-linked list, the double-linked list can easily complete various insertion and deletion operations. 1.2.1 Insert operation Note that the arrows are not in the straight-in box but the overall representation points to the entire node including prior data next;

void Dinsertbefore (Dlistnode *p,datatype x) {//In the doubly linked list of lead nodes, insert a new node with a value of x before inserting *p, set p≠null dlistnode *s=malloc (sizeof (D ListNode))//① (dynamically allocating memory for linked list nodes) S->data=x;//② (assigns the value of data X to S->data) S->prior=p->prior;//③ (node The value of the precursor of P is assigned to the precursor of s so that the precursor of S is pointed to the previous node before P S->next=p;//④ (so that the S's rear drive point P passes through 2.3.4 steps node s each part is assigned value is finished) P->prior->next =s;//⑤ (The post-drive point of the node before P) P->prior=s;//⑥ (to make P's precursor pointing to s)} NOTE: The order of step ⑤⑥ cannot be changed because the 6th operation affects fifth step, and the 1.2.2 delete operation on the double linked list deletes the node *p itself. For

void Ddeletenode (Dlistnode *p) {//In the doubly linked list of the leading nodes, delete the node *p, set the *p as the non-terminal node P->prior->next=p->next;//① (so that the previous node of P The rear drive directly points to the original p of the rear drive) P->next->prior=p->prior;//② (so that the precursor of P's next node directly to the previous node of the original P) free (p);//③ (   Release P's memory this is important especially when dealing with large amounts of data} Note: Unlike insert and delete operations on a single-linked list, inserting and deleting in a doubly linked list must modify pointers in two directions at the same time. Both algorithms have a time complexity of O (1). 3.2.3 Code Implementation
#include <stdio.h>#include<stdlib.h>typedefstructdoublelinkedlist{intdata; structDoublelinkedlist *Pre; structDoublelinkedlist *Next;} Dlinkedlist_node;//Create a linked listdlinkedlist_node*Createdlink () {Dlinkedlist_node*head,*p,*s; intx; Head= (dlinkedlist_node*)malloc(sizeof(Dlinkedlist_node)); P=Head;  while(1) {printf ("Please input the data: \ n"); scanf ("%d",&x); if(X! =65535) {s= (dlinkedlist_node*)malloc(sizeof(Dlinkedlist_node)); S->data =x; SPre =p; P->next =s; P=s; }        Else{printf ("\ nthe data entry ends \ n");  Break; }} P->next =NULL; Head= HeadNext; Head->pre =NULL; returnhead;}//sequential, reversed-order print chain listvoidPrintdlink (Dlinkedlist_node *head) {Dlinkedlist_node*p,*s; P=Head; printf ("positive sequence output doubly linked list: \ n");  while(p) {printf ("%d",p->data); S=p; P= p->Next; } printf ("\ n Reverse output doubly linked list: \ n");  while(s) {printf ("%d",s->data); S= s->Pre; } printf ("\ N");}//Delete a nodeDlinkedlist_node* Deletedlinkedlist_node (Dlinkedlist_node *head,inti) {Dlinkedlist_node*p; P=Head; if(P->data = =i) {head= p->Next; Head->pre =NULL;  Free(P); returnHead; }     while(p) {if(P->data = =i) {p->pre->next = p->Next; P->next->pre = p->Pre;  Free(P); returnHead; } P= p->Next; } printf ("the data you want to delete is not found \ n"); returnhead;}//Insert a node.Dlinkedlist_node* Insertdlinkedlist_node (Dlinkedlist_node *head,inti) {Dlinkedlist_node*p,*temp; P=Head; Temp= (dlinkedlist_node*)malloc(sizeof(Dlinkedlist_node)); Temp->data =i; if(I < P->data)//smaller than head node data, inserted into the list header{Head=temp; Head->next = p;//here P is the original headHead->pre =NULL; P->pre = head;//here P is the original head        returnHead; }     while(P! = NULL && i > P->data)//find the right insertion position{p= p->Next; }    if(I < P->data)//find a suitable insertion point somewhere in the middle of the list{Temp->next =p; Temp->pre = p->Pre; P->pre->next =temp; P->pre =temp; returnHead; }    Else//no suitable location is found, only data is inserted at the end of the list{p->next = temp;//traverse to the tail of the list, P==nullTemp->pre =p; Temp->next =NULL; returnHead; }}intMain () {Dlinkedlist_node*Head; Head=Createdlink ();    Printdlink (head); Head= Insertdlinkedlist_node (Head,1012); Head= Deletedlinkedlist_node (Head,1991); Printdlink (head);}

Algorithm 2---List 3---doubly linked list

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.