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

221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345

206: Write a program to create a class named "Circle" which has the property "radius". Define functions to calculate the area and circumference of the circle.

212: Build a class representing a university student with private data members for name, ID, and a static data member for the total number of students. Implement getter and setter functions for name and ID, and a static getter function to retrieve the total number of students. Use default constructors