Created
March 3, 2024 19:55
-
-
Save mudassaralichouhan/d1de2f0f6bd231bf76779e229440bf91 to your computer and use it in GitHub Desktop.
Double Link List [$ g++ main.cc -o main.o && ./main.o]
This file contains 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 namespace std; | |
struct Node { | |
int id; | |
string name; | |
Node *previous; | |
Node *next; | |
Node(int id, string name, Node *previous = nullptr, Node *next = nullptr) : | |
id(id), name(name), previous(previous), next(next) {}; | |
}; | |
class DLL { | |
private: | |
Node *start = nullptr; | |
public: | |
DLL() { | |
start = nullptr; | |
} | |
void print() { | |
Node *tmp = start; | |
while (tmp != nullptr) { | |
cout << tmp->id << ' ' << tmp->name << endl; | |
tmp = tmp->next; | |
} | |
} | |
void insert(int id, string name) { | |
if (start == nullptr) { | |
start = new Node(id, name); | |
return; | |
} | |
Node *tmp = start; | |
while (tmp && tmp->next != nullptr) { | |
tmp = tmp->next; | |
} | |
tmp->next = new Node(id, name, tmp); | |
} | |
int insertAfter(int needle, int id, string name) { | |
if (start == nullptr) { | |
return 0; | |
} | |
Node *tmp = start; | |
while (tmp != nullptr) { | |
if (tmp->id == needle) { | |
break; | |
} | |
tmp = tmp->next; | |
} | |
if (tmp == nullptr) | |
return 0; | |
tmp->next = new Node(id, name, tmp->previous, tmp->next); | |
return 1; | |
} | |
int insertBefore(int needle, int id, string name) { | |
if (start == nullptr) { | |
return 0; | |
} | |
Node *tmp = start; | |
while (tmp != nullptr) { | |
if (tmp->id == needle) { | |
break; | |
} | |
tmp = tmp->next; | |
} | |
if (tmp == nullptr) | |
return 0; | |
if (tmp->previous == nullptr) { | |
insertStart(id, name); | |
return 1; | |
} | |
tmp->previous->next = new Node(id, name, tmp->previous, tmp); | |
return 1; | |
} | |
void insertStart(int id, string name) { | |
if (start == nullptr) { | |
start = new Node(id, name); | |
return; | |
} | |
Node *tmp = start; | |
start = new Node(id, name); | |
start->next = tmp; | |
tmp->previous = start; | |
} | |
int update(int needle, int id, string name) { | |
Node *tmp = start; | |
while (tmp) { | |
if (tmp->id == needle) { | |
tmp->id = id; | |
tmp->name = name; | |
return 1; | |
} | |
tmp = tmp->next; | |
} | |
return 0; | |
} | |
Node *search(int needle) { | |
Node *tmp = start; | |
while (tmp) { | |
if (tmp->id == needle) { | |
return tmp; | |
} | |
tmp = tmp->next; | |
} | |
return nullptr; | |
} | |
int deleteNode(int id) { | |
Node *found = search(id); | |
if (!found) | |
return 0; | |
if (found->previous == nullptr) { | |
start = found->next; | |
delete found; | |
return 1; | |
} | |
if (found->next == nullptr) { | |
Node *ext = found->previous->next; | |
found->previous->next = nullptr; | |
delete ext; | |
return 1; | |
} | |
found->previous->next = found->next; | |
found->next->previous = found->previous; | |
delete found; | |
return 1; | |
} | |
void sort() { | |
if (start == nullptr) | |
return; | |
bool swapped; | |
Node *tmp; | |
do { | |
swapped = false; | |
tmp = start; | |
while (tmp->next != nullptr) { | |
if (tmp->id > tmp->next->id) { | |
if (tmp->previous == nullptr) { | |
tmp->next->previous = tmp->previous; | |
start = tmp->next; | |
} else { | |
tmp->previous->next = tmp->next; | |
tmp->next->previous = tmp->previous; | |
} | |
if (tmp->next->next == nullptr) { | |
tmp->next->next = tmp; | |
tmp->previous = tmp->next; | |
tmp->next = nullptr; | |
} else { | |
Node *t = tmp->next->next; | |
t->previous = tmp; | |
tmp->previous = tmp->next; | |
tmp->next->next = tmp; | |
tmp->next->next->next = t; | |
} | |
swapped = true; | |
tmp = tmp->previous; | |
} else | |
tmp = tmp->next; | |
} | |
} while (swapped); | |
} | |
~DLL() { | |
Node *temp = start; | |
while (temp) { | |
Node *next = temp->next; | |
delete temp; | |
temp = next; | |
} | |
} | |
}; | |
int main() { | |
DLL dll; | |
dll.insert(9, "nine"); | |
dll.insert(1, "one"); | |
dll.insert(0, "zero"); | |
dll.insert(2, "two"); | |
dll.insert(3, "three"); | |
dll.insert(4, "four"); | |
dll.insert(5, "five"); | |
dll.insert(6, "six"); | |
dll.insert(7, "seven"); | |
dll.insert(8, "eight"); | |
dll.insertStart(0, "insertStart"); | |
dll.insertBefore(0, 56, "insertBefore"); | |
dll.insertAfter(0, 55, "insertAfter"); | |
dll.update(55, 99, "negative"); | |
dll.update(56, 6, "five--"); | |
dll.deleteNode(5); | |
dll.deleteNode(-1); | |
dll.deleteNode(4); | |
dll.print(); | |
dll.sort(); | |
cout << endl << "After Sort: " << endl; | |
dll.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ g++ main.cc -o main.o && ./main.o
6 five--
0 insertStart
99 negative
9 nine
1 one
0 zero
2 two
3 three
6 six
7 seven
8 eight
After Sort:
0 insertStart
0 zero
1 one
2 two
3 three
6 five--
6 six
7 seven
8 eight
9 nine
99 negative