Created
January 10, 2020 23:35
-
-
Save misterpoloy/fecbdee3a3d4957e0e95c19ef340fb0b to your computer and use it in GitHub Desktop.
(Naiv) Double linked list
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
// xhttps://www.youtube.com/watch?v=VOQNf1VxU3Q&list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P&index=14&t=0s | |
#include <iostream> | |
struct Node { | |
int value; | |
Node* prev; | |
Node* next; | |
Node(int x): prev(nullptr), next(nullptr) | |
{ | |
value = x; | |
} | |
}; | |
class DLinkList { | |
private: | |
Node* head; | |
Node* last; | |
public: | |
DLinkList() { | |
head = nullptr; | |
last = nullptr; | |
} | |
Node* getHead() { | |
return head; | |
} | |
void print() { | |
Node* temp = head; | |
while (temp != nullptr) { | |
std::cout << temp->value << std::endl; | |
temp = temp->next; | |
} | |
} | |
void addAtBegining(int value) { | |
Node* node = new Node(value); | |
if (head == nullptr) { | |
head = node; | |
return; | |
} | |
head->prev = node; | |
node->next = head; | |
head = node; | |
} | |
// iterative reverse | |
void reverseIterative() {} | |
// recursive print reverse | |
void recurisvePrintReverse(Node* currentNode) { | |
std::cout << currentNode->value << std::endl; | |
if (currentNode->next == nullptr) { | |
return; | |
} | |
recurisvePrintReverse(currentNode->next); | |
} | |
}; | |
int main() { | |
DLinkList list; | |
list.addAtBegining(1); | |
list.addAtBegining(2); | |
list.addAtBegining(3); | |
list.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment