Implementation of two-way linked list

Source: Internet
Author: User

The previous blog article introduced how to use the C language to implement a single-chain table. This blog article introduces the implementation of a two-way linked list. Each node in a single linked list has only one rear drive, while each node in the two-way linked list has a rear drive and a front drive (except that the first node has only one rear drive, and the last node has only one front drive ). Each node in a two-way linked list has a data domain and two pointer domains pointing to the forward and next nodes. Code implementation:

First, you must create a node struct: double_node

typedef struct Double_Node{int data;Double_Node *front;Double_Node *next;}Double_Node,*DoubleLink;
The following is a function for creating a node: create_node (INT). The parameter is the data of the Data Field of the created node:

DoubleLink Create_Node(int value){DoubleLink p=NULL;p=new Double_Node;p->data=value;p->next=NULL;return p;}
Next, write the create_link (INT) function for creating a two-way linked list with a specific length. The parameter is the length of the created two-way linked list:

DoubleLink create_Link(int number){if(number==0) return false;int x=0;int y=0;cin>>y;DoubleLink p1=Create_Node(y);DoubleLink head=p1;p1->front=NULL;x++;DoubleLink p2;while(x<number){cin>>y;p2=Create_Node(y);p1->next=p2;p2->front=p1;p1=p2;x++;}p1->next=NULL;return head;}
The following is the function for inserting a node into a two-way linked list: insertvalue (doublelink &, INT). The first parameter is the header of the two-way linked list to be inserted, the second parameter is the data of the Data Field of the node to be inserted:

bool insert_Value(DoubleLink &D,int e){DoubleLink temp=Create_Node(e);DoubleLink head=D;if(eFinally, write a program to test the code of the two-way linked list: to input a specific number of integer data, and then output in ascending order:

Void main () {doublelink head = NULL; int x = 0; int n = 0; cout <"Number of prepared input data:"; CIN> N; head = create_link (1); For (INT I = 0; I <N-1; I ++) {CIN> X; insert_value (Head, x);} while (Head! = NULL) {cout The test result is:







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.