From 22efa0a3ab3ff2e393aa2788bbad3089651cb3e4 Mon Sep 17 00:00:00 2001 From: megamayoy Date: Thu, 4 Oct 2018 15:05:21 +0200 Subject: [PATCH] Add DoublyinkedList implementation --- .../DoublyLinkedList/doubly_linked_list.cpp | 272 ++++++++++++++++++ .../DoublyLinkedList/doubly_linked_list.h | 68 +++++ 2 files changed, 340 insertions(+) create mode 100644 data-structures/DoublyLinkedList/doubly_linked_list.cpp create mode 100644 data-structures/DoublyLinkedList/doubly_linked_list.h diff --git a/data-structures/DoublyLinkedList/doubly_linked_list.cpp b/data-structures/DoublyLinkedList/doubly_linked_list.cpp new file mode 100644 index 00000000..8402dce5 --- /dev/null +++ b/data-structures/DoublyLinkedList/doubly_linked_list.cpp @@ -0,0 +1,272 @@ +#include"doubly_linked_list.h" + +template +void DoublyLinkedList :: Push_back(T val) +{ + + Node* new_node = new Node(val); + + if(Tail != NULL) //or if(Size != 0) + { + Tail->next = new_node; + new_node->prev = Tail; + Tail = new_node; + + } + else + { + Tail = new_node; + Head = new_node; + } + + Size_++; +} + + +template +void DoublyLinkedList :: Pop_back() +{ + //if list is empty + if(Tail == NULL) //or Head == NULL + { + cout<<"Can't pop back the DLS is empty"<<'\n'; + return; + } + + if(Tail == Head) // if there's only one element in the DLS + { + delete Tail; + Tail = NULL; + Head = NULL; + + } + else + { + Node* previous_node = Tail->prev; + + delete Tail; + + Tail = previous_node; + Tail->next = NULL; + } + + Size_--; + +} + +template +void DoublyLinkedList :: Push_front(T val) +{ + + Node* new_node = new Node(val); + + new_node->next = Head; + if(Head != NULL) + { + Head->prev = new_node; + + } + Head = new_node; + if(Tail == NULL) + { + Tail = Head; + } + + Size_++; + +} + +template +void DoublyLinkedList :: Pop_front() +{ + if(Head == NULL) //if dls is empty can't pop + { + cout<<"Can't pop front the DLS is empty"<<'\n'; + return; + } + + Node* next_node = Head->next; + delete Head; + Head = next_node; + + if(Head == NULL) //if we popped the last element + { + Tail = NULL; + } + else + { + Head->prev = NULL; + } + +Size_--; + +} + +template +void DoublyLinkedList :: Add_before(Node* node, T val) +{ + + Node* new_node = new Node(val); + new_node->next = node; + new_node->prev = node->prev; + node->prev = new_node; + + if(new_node->prev != NULL) + { + new_node->prev->next = new_node; + } + + if(Head == node) + { + Head = new_node; + + } + Size_++; +} + + + +template +void DoublyLinkedList :: Add_after(Node* node,T val) +{ + + Node* new_node = new Node(val); + new_node->prev = node; + new_node->next = node->next; + node->next = new_node; + + + if(new_node->next != NULL) + { + new_node->next->prev = new_node; + + } + + if(Tail == node) + { + Tail = new_node; + } + +Size_++; + +} + +template +void DoublyLinkedList :: Display() +{ + + if(Size_ == 0) + { + cout<<"Linked List is empty"; + } + else + { + for(Node* tmp_ptr = Head;tmp_ptr!= NULL; tmp_ptr= tmp_ptr->next) + { + cout<data<<" "; + + } + } +cout<<'\n'; +} + + + +template +void DoublyLinkedList :: Clear() +{ + + Node* tmp = Head; + if(Size_ == 0 ) + { + cout<<" all cleared linked list is empty"<<'\n'; + return; + } + + while(Head != NULL) + { + + Head = Head->next; + delete tmp; + tmp = Head; + + } + cout<<" all cleared linked list is empty"<<'\n'; +Tail = NULL; +Size_ = 0; +} +template +void DoublyLinkedList :: Insert_at(T val ,int position) +{ + if(position >Size_ || position <= 0) + { + cout<<"invalid position choose a position between 1 and size "<<'\n'; + cout<<"size is: "<* tmp = Head; + //get a pointer of that position that position + for(int i =1 ; i<=position-1 ; i++,tmp = tmp->next); + Add_before(tmp,val); + } + + + } + +template +void DoublyLinkedList :: Delete_at(int position) +{ + + + if(Size_ ==0) + { + cout<<"Can't delete DLS is empty "<<'\n'; + return; + } + + if(position >Size_ || position < 0) + { + cout<<"invalid position choose a position between 1 and size "<<'\n'; + cout<<"size is: "<* tmp = Head; + + for(int i = 1; i <= position-1; i++,tmp = tmp->next); + + if(tmp->next != NULL) + { + tmp->next->prev = tmp->prev; + } + + if(tmp->prev != NULL) + { + tmp->prev->next = tmp->next; + + } + + if(Head == tmp) + { + Head = tmp->next; + + } + + if(Tail == tmp) + { + Tail = Tail->prev; + } + +delete tmp; +Size_--; + + +} + + + diff --git a/data-structures/DoublyLinkedList/doubly_linked_list.h b/data-structures/DoublyLinkedList/doubly_linked_list.h new file mode 100644 index 00000000..4acf21fb --- /dev/null +++ b/data-structures/DoublyLinkedList/doubly_linked_list.h @@ -0,0 +1,68 @@ +#include + +using namespace std; + +template + +class Node{ + + + //each node has a next pointer and a previous pinter + public: + T data; + Node* next; + Node* prev; + + + Node() + { + next = NULL; + prev = NULL; + + } + + Node( T value) + { + data = value; + next = NULL; + prev = NULL; + } + +}; + + +template + +class DoublyLinkedList{ + + public: + Node* Head; + Node* Tail; + int Size_; + + DoublyLinkedList() + { + Head = NULL; + Tail = NULL; + Size_ = 0; + + } + + void Push_back(T val); //append + void Pop_back(); + void Push_front(T val); //prepend + void Pop_front(); + void Display(); + void Clear(); + void Insert_at(T val,int position); + void Delete_at(int position); + void Add_before(Node* node, T val); + void Add_after(Node* node,T val); + + ~DoublyLinkedList() + { + cout<<"destructor is called"<<'\n'; + Clear(); + } + +};