Data structure and algorithm--linear table-type storage (bidirectional circular link list)

Source: Internet
Author: User

Today we summarize the two-way loop linked list in a linear table.

What is a two-way circular linked list?

look at the name, you know, Prime Minister. He is a circular list, that is, the last node of the pointer field is not empty, but point to the head node, followed by a one-way circular linked list, it is bidirectional. The so-called bidirectional is to add a pointer field to each node, and this pointer field points to the previous node.

That is the following (from Baidu Pictures):


Why use a two-way loop linked list?

Whether a single-linked list or a one-way circular list, there is only one pointer field, they are directly pointed to the successor node, if you want to find the current node of the successor node, it is very convenient. However, given a node, to get its forward node, it will be very troublesome, must start from the first element to traverse, and then determine when the pointer field of a node points to the current node, it is determined that the node is the current node of the forward node, the time complexity of O (n). And if you add a pointer field to each node and point to its forward node, then a node can be given a direct link to its forward node.

How to implement two-way circular linked list?

<span style= "Font-family:courier new;font-size:14px;"             > #include <iostream>using namespace std;template <class t>struct Node {T data; Data domain struct node<t> *pre; Point to the previous node struct node<t> *next; Point to the latter node};template <class t>//bidirectional circular linked list template class Doublelinklist {private:node<t> *front;//Head pointer public://NULL        The circular link list doublelinklist () {front = new node<t>;//head node Front->next = front;    Front->pre = Front; } ~doublelinklist (); destructor frees the node space doublelinklist (T a[],int N); Initialize the linked list void Insert (int i,t x) with array A; Insert meta poison to the specified position T Delete (int i);   Deletes the element at the specified position node<t> *getnode (int i);  Returns the node int getlength () at the specified position; Gets the length of the linked list void printlinklist ();    Traverse list};template <class t>//tail interpolation doublelinklist<t>::D oublelinklist (T a[],int n) {front = new node<t>; Front->next = Front->pre = Front; Initialize empty table for (int i=0;i<n;i++) {node<t> *s = new node<t>;        S->next = front->next;        Make the current node point to the head node S->data = A[i];        S->pre = Front;      The current node pre pointer points to the previous node Front->next = s;        The previous node points to the current node Front->pre = s;   Front = front->next; Pointer moves back}}template <class t>doublelinklist<t>::~doublelinklist () {node<t> *p = front->next;//Won   Take the head node while (p!=front->next) {node<t> *q = p;        Save the address you want to delete p=p->next;    Delete q;    }}template <class t>int doublelinklist<t>::getlength () {node<t> *p = front->next;    int count = 1;        while (P!=front->pre) {count++;    p = p->next; } return count;}  Template <class t>void doublelinklist<t>::P rintlinklist () {node<t> *p = front->next->next;        Get the first node for (int i=0;i<getlength (); i++) {cout<<p->data<< "";    p = p->next; } Cout<<endl;} Template <class t>node<t> * doublelinklIst<t>::getnode (int i) {node<t> * p = front->next;    for (int j=0;j<i;j++) {p = p->next; } return p;}    Template <class t>t doublelinklist<t>::D elete (int i) {node<t> *p = front; if (i!=1) p = getnode (i-1);  Gets the previous node of the node to be deleted if (p) {node<t> *q = p->next;         Save the node you want to delete T x = q->data; The element to be deleted p->next->next->pre = p;  The pre-pointer field of the successor node of the node to be deleted points to the previous node P->next = p->next->next;        The previous node next pointer field points to the element that is behind the deleted node delete q;    return x; } return 0;} /** insert operation idea: 1. Gets the previous node of the position to be inserted 2. Create a new node to save the inserted node element in its data field 3. Point the insertion point to the node that is behind the insert position, and its pre points to the previous node 4. Point the Pre-node    Insert node, point the front node to the insertion point technique: Here the possible nodes point to the order of the operations is easy to confuse, resulting in an error. My way is, as long as the current node first points to the successor node and the previous node, and then the former node and the successor node points to the current node in a word, remember to deal with the current node of the pointer field, the order is basically not wrong.    */template <class t>void doublelinklist<t>::insert (int i,t x) {node<t> *p = front;    if (i!=1) p = getnode (i-1); if (p) {node<t> *s = new node<t>;        S->data = x; S->next = p->next;       Point to the successor node S->pre = p;   The Pre pointer field refers to a forward node P->next->pre = s;       The successor node pre points to the node to be inserted p->next = s;    The previous node points to the node to be inserted}}int main () {int a[6] = {1,3,6,7,4,8};    Doublelinklist<int> list (a,6);    cout<< "Two-way circular linked list length:" <<endl; Cout<<list.    GetLength () <<endl;    cout<< "traverse the bidirectional circular link list" <<endl; List.    Printlinklist ();    cout<< "Delete element of position 3:" <<endl; cout<< "deleted elements are:" <<list.    Delete (3) <<endl; List.    Printlinklist ();    cout<< "inserting element 5 in position 3" <<endl; List.    Insert (3,5); List.    Printlinklist (); return 0;} </span>
Operation Result:



Data structure and algorithm--linear table-type storage (bidirectional circular link 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.