list node

 avatar
unknown
plain_text
2 years ago
2.2 kB
5
Indexable
#include <bits/stdc++.h>
using namespace std;

template <typename T>
class ListNode
{
    T data;
    ListNode * next, *prev;
    public:
    ListNode(T d, ListNode<T> *n=nullptr, ListNode<T> *p=nullptr)
    {
        data = d;
        next = n;
        prev = p;
    }
     
    ListNode* getNext()
    {
        return next;
    }

    ListNode* getPrev()
    {
        return prev;
    }
    ListNode* getData()
    {
        return data;
    }

    void setNext(ListNode <T> *n)
    {
        if(this->next)
        {
            this->next->prev = n;
        }
        n->next = this->next;
        this->next = n;
        n->prev = this;
    }

    void setPrevious(ListNode <T> *p)
    {
        if(this->prev)
        {
            this->prev->setNext(p);
        }
        //else{} - za doma
        
    }

    void printNode()
    {
        cout<<"value: "<<this->data<<"\n"<<"prev: "<<this->prev<<"\n"<<"next: "<<this->next<<"\n";
    }

    //friend znaci deka ima dozovola da do privatnite delovi na klasata
    friend ostream& operator<<(ostream& o, const ListNode<T>& v) //istoto go pravi kako printNode, samo u main mozes da pises cout<<el
    {
        o<<"value: "<<n.data<<"\n"<<"prev: "<<n.prev<<"\n"<<"next: "<<n.next<<"\n";
        return o;
    }

};

// class SLL
// {
//     ListNode* firs;
//     public:
//     SLL(ListNode& r)
//     {
//         first = nullptr;
//     }

//     void add_back(ListNode *n)
//     {
//         ListNode * n1 = new ListNode(n);
//         ListNode* p = first;
//         while(!(p->isLast()))
//         {
//             p = (p-> getNext());
//         }
//         p -> setNext(n1);
//     }

//     void add_front(ListNode *n)
//     {
//         ListNode * n1 = new ListNode(n);
//         ListNode* p = root.getNext();
//         root.setNext(n);
//         n.setNext(*p);
//     }

//     ListNode* getPrevious(ListNode *c)
//     {
//         ListNode *p = this->first;
//         while(p->getNext()!=c)
//         {
//             p = p->getNext();
//         }
//         return p;
//     }
// };

int main()
{
   // SLL l;
    ListNode el(4), el1(5,&el,&el);
    el.printNode();
    return 0;
}
Editor is loading...
Leave a Comment