How to use C++ to achieve two way circular linked list

  • 2020-04-02 00:57:08
  • OfStack

Bidirectional circular linked list, that is, each node has a front and back two Pointers and the first and last linked list. The simple differences between the lists are as follows:
One-way linked list: Basic linked list;
One-way loop linked list: Unlike a one-way list, where the end of the list is determined by NULL, the end of the one-way loop list is linked to the table head.
Two-way linked list: There are more Pointers to the previous node than the one-way linked list, but in fact, the bidirectional linked list is rarely used without a loop.
Two-way circular linked list: As opposed to a one-way circular list, a bidirectional circular list iterates backwards from the head, which is efficient when the list is long and you need to get, insert, or delete elements near the end of the list. A one-way loop list can only iterate forward from the header, and the execution time is longer than that from the reverse.
Node. H


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;

LinkedList::LinkedList() {
    header = new Node(NULL, NULL, NULL);
    header->next = header;
    header->previous = header;
    size = 0;
}

void LinkedList::addFirst(int i) {
    header->next = new Node(i, header->next, header);
    header->next->next->previous = header->next;
    ++size;
}

void LinkedList::addLast(int i) {
    header->previous = new Node(i, header, header->previous);
    header->previous->previous->next = header->previous;
    ++size;
}

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;
}

int LinkedList::getFirst() {
    if(!size)
        cout << "Exception in getFirst(): List is empty." << 'n';
    return header->next->element;
}

int LinkedList::getLast() {
    if(!size)
        cout << "Exception in getLast(): List is empty." << 'n';
    return header->previous->element;
}

int LinkedList::removeFirst() {
    int remove = header->next->element;
    header->next->next->previous = header;
    header->next = header->next->next;
    --size;
    return remove;
}

int LinkedList::removeLast() {
    int remove = header->previous->element;
    header->previous->previous->next = header;
    header->previous = header->previous->previous;
    --size;
    return remove;
}

void LinkedList::iterate() {
    if(!size) {
        cout << "Exception in iterate(): List is empty." << 'n';
        return;
    }
    for(Node *entry = header->next; entry != header; entry = entry->next)
        cout << entry->element << " ";
    cout << 'n';
}

Related articles: