C + + dual-linked list of basic operations (detailed) _c language

Source: Internet
Author: User
Tags stdin

1. The concept

Two-way linked list is also called double linked list, which is a kind of linked list, and it has two pointers in each data node, pointing to direct successor and direct precursor respectively. Therefore, starting from any node in a two-way list, it is easy to access its predecessor nodes and subsequent nodes. In general we construct bidirectional cyclic lists.

The structure diagram looks like this:

  

  

2. Basic Operation Example

DoubleList.cpp

#include "stdafx.h" #include "DoubleList.h" #include <stdio.h> #include <malloc.h> #include <stdlib.h
    > doublelist::D oublelist () {Pdoublelistnode pdoulist = NULL;
    Create double linked list createdoulist (pdoulist);
    Printdoulist (pdoulist);
    Print Reverse chain list printdoureverselist (pdoulist);
    The node is inserted after the node insertnodeafter (pdoulist);
    Printdoulist (pdoulist);
    The node is inserted before the node Insertnodebefore (pdoulist);
    Printdoulist (pdoulist);
    Delete node Deletenode (pdoulist);
    Printdoulist (pdoulist);
    Delete List deletedoulist (pdoulist);
    Printdoulist (pdoulist);
System ("PAUSE");     Doublelist::~doublelist () {}///create bidirectional linked list void doublelist::createdoulist (Pdoublelistnode &head) {char x;
  Defined as a char is used to enter the ' Q ' When you can exit, in fact, defined as an int can also exit Pdoublelistnode p, s;
  Head = (pdoublelistnode) malloc (sizeof (Doublelistnode));
  Head->next = NULL;    Head->prior = NULL;
  The Construction header node P p = head;
  printf ("\ n Enter the elements of a two-way list, press ENTER after each element is entered, enter Q to indicate the end. \ n");  Fflush (stdin); Empty input Buffer x = GetChar ();
    while (x!= ' Q ') {s = (Pdoublelistnode) malloc (sizeof (Doublelistnode)); S->data = x-' 0 ';
    Gets the ASCII code of the input character, minus 30H becomes the desired number s->next = NULL;
    S->prior = p;
    P->next = s;
    p = s;
    Fflush (stdin);
  x = GetChar ();
  } if (x = = ' Q ') {printf ("Two-way linked list constructed!\n");
  }//Print bi-directional list void Doublelist::P rintdoulist (Pdoublelistnode &head) {Pdoublelistnode p;
  printf ("\ n Print the bidirectional linked list data as: \ n"); if (!
    Isdoulistempty (head)) {p = head->next;
      while (p) {printf ("%d\n", p->data);
    p = p->next;
  Reverse print bidirectional list void Doublelist::P rintdoureverselist (Pdoublelistnode &head) {Pdoublelistnode p;
  printf ("\ n print out reverse bidirectional linked list data as: \ n"); if (!
    Isdoulistempty (head)) {p = head->next;
    while (p->next) {p = p->next;
      while (P->prior) {printf ("%d \ n", p->data);
    p = p->prior; List length int doublelist::getdoulistlength (Pdoublelistnode &head) {int length = 0;
  if (head = = NULL) {printf ("The list does not exist, please initialize the!\n first");
    else {Pdoublelistnode p = head->next;
      while (p) {length++;
    p = p->next;
} return length; }//Determine if the list is empty bool Doublelist::isdoulistempty (Pdoublelistnode &head) {if (head = = NULL) {printf ("The list does not exist, please initialize!").
    \ n ");
  return true;
    else if (Head->next = null) {printf ("linked list is empty!\n");
  return true;
return false; ///Put the bidirectional link table empty void doublelist::cleardoulist (Pdoublelistnode &head) {if (head = = NULL) {printf ("The list does not exist, please initialize!\n first"
  );
    else {Pdoublelistnode p, q;  p = q = head->next;
    is P, Q point to the first element head->next = NULL;
      while (p)//release of the element as memory {p = p->next;
      Free (q);
    Q = p;
  }///delete bidirectional list void Doublelist::D eletedoulist (Pdoublelistnode &head) {printf (\ n Delete bidirectional linked lists \ n);
  Cleardoulist (head);
  Free (head);
head = NULL; ///Insert element void Doublelist::insertnodeafter after position I in a bidirectional list (PdoublelisTnode &head) {int data, POS;
  Pdoublelistnode p, S;
  p = head;
  int i = 0;
  printf ("\ n Insert an element after the first position in a two-way list \ n");
  printf ("Enter the element and location to insert: \ n");
  scanf_s ("%d%d", &data, &pos, 100);
  if (head = = NULL) {printf ("The list does not exist, please initialize the!\n first");
    else if (Head->next = null) {printf ("The list is empty, insert the first element!\n");
    s = (pdoublelistnode) malloc (sizeof (Doublelistnode));
    S->data = data;  
    S->prior = NULL;
    S->next = NULL;    Head->next = s;
  Insert new node head after} else if (pos<1 | | pos>getdoulistlength (HEAD) + 1) {printf ("Insert position error!\n");
      else {while (I < pos) {p = p->next;
    i++; } if (i = = Getdoulistlength (head)//If insert data {s = (Pdoublelistnode) malloc after the last element (sizeof Doublelistnode
      ));
      S->data = data;
      S->next = NULL;
      S->prior = p;
    P->next = s;
      else {s = (Pdoublelistnode) malloc (sizeof (Doublelistnode));
      S->data = data; S-&gT;next = p->next;
      P->next->prior = s;
      P->next = s;
    S->prior = p;
  }}///insert element void Doublelist::insertnodebefore (Pdoublelistnode &head) {int data, POS before position I in a two-way list;
  Pdoublelistnode p, S;
  p = head;
  int i = 0;
  printf ("\ n insert element \ n before position I in a two-way list)";
  printf ("Enter the element and location to insert: \ n");
  scanf_s ("%d%d", &data, &pos, 100);
  if (head = = NULL) {printf ("The list does not exist, please initialize the!\n first");
    else if (Head->next = null) {printf ("The list is empty, insert the first element!\n");
    s = (pdoublelistnode) malloc (sizeof (Doublelistnode));
    S->data = data;
    S->prior = NULL;
    S->next = NULL;    Head->next = s;
  Insert new node head after} else if (pos<1 | | pos>getdoulistlength (HEAD) + 1) {printf ("Insert position error!\n");
      else {while (I < pos) {p = p->next;
    i++;
      if (i = = 1)//If the data {s = (Pdoublelistnode) malloc (sizeof (Doublelistnode)) is inserted before the first element;
      S->data = data;    Head->next = s; The newThe node is inserted into head after S->prior = head;            The front node of the new node points to the head node S->next = p;          The posterior node of the new node points to the posterior node of the original head p->prior = s;
      The former node of the original first node points to the new node} else {s = (Pdoublelistnode) malloc (sizeof (Doublelistnode));
      S->data = data;
      S->prior = p->prior;
      S->next = p;
      P->prior->next = s;
    P->prior = s;
  }}//delete the I element void Doublelist::D eletenode (Pdoublelistnode &head) {int pos;
  int i = 0;
  Pdoublelistnode p = head;
  printf ("\ n Delete elements of position I in a two-way list \ n");
  printf ("Please enter a location to delete:");
  
  scanf_s ("%d", &pos, 100);
  if (isdoulistempty) {return;
  else if (pos<1 | | pos>getdoulistlength (head)) {printf ("Deleted location does not exist!\n");
      else {while (I < pos) {p = p->next;
    i++;
      The IF (i = = Getdoulistlength (head)) {p->prior->next = NULL;
    Free (p);
      else {P->prior->next = p->next; p->next-&Gt;prior = p->prior;
    Free (p); }
  }
}

DoubleList.h

#pragma once typedef struct DOUBLELISTNODE {int data; data struct Doublelistnode *prior; precursor struct Doublelistnode *next;
subsequent}doublelistnode, *pdoublelistnode;
  Class Doublelist {public:doublelist ();
  ~doublelist ();
  Initializes a two-way list of void Doublelist::createdoulist (Pdoublelistnode &head);
  Print bidirectional list void Doublelist::P rintdoulist (Pdoublelistnode &head);
  Reverse print bidirectional list void Doublelist::P rintdoureverselist (Pdoublelistnode &head);
  To find the length of the chain table int doublelist::getdoulistlength (Pdoublelistnode &head);
  Determine if the linked list is an empty bool Doublelist::isdoulistempty (Pdoublelistnode &head);
  The two-way link table is placed in the empty void Doublelist::cleardoulist (Pdoublelistnode &head);
  Delete bidirectional list void Doublelist::D eletedoulist (Pdoublelistnode &head);
  Inserts the element m void Doublelist::insertnodeafter (Pdoublelistnode &head) after the first position in the bidirectional list;
  Inserts the element void Doublelist::insertnodebefore (Pdoublelistnode &head) in front of the first position in the bidirectional list; Delete the first element void doublelist in a two-way list::D Eletenode (Pdoublelistnode &head); };

3. Understanding of the insertion node of the linked list

For example, insert a new node before node I (that is, the insertnodebefore function in the previous code):

The list structure body is:
typedef struct DOUBLELISTNODE
{
  int data;       Data
  struct Doublelistnode *prior;//precursor
  struct doublelistnode *next;//successor
}doublelistnode, * Pdoublelistnode;

Suppose the list is composed of five nodes, a,b,c,d,e

  

The figure assumes that the address of the a,b,c,d,e is: Addressa,addressb,addressc,addressd,addresse.

The preceding example of a linked list is analyzed below:

Double-linked list of the forward, below this is the node "D" before inserting a new node "S" Code and analysis

s = (pdoublelistnode) malloc (sizeof (Doublelistnode)); //apply for a section of memory space, the pointer points to the first address for addresss
S->data = data; Assigning data to Node s
S->prior = p->prior; P points to the D node, P->prior represents ADDRESSC, assigns it to S->prior, then s->prior the value inside is ADDRESSC, pointing to ADDRESSC This address is node C, which is the blue line of the diagram s node below
S->next = p; p is ADDRESSD, assigns it to S->next,s->next the value in ADDRESSD, or s->next points to D, The red line of the diagram s node below
P->prior->next = s; P->prior is ADDRESSC, that is,node C, so P->prior->next is equivalent to the ADDRESSD before the S is inserted, and inserting s will The first address of S is Addresss assigned to this position, so at this point, the red line from C to D breaks, and the red line target becomes S, the red line of C node as shown below
P->prior = s; in thesame vein, P->prior also points to S, the p->prior addressc into Addresss, and the blue line of D pointing to C breaks. Into the following figure D node pointing to the blue line of S node.

The above C + + double linked list of basic operations (detailed) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.