Created
December 16, 2017 07:32
-
-
Save sisingh/9b68efcdcac0124b375ae0ac52a50ada to your computer and use it in GitHub Desktop.
Singly Linked List as Doubly to move in both the directions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::cout; | |
using std::endl; | |
template < typename TYPE > | |
class NODE { | |
public: | |
NODE() { | |
next = NULL; | |
} | |
NODE(const TYPE data) : data(data), next(NULL) { } | |
TYPE getData() { | |
return data; | |
} | |
NODE * getNext() { | |
return next; | |
} | |
void setNext(long next) { | |
this->next = reinterpret_cast<NODE *>(next); | |
} | |
private: | |
TYPE data; | |
NODE *next; | |
}; | |
template <typename TYPE> | |
class LIST { | |
public: | |
LIST() { | |
XORdata = head = tail = NULL; | |
} | |
void add(TYPE data); | |
void traverseForward(); | |
void traverseBackward(); | |
private: | |
NODE<TYPE> *head; | |
NODE<TYPE> *tail; | |
NODE<TYPE> *XORdata; | |
}; | |
template <typename TYPE> | |
inline | |
void LIST<TYPE>::traverseBackward() { | |
NODE<TYPE> *tempTail = tail; | |
NODE<TYPE> *tempTail1 = tail; | |
NODE<TYPE> *tempXOR = XORdata; | |
while(tempTail) { | |
cout << "Data : " << tempTail->getData() << endl; | |
long p1 = reinterpret_cast<TYPE> (tempTail->getNext()); | |
long p2 = reinterpret_cast<TYPE> (tempXOR); | |
tempTail = reinterpret_cast<NODE <TYPE> * >(p1^p2); | |
tempXOR = tempTail1; | |
tempTail1 = tempTail; | |
} | |
} | |
template <typename TYPE> | |
inline | |
void LIST<TYPE>::traverseForward() { | |
NODE<TYPE> *tempHead = head; | |
NODE<TYPE> *tempHead1 = head; | |
NODE<TYPE> *tempXOR = NULL; | |
while(tempHead) { | |
cout << "Data : " << tempHead->getData() << endl; | |
if (!tempHead->getNext()) break; | |
long p1 = reinterpret_cast<TYPE> (tempHead->getNext()); | |
long p2 = reinterpret_cast<TYPE> (tempXOR); | |
tempHead = reinterpret_cast< NODE<TYPE> * >(p1^p2); | |
tempXOR = tempHead1; | |
tempHead1 = tempHead; | |
} | |
} | |
template <typename TYPE> | |
inline | |
void LIST<TYPE>::add(TYPE data) { | |
if(NULL == head) { | |
tail = head = new NODE<TYPE>(data); | |
} else { | |
NODE<TYPE> *temp = new NODE<TYPE>(data); | |
long p1 = reinterpret_cast<TYPE>(XORdata); | |
long p2 = reinterpret_cast<TYPE>(temp); | |
tail->setNext(p1^p2); | |
XORdata = tail; | |
tail = temp; | |
} | |
} | |
int main(int argc, char **argv, char **arge) try { | |
LIST<long> list; | |
for(int i = 1; i < 16; ++i ) { | |
list.add (i); | |
} | |
cout << "Forward Traversal " << endl; | |
list.traverseForward(); | |
cout << "Now moving backward !!!" << endl; | |
list.traverseBackward(); | |
return 0; | |
} catch(...) { | |
cout << "Uncaught exception!" << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment