-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.cpp
157 lines (146 loc) · 4.46 KB
/
linkedlist.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <unordered_map>
struct node{
int data;
node *next;
};
class linkedlist{
private:
node *head;
node *tail;
int count = 0;
public:
linkedlist(){ //default constructor
head = NULL;
tail = NULL;
std::cout << "Default Constructor called \n";
}
linkedlist(const linkedlist &o1); //copy constructor
const linkedlist& operator=(const linkedlist &o2); //assignment operator
~linkedlist(){
std::cout << "Destructor called \n";
}
void display(){
node *itr = new node;
itr = head;
while(itr != NULL){
std::cout << itr->data << "\n";
itr = itr->next;
}
std::cout << "There are " << count << " nodes in the list. \n";
}
void add_to_start(int n){ //adding a node to the beginning of the list
node *temp = new node;
temp->data = n;
temp->next = NULL;
if(head == NULL){ //empty list
head = temp;
tail = head;
count++;
}
else if(head == tail){ //one item
head = temp;
temp->next = tail;
count++;
}
else if(head != NULL && head != tail){ //multiple elements in list
temp->next = head; //this assigns tail's next pointer to what head is pointing to
head = temp; //this makes head pointer point to what temp is pointing to
count++;
}
}
void add_to_end(int n){
node *temp = new node;
temp->data = n;
temp->next = NULL;
if(head == NULL){ //empty list
head = temp;
tail = head;
count++;
}
else if(head == tail){ //only one element
head->next = temp; //head next now points to what temp points to
tail = temp; //tail pointer now points to what temp points to
count++;
}
else if(head != NULL && head != tail){ //multiple elements in list
tail->next = temp;
tail = temp;
count++;
}
}
void delete_head(){
node *temp = new node;
if(head == NULL) //empty list
std:: cout << "List is already empty.\n";
else if(head == tail){ //only one element
temp = head;
head = NULL;
tail = NULL;
delete temp;
count --;
}
else{ //list has multiple elements
temp = head; //temp points to what head is pointing to
head = head->next; //head is shifted to its next pointer
delete temp; //delete whatever temp is pointing to
count--;
}
}
void delete_tail(){
node *temp = new node;
if(head == NULL) //list is empty
std::cout << "List is empty.\n";
else if(head != NULL && head == tail){ //list has one node
temp = head; //temp points to what head points to
head = NULL;
tail = head;
delete temp; //delete whatever tail points to
count--;
}
else{ //multiple elements
temp = head;
while(((temp->next)->next)!=NULL){ //loop stops at 2nd to last node
temp = temp->next;
}
//std::cout << temp->data << "\n";
tail = temp; //tail is now pointing to second to last node
tail->next = NULL; //otherwise it may point to a garbage value
temp = temp->next; //set temp to last node
delete temp; //delete the last node
count--;
}
}
void reverse(){
node *itr = new node;
int reversecount = 1;
std::unordered_map<int, int> reversekey; //we store the position of each node with the value of each node
itr = head;
if(count==0){
std::cout << "Nothing to reverse, list empty.\n";
}
else if(count == 1){
std::cout << head->data << "\n";
}
else{
while(itr != NULL){
reversekey.insert(std::make_pair(reversecount,itr->data));
itr = itr->next;
reversecount++;
}
int i;
for(i = (reversecount-1); i >0; i--){
std::cout << reversekey.at(i) << "\n";
}
}
}
};
int main(){
linkedlist a;
a.add_to_start(3);
a.add_to_start(4);
a.add_to_start(5);
a.display();
a.reverse();
//a.display();
}