Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------DOUBLY-LINKED-LIST-C++-----------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------------------
- class Node
- {
- private:
- Node* next;
- Node* previous;
- int iVal;
- public:
- Node(int iElem) { iVal = iElem; }
- ~Node() { }
- Node* iGetNext() { return next; }
- Node* iGetPrevious() { return previous; }
- int iGetVal() { return iVal; }
- void vSetNext(Node* node) { next = node; }
- void vSetPrevious(Node* node) { previous = node; }
- void vSetIVal(int iElem) { iVal = iElem; }
- };
- class CLinkedList
- {
- public:
- CLinkedList();
- ~CLinkedList();
- void vAdd(int iELem);
- void vAdd(int iElem, int iIndex);
- void vAddFirst(int iElem);
- void vRemoveFirst();
- void vRemoveLast();
- void vRemove(int iIndex);
- int iGet(int iIndex);
- bool bContains(int iElem);
- void vToString();
- void vClear();
- int iSize();
- bool bIsEmpty();
- void vReplace(int iElem, int iIndex);
- private:
- Node* pcHead;
- Node* pcTail;
- Node* current;
- void printHelper(Node* node);
- //vector <Node*> vList;
- int iCounter;
- void vAddLikeFirst(int iELem);
- void vReplaceHelper(int iElem, int iIndex, Node* tmp);
- bool bContains(int iElem, Node* tmp);
- void vAddHelper(int iElem, int iIndex, Node* tmp);
- Node* pcGetHelper(int iIndex, Node* tmp);
- void vRemoveHelper(int iIndex, Node* tmp);
- };
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- CLinkedList::CLinkedList()
- {
- pcHead = NULL;
- pcTail = NULL;
- current = NULL;
- iCounter = 0;
- }
- CLinkedList :: ~CLinkedList()
- {
- Node* current = pcHead;
- Node* next;
- for (int i = 0; i < iCounter-1; i++)
- {
- next = current->iGetNext();
- cout << "Delete: " << current->iGetVal() << " ";
- delete current;
- current = next;
- }
- iCounter = 0;
- }
- void CLinkedList::vAdd(int iElem)
- {
- if (bIsEmpty())
- vAddLikeFirst(iElem);
- else
- {
- Node* nElem = new Node(iElem);
- nElem->vSetPrevious(current);
- current->vSetNext(nElem);
- nElem->vSetNext(pcHead);
- pcTail = nElem;
- pcHead->vSetPrevious(nElem);
- iCounter++;
- current = nElem;
- }
- }
- void CLinkedList::vAddLikeFirst(int iElem)
- {
- Node* nElem = new Node(iElem);
- pcHead = nElem;
- pcTail = nElem;
- current = nElem;
- nElem->vSetPrevious(nElem);
- nElem->vSetNext(nElem);
- iCounter++;
- }
- bool CLinkedList::bIsEmpty()
- {
- return iCounter == 0;
- }
- void CLinkedList :: vToString()
- {
- printHelper(pcHead);
- cout << endl;
- }
- void CLinkedList::printHelper(Node* node)
- {
- cout << "value: " << node->iGetVal() << " " << "previous: " << node->iGetPrevious()->iGetVal() << " next: "<< node->iGetNext()->iGetVal() << endl;
- if (node->iGetNext() != pcHead)
- printHelper(node->iGetNext());
- }
- void CLinkedList::vAdd(int iElem, int iIndex)
- {
- if (iIndex > 0 && iIndex < iCounter)
- {
- vAddHelper(iElem, iIndex, pcHead);
- }
- else
- vAddFirst(iElem);
- }
- void CLinkedList::vAddHelper(int iElem, int iIndex, Node* tmp)
- {
- if (iIndex != 0)
- vAddHelper(iElem, iIndex - 1, tmp->iGetNext());
- else
- {
- Node* nElem = new Node(iElem);
- nElem->vSetNext(tmp);
- nElem->vSetPrevious(tmp->iGetPrevious());
- tmp->iGetPrevious()->vSetNext(nElem);
- tmp->vSetPrevious(nElem);
- iCounter++;
- }
- }
- void CLinkedList::vAddFirst(int iElem)
- {
- if (bIsEmpty())
- vAddLikeFirst(iElem);
- else
- {
- Node* nElem = new Node(iElem);
- nElem->vSetPrevious(pcTail);
- nElem->vSetNext(pcHead);
- pcHead -> vSetPrevious(nElem);
- pcHead = nElem;
- pcTail->vSetNext(nElem);
- iCounter++;
- }
- }
- void CLinkedList::vRemoveFirst()
- {
- if (!bIsEmpty())
- {
- Node* nElem = pcHead->iGetNext();
- pcHead = nElem;
- pcTail->vSetNext(pcHead);
- pcHead->vSetPrevious(pcTail);
- iCounter--;
- }
- }
- void CLinkedList::vRemoveLast()
- {
- if (!bIsEmpty())
- {
- Node* nElem = pcTail->iGetPrevious();
- pcTail = nElem;
- pcHead->vSetPrevious(pcTail);
- pcTail->vSetNext(pcHead);
- iCounter--;
- }
- }
- void CLinkedList::vRemove(int iIndex)
- {
- if (iIndex == 0)
- vRemoveFirst();
- if (iIndex == iCounter - 1)
- vRemoveLast();
- if (!bIsEmpty() && iIndex > 0 && iIndex < iCounter)
- vRemoveHelper(iIndex, pcHead);
- else
- cout << "UNBOUND OPERATION!!!" << endl;
- }
- void CLinkedList::vRemoveHelper(int iIndex, Node* tmp)
- {
- if (iIndex != 0)
- vRemoveHelper(iIndex - 1, tmp->iGetNext());
- else
- {
- tmp->iGetPrevious()->vSetNext(tmp->iGetNext());
- tmp->iGetNext()->vSetPrevious(tmp->iGetPrevious());
- }
- }
- bool CLinkedList::bContains(int iElem)
- {
- return bContains(iElem, pcHead);
- }
- bool CLinkedList::bContains(int iElem, Node* tmp)
- {
- if (iElem == tmp->iGetVal())
- return true;
- else if (tmp->iGetNext() != pcHead)
- bContains(iElem, tmp->iGetNext());
- else
- return false;
- }
- void CLinkedList::vClear()
- {
- delete pcHead;
- delete pcTail;
- pcHead = nullptr;
- pcTail = nullptr;
- iCounter = 0;
- cout << "List is empty? " << bIsEmpty() << endl;
- }
- int CLinkedList::iSize()
- {
- return iCounter;
- }
- void CLinkedList::vReplace(int iElem, int iIndex)
- {
- vReplaceHelper(iElem, iIndex, pcHead);
- }
- void CLinkedList::vReplaceHelper(int iElem, int iIndex, Node* tmp)
- {
- if (iIndex != 0)
- vReplaceHelper(iElem, iIndex - 1, tmp->iGetNext());
- else
- tmp->vSetIVal(iElem);
- }
- int CLinkedList::iGet(int iIndex)
- {
- if (iIndex < iCounter && iIndex > 0)
- return pcGetHelper(iIndex, pcHead)->iGetVal();
- else
- return(-1);
- }
- Node* CLinkedList :: pcGetHelper(int iIndex, Node* tmp)
- {
- if (iIndex != 0)
- pcGetHelper(iIndex - 1, tmp->iGetNext());
- else
- return tmp;
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------TESTER----------------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------------------
- void vTest()
- {
- CLinkedList list;
- list.vAdd(10);
- list.vAdd(20);
- list.vAdd(30);
- list.vAdd(40);
- list.vAdd(50);
- list.vAdd(60);
- list.vAdd(99);
- list.vToString();
- list.vAdd(2555555, 2);
- list.vToString();
- list.vAddFirst(999999);
- list.vToString();
- list.vRemoveFirst();
- list.vToString();
- list.vRemoveLast();
- list.vToString();
- list.vRemove(2);
- list.vToString();
- cout << "Do you have 10? " << list.bContains(10) << endl;
- cout << "How many elems have u? " << list.iSize() << endl;
- list.vReplace(100000, 3);
- list.vToString();
- cout << "What is in third place? " << list.iGet(2) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement