Basic operations of a linked list-two-way linked list

Source: Internet
Author: User

The two-way linked list is also determined by the unique header pointer.
A two-way linked list can be connected at the beginning and end of a chain table.

/* Basic operations on the linked list ~~ Development Environment: vs2010win32 console application. */# include "stdafx. H "# include <iostream> using namespace STD; struct node {int data; node * Next; // points to the node * pre; // points to the frontend node }; node * createbidirlist (int n); void outputbidirlist (node * head); node * insertdata (int n, node * head); node * deletedata (int n, node * head ); int main () {int N; node * listhead = NULL; cout <"Please enter the number of nodes:"; CIN> N; // you can enter 5 (1 2 3 4 5) if (n> 0) listhead = createbidirlist (n); outputbidirlist (listhead); insertdata (7, listhead ); outputbidirlist (listhead); insertdata (6, listhead); deletedata (2, listhead); outputbidirlist (listhead); Return 0 ;}

// Create a two-way linked list node * createbidirlist (int n) {node * temp, * tail = NULL, * head = NULL; int num; CIN> num in the order of data input; head = new node; // dynamically allocates memory for the new node if (Head = NULL) {cout <"no memory available! "; Return NULL;} else {head-> DATA = num; head-> next = NULL; head-> pre = NULL; tail = head ;} for (INT I = 0; I <n-1; I ++) {CIN> num; temp = new node; // dynamically allocate memory for the new node if (temp = NULL) {cout <"no memory availabe"; return head;} else {temp-> DATA = num; temp-> next = NULL; temp-> pre = tail; tail-> next = temp; tail = temp;} return head ;}

 

 

// Output the value of the data member of each node in the two-way linked list void outputbidirlist (node * head) {cout <"list:"; node * curnode = head; while (curnode) {If (curnode-> pre) cout <"<-"; cout <curnode-> data; If (curnode-> next) cout <"-> "; curnode = curnode-> next;} cout <Endl; return ;}

// Insert the integer n into a sorted two-way linked list (from small to large) node * insertdata (int n, node * head) {node * curnode = head; // point to the node after the insertion point * prenode = NULL; // point to the node before the insertion point * newnode = NULL; // point to the new node // find the insert position while (curnode! = NULL) & (curnode-> data <n) {prenode = curnode; curnode = curnode-> next;} newnode = new node; // dynamically allocate memory for the new node if (newnode = NULL) {// memory allocation failure cout <"not memory available! "; Return head;} newnode-> DATA = N; If (prenode = NULL) {// newnode-> next = curnode; newnode-> pre = NULL; if (curnode! = NULL) curnode-> pre = newnode; return newnode;} If (curnode = NULL) {// newnode-> pre = prenode; prenode-> next = newnode; newnode-> next = NULL; return head;} else {// prenode-> next = newnode; newnode-> next = curnode; newnode-> pre = prenode; curnode-> pre = newnode; return head ;}}
// Search for and delete the specified node * deletedata (int n, node * head) {node * curnode = head in the two-way linked list; // point to the current node while (curnode & curnode-> data! = N) curnode = curnode-> next; If (curnode = NULL) {cout <"can't find" <n <Endl; return head ;} if (curnode-> pre = NULL) {head = head-> next; head-> pre = NULL ;} else {curnode-> pre-> next = curnode-> next; If (curnode-> next! = NULL) curnode-> next-> pre = curnode-> pre;} Delete curnode; return head; // return the first pointer of the chain}

 

 

 

 

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.