-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoraha_LinkedList.h
More file actions
109 lines (93 loc) · 2.76 KB
/
Loraha_LinkedList.h
File metadata and controls
109 lines (93 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef LORAHA_LINKEDLIST_H
#define LORAHA_LINKEDLIST_H
#include "Loraha_Node.h"
using namespace std;
template <typename T>
class LinkedList{
public:
Node<T>* head;
Node<T>* tail;
LinkedList(){
this->head = nullptr;
this->tail = nullptr;
}
void addNodeTop(T data){
Node<T>* temp = new Node<T>(data);
if(isEmpty()){
this->head = temp;
this->tail = temp;
} else if(this->head == this->tail){
this->tail->previous = temp;
temp->next = this->tail;
this->head = temp;
} else {
this->head->previous = temp;
temp->next = this->head;
this->head = temp;
}
}
void addNodeBottom(T data){
Node<T>* temp = new Node<T>(data);
if(isEmpty()){
this->head = temp;
this->tail = temp;
} else if(this->head == this->tail){
this->head->next = temp;
temp->previous = this->head;
this->tail = temp;
} else {
this->tail->next = temp;
temp->previous = this->tail;
this->tail = temp;
}
}
void removeNodeTop(Node<T>* node){
if(isEmpty()){
std::cout << "List is empty\n";
return;
} else if (this->head == this->tail){
this->head = nullptr;
this->tail = nullptr;
} else {
node = this->head;
this->head = this->head->next;
this->head->previous = nullptr;
}
delete node;
}
void removeNodeBottom(Node<T>* node){
if(isEmpty()){
printf("List is empty\n");
return;
} else if (this->head == this->tail){
this->head = nullptr;
this->tail = nullptr;
} else {
node = this->tail;
this->tail = this->tail->previous;
this->tail->next = nullptr;
}
delete node;
}
bool isEmpty(){
return (this->head == nullptr);
}
void printList(){
Node<T>* temp = this->head;
while(temp != nullptr){
cout << temp->data << " ";
temp = temp->next;
}
printf("\n");
}
T getData(Node<T>* node){
return node->data;
}
Node<T>* getHead(){
return this->head;
}
Node<T>* getTail(){
return this->tail;
}
};
#endif