225: Test post


#include <iostream>
using namespace std;
class Node
{
private:
    int value;
    Node *nextNode;
    Node *prevNode;

public:
    Node() { value = 0, nextNode = nullptr, prevNode = nullptr; }
    Node(int value) { this->value = value, nextNode = nullptr, prevNode = nullptr; }
    void setValue(int value) { this->value = value; }
    void setNextNode(Node *nextNode) { this->nextNode = nextNode; }
    void setPrevNode(Node *prevNode) { this->prevNode = prevNode; }
    int getValue() { return value; }
    Node *getNextNode() { return nextNode; }
    Node *getPrevNode() { return prevNode; }
};

class DoublyLinkedList
{
private:
    int size;
    Node *headNode;
    Node *currentNode;

public:
    DoublyLinkedList() { size = 0, headNode = nullptr, currentNode = nullptr; }

    void printLinkedList();

    void insertAtHead(int value);
    void insertAtTail(int value);
    void insertAfter(int after, int value);
    void insertTwoAfter(int after, int value1, int value2);
    void insertBefore(int before, int value);
    void insertTwoBefore(int before, int value1, int value2);

    void remove(int value);
};

void DoublyLinkedList::printLinkedList()
{
    if (headNode == nullptr)
    {
        cout << "List is empty" << endl;
        return;
    }

    currentNode = headNode;
    while (currentNode != nullptr)
    {
        if (currentNode->getNextNode() != nullptr)
        {
            cout << currentNode->getValue() << " -> ";
        }
        else
        {
            cout << currentNode->getValue() << endl;
        }

        currentNode = currentNode->getNextNode();
    }

    currentNode = currentNode->getPrevNode();
    cout << "hello" << endl;
    while (currentNode != nullptr)
    {
        if (currentNode->getPrevNode() != nullptr)
        {
            cout << currentNode->getValue() << " <- ";
        }
        else
        {
            cout << currentNode->getValue() << endl;
        }

        currentNode = currentNode->getPrevNode();
    }
}

void DoublyLinkedList::insertAtHead(int value)
{
    Node *newNode = new Node(value);
    if (headNode == nullptr)
    {
        headNode = newNode;
    }
    else
    {
        newNode->setNextNode(headNode);
        headNode->setPrevNode(newNode);
        headNode = newNode;
    }
    size++;
    cout << value << " successfully added at head" << endl;
}

void DoublyLinkedList::insertAtTail(int value)
{
}
void DoublyLinkedList::insertAfter(int after, int value) {}
void DoublyLinkedList::insertTwoAfter(int after, int value1, int value2) {}
void DoublyLinkedList::insertBefore(int before, int value) {}
void DoublyLinkedList::insertTwoBefore(int before, int value1, int value2) {}

void DoublyLinkedList::remove(int value) {}

int main()
{
    DoublyLinkedList List;
    List.insertAtHead(5);
    List.insertAtHead(4);
    List.insertAtHead(3);
    List.insertAtHead(2);
    List.insertAtHead(1);

    List.printLinkedList();

    return 0;
}

 

Comments

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_