Home > Net >  Doubly Linked List Class with Nodes C
Doubly Linked List Class with Nodes C

Time:12-10

I'm Trying to do a doubly linked list class that calls a Node class in C

The Code for the Node class works just fine and I have the code for my class that works only when trying to print the values from least to greatest but if I try to print from greatest to least it only prints some values.

I also need guide on how to make a remove function that works similar to insert

Code of Node class:

Node.h

class Node {
public:

    explicit Node(int data = 0, Node *nextPtr = nullptr, Node *beforePtr = nullptr);

    int getData() const;

    void setData(int data);

    Node *getNextPtr() const;

    void setNextPtr(Node *nextPtr);

    Node *getBeforePtr() const;

    void setBeforePtr(Node *beforePtr);

    void print() const;

private:
    int data;
    Node *nextPtr;
    Node *beforePtr;


};

Node.cpp

Node::Node(int data, Node *nextPtr, Node *beforePtr) : data(data), nextPtr(nextPtr), beforePtr(beforePtr) {}

int Node::getData() const {
    return data;
}

void Node::setData(int data) {
    Node::data = data;
}

Node *Node::getNextPtr() const {
    return nextPtr;
}

void Node::setNextPtr(Node *nextPtr) {
    Node::nextPtr = nextPtr;
}

Node *Node::getBeforePtr() const {
    return beforePtr;
}

void Node::setBeforePtr(Node *beforePtr) {
    Node::beforePtr = beforePtr;
}

void Node::print() const {

    cout << getData() << endl;

}
MyList.h

class MyList {

public:
    MyList(Node *currentPrt = nullptr);

    void insert(int value);

    void print() const;


private:
    Node *currentPrt;

};

MyList.cpp

MyList::MyList(Node *currentPrt) {}

void MyList::insert(int value) {

    if(currentPrt == nullptr){
        currentPrt = new Node;
        currentPrt->setData(value);
        currentPrt->setNextPtr(nullptr);
        currentPrt->setBeforePtr(nullptr);

    }
    else{
        if(value > currentPrt->getData()){
            while (currentPrt->getNextPtr() != nullptr && currentPrt->getNextPtr()->getData() < value){
                currentPrt = currentPrt->getNextPtr();
            }
            Node *newPtr = new Node(value);
            newPtr->setNextPtr(currentPrt->getNextPtr());
            currentPrt->setNextPtr(newPtr);
            newPtr->setBeforePtr(currentPrt);
        }
        else{
            while (currentPrt->getBeforePtr() != nullptr && currentPrt->getBeforePtr()->getData() > value){
                currentPrt = currentPrt->getBeforePtr();
            }
            Node *newPtr = new Node(value);
            if (currentPrt->getBeforePtr() != nullptr){
                currentPrt = currentPrt->getBeforePtr();
                newPtr->setNextPtr(currentPrt->getNextPtr());
                currentPrt->setNextPtr(newPtr);
                newPtr->setBeforePtr(currentPrt);
            }
            else{
                currentPrt->setBeforePtr(newPtr);
                newPtr->setNextPtr(currentPrt);
            }
        }
    }




}

void MyList::print() const {
    Node *ptr;
    ptr = currentPrt;
    while(ptr->getNextPtr() != nullptr){
        ptr = ptr->getNextPtr();
    }
    for (ptr; ptr != nullptr; ptr = ptr->getBeforePtr()){
        cout << ptr->getData() << endl;
    }

}

MyList test;

    test.insert(5);
    test.insert(3);

    test.insert(2);
    test.insert(1);
    test.insert(2);

    test.insert(7);
    test.insert(8);
    test.insert(6);
    test.print();

Output: 8 7 5 3 2 1

and when the print function is this:

void MyList::print() const {
    Node *ptr;
    ptr = currentPrt;
    while(ptr->getBeforePtr() != nullptr){
        ptr = ptr->getBeforePtr();
    }
    for (ptr; ptr != nullptr; ptr = ptr->getNextPtr()){
        cout << ptr->getData() << endl;
    }

}

Output is: 1 2 2 3 5 6 7 8 as expected

Thank you for all the help

CodePudding user response:

First of all, your code has undefined behaviour because of this constructor:

MyList::MyList(Node *currentPrt) {}

This leaves the currentPrt member uninitialised. It should be:

MyList::MyList(Node *currentPrt) : currentPrt(currentPrt) {}

The problem you have with the list order is caused by the insert method. There are two code blocks there where two next pointers are set, but only one before pointer, so in total three pointers are set. But there should be four of them in the general case.

This issue occurs in the second if block:

        Node *newPtr = new Node(value);
        newPtr->setNextPtr(currentPrt->getNextPtr());
        currentPrt->setNextPtr(newPtr);
        newPtr->setBeforePtr(currentPrt);

where newPtr->setNextPtr(currentPrt->getNextPtr()); is not mirrored by a link in the opposite direction. The correction is:

        Node *newPtr = new Node(value);
        newPtr->setNextPtr(currentPrt->getNextPtr());
        if (currentPrt->getNextPtr() != nullptr) // <---
            currentPrt->getNextPtr()->setBeforePtr(newPtr); // <---
        currentPrt->setNextPtr(newPtr);
        newPtr->setBeforePtr(currentPrt);

The same is going wrong in the deeper if block, where the if protection is not needed:

        if (currentPrt->getBeforePtr() != nullptr){
            currentPrt = currentPrt->getBeforePtr();
            newPtr->setNextPtr(currentPrt->getNextPtr());
            currentPrt->setNextPtr(newPtr);
            newPtr->setBeforePtr(currentPrt);
        }

The same statement is missing, and the correction can be:

        if (currentPrt->getBeforePtr() != nullptr){
            currentPrt = currentPrt->getBeforePtr();
            newPtr->setNextPtr(currentPrt->getNextPtr());
            currentPrt->getNextPtr()->setBeforePtr(newPtr); // <---
            currentPrt->setNextPtr(newPtr);
            newPtr->setBeforePtr(currentPrt);
        }
  • Related