-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_List.cpp
79 lines (69 loc) · 1.95 KB
/
Linked_List.cpp
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
#include "Linked_List.h"
Linked_List::Linked_List(){
this->headNode = NULL;
this->endNode = NULL;
}
Linked_List::~Linked_List(){
Node* tempNode;
while(headNode!=NULL){
tempNode = headNode->next;
delete headNode;
headNode = tempNode;
}
}
void Linked_List::addNodeAtTheBack(std::string data, char player){
//create a new node
Node* currentNode = new Node();
this->listSize += 1;
currentNode->data = data;
currentNode->player = player;
currentNode->next = NULL;
//if the list is empty
if(this->headNode==NULL){
this->headNode = currentNode;
this->endNode = currentNode;
currentNode->prev = NULL;
}
else{
currentNode->prev = this->endNode;
this->endNode->next = currentNode;
this->endNode = currentNode;
}
}
void Linked_List::addNodeAtTheFront(std::string data, char player){
Node* currentNode = new Node();
this->listSize += 1;
currentNode->data = data;
currentNode->player = player;
currentNode->prev = NULL;
//if the list is empty
if(this->headNode==NULL){
this->headNode = currentNode;
this->endNode = currentNode;
currentNode->next = NULL;
}else{
this->headNode->prev = currentNode;
currentNode->next = this->headNode;
this->headNode = currentNode;
}
}
void Linked_List::printTheList(){
Node *currentNode = this->headNode;
while(currentNode!=NULL){
std::cout << "Player " << currentNode->player << " > " << currentNode->data << '\n';
currentNode = currentNode->next;
}
}
bool Linked_List::containsElement(std::string data, char player){
Node *currentNode = this->headNode;
while(currentNode!=NULL){
if(currentNode->data==data && currentNode->player==player){
return true;
}
currentNode = currentNode->next;
}
return false;
}
int Linked_List::size(){
return this->listSize;
}