-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoraha_SinglyLinkedList.h
More file actions
75 lines (61 loc) · 1.69 KB
/
Loraha_SinglyLinkedList.h
File metadata and controls
75 lines (61 loc) · 1.69 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
#ifndef LORAHA_SINGLYLINKEDLIST_H
#define LORAHA_SINGLYLINKEDLIST_H
#include "Loraha_Node.h"
using namespace std;
template <typename T>
class SinglyLinkedList{
public:
Node<T>* head;
Node<T>* tail;
SinglyLinkedList(){
this->head = nullptr;
this->tail = nullptr;
}
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;
this->tail = temp;
} else {
this->tail->next = temp;
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;
}
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