Skip to content

Commit

Permalink
[6] Double Linked List (#6)
Browse files Browse the repository at this point in the history
* [5] LinkedList

* [6] Doubly Linked List
  • Loading branch information
sureshvazrala authored Aug 11, 2024
1 parent 71b5963 commit b2da234
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "test/Numbermanipulationtest.cpp"
#include "test/LinkedListtest.cpp"

#include "src/DATA_STRUCTURES/DoublyLinkedList.cpp"

#include "src/OOPS/class.cpp"

using namespace std;
Expand All @@ -15,5 +17,6 @@ int main()
test();
class_demo();
linkedlisttest();
doublyLinkedListtest();
return 0;
}
133 changes: 133 additions & 0 deletions src/DATA_STRUCTURES/DoublyLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <iostream>


struct node {
int32_t val;
node *prev;
node *next;
}*head;


class doublyLinkedList{
private:
//node *head;
public:
doublyLinkedList() {
head = nullptr;
}
void insert(int8_t n);
void remove(int8_t idx);
void show();
void reverseShow();

~doublyLinkedList() {
node* current = head;
while(current != nullptr)
{
node* next = current->next;
delete current;
current = next;
}
head = nullptr;
};

};
void doublyLinkedList::insert(int8_t n)
{
std::cout << "Inserting: " << (int)n << std::endl;
node *t = head;

node *new_node = new node();

if (head != nullptr)
{
while (t->next != nullptr)
{
t = t->next;
}

new_node->val = n;
t->next = new_node;
new_node->next = nullptr;
new_node->prev = t;
}
else{
new_node->val = n;
new_node->next = nullptr;
new_node->prev = nullptr;
head = new_node;
}
}

void doublyLinkedList::show()
{
std::cout<<"Display Double linkedlist: ";
node *temp = head;
while(temp != nullptr)
{
cout << temp->val << " ";
temp = temp->next;
}
std::cout << std::endl;
}

void doublyLinkedList::reverseShow()
{
node* curr = head;
node *temp = nullptr;

while(curr != nullptr)
{
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
curr = curr->prev;
}
if(temp != nullptr)
{
head = temp->prev;
}

}

void doublyLinkedList::remove(int8_t idx)
{
std::cout << "Removing: " << (int)idx << std::endl;
node* temp = head;

while (temp != nullptr)
{
//std::cout<<"temp: "<<temp->val<<std::endl;
if (temp->val == idx)
{
//std::cout<<"removing: "<<temp->val<<std::endl;
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
break;
}
temp = temp->next;
}
}

int doublyLinkedListtest()
{
doublyLinkedList l;
l.insert(1);
l.insert(2);
l.insert(3);
l.insert(4);
l.insert(5);
l.show();
l.reverseShow();
l.show();
l.remove(3);
l.show();
l.reverseShow();
l.show();
l.insert(3);
l.show();
l.reverseShow();
l.show();
return 0;
}

0 comments on commit b2da234

Please sign in to comment.