How to realize bidirectional cyclic link list _c language with C + +

Source: Internet
Author: User
Tags int size
two-way circular chain list, that is, each node has a first and last two pointers and the linked list of the chain. The simple differences between the various lists are as follows:
one-way linked list:Basic linked list;
One-way circular chain list:Different from one-way linked list to NULL to determine the tail of the linked list, one-way circular chain list of the tail linked to the table, so when the iterative operation to the table head is the tail;
two-way linked list:A pointer to the previous node is more than a one-way list, but it is rarely used without loops when using a two-way linked list;
bidirectional circular chain list:In contrast to one-way cyclic lists, bidirectional cyclic lists can be reversed from the head, which is highly efficient when the chain list is large and needs to be fetched, inserted, or deleted near the tail element of the list. A one-way loop list can only be positive from the table header, and the execution time is greater than the reverse iteration.
Node.h
Copy Code code as follows:

/*
* Node type. Three members are: A pointer to the previous node, the element itself, and a pointer to the latter node.
*/
Class Node {
Public
int element;
Node *next;
Node *previous;
node (int element, node *next, node *previous) {
This->element = element;
This->next = Next;
This->previous = previous;
}
};
Linkedlist.h:
#include "node.h"
struct LinkedList {
LinkedList ();
void AddFirst (int);
void addlast (int);
void Add (int index, int element);
int GetFirst ();
int GetLast ();
int get (int);
int Removefirst ();
int Removelast ();
int remove (int);
void iterate ();
Private
Node *header;
int size;
};
Linkedlist.cpp:
#include "Linkedlist.h"
#include <iostream>
Using Std::cout;
/*
* Construction method.
* Generate an empty node between the table header and the footer, the initial and backward pointers are pointing to themselves.
*/
Linkedlist::linkedlist () {
Header = new Node (null, NULL, NULL);
Header->next = header;
Header->previous = header;
size = 0;
}
/*
* Add an element to the head of the list.
* A new node is generated, pointing forward to the empty node, pointing backwards to the next node of the original empty node, the original first node.
* The null node points back to this node, and the original first node points forward to this node.
*/
void Linkedlist::addfirst (int i) {
Header->next = new Node (i, Header->next, header);
Header->next->next->previous = header->next;
++size;
}
/*
* Add an element at the end of the list.
* Generates a new node, pointing forward to the previous node of the original empty node, the original last node, pointing back to the empty node.
* The original last node points back to this node, and the empty node points forward to this node.
*/
void Linkedlist::addlast (int i) {
header->previous = new Node (i, header, header->previous);
Header->previous->previous->next = header->previous;
++size;
}
/*
* Inserts an element before the specified index. 0 <= Index <= chain table length.
* If the index value is less than half the length of the list, backward (forward) iterations get the node of the index value position, or forward (reverse).
* Generate a new node that points forward to the previous node of the node in the original location and points back to the node in the original location.
* The previous node of the node in this position is pointing backwards to this node, and the node in the original position is pointing forward to this node.
* (similar to deleting an element implementation method at the specified index)
*/
void Linkedlist::add (int index, int i) {
if (Index > Size | | Index < 0) {
cout << "Exception in Add (): Index out of bound." << ' n ';
Return
}
Node *entry;
if (Index < SIZE/2) {
Entry = header->next;
for (int i = 0; i < index; ++i)
Entry = entry->next;
}
else {
Entry = header;
for (int i = size; i > index; i.)
Entry = entry->previous;
}
Entry->previous->next = new Node (i, entry, entry->previous);
Entry->previous = entry->previous->next;
++size;
}
/*
* Gets the first element of the list.
* The node to which the null node is pointing backwards is the first element.
*/
int Linkedlist::getfirst () {
if (!size)
cout << "Exception in GetFirst (): The List is empty." << ' \ n ';
Return header->next->element;
}
/*
* Get the last element of the list.
* The node to which the null node is pointing forward is the last element.
*/
int Linkedlist::getlast () {
if (!size)
cout << "Exception in GetLast (): The List is empty." << ' \ n ';
Return header->previous->element;
}
/*
* Deletes and returns the first element of the list.
* The second node of the list points forward to the empty node, and the null node points back to the second node.
*/
int Linkedlist::removefirst () {
int remove = header->next->element;
Header->next->next->previous = header;
Header->next = header->next->next;
--size;
return remove;
}
/*
* Deletes and returns the last element of the list.
* The penultimate node of the list points back to the null node, and the null node forwards to the penultimate node.
*/
int Linkedlist::removelast () {
int remove = header->previous->element;
Header->previous->previous->next = header;
Header->previous = header->previous->previous;
--size;
return remove;
}
/*
* An iterative method for outputting all elements.
*/
void Linkedlist::iterate () {
if (!size) {
cout << "Exception in Iterate (): The List is empty." << ' \ n ';
Return
}
for (Node *entry = header->next; entry!= header; entry = Entry->next)
cout << entry->element << "";
cout << ' \ n ';
}

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.