-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
106 lines (93 loc) · 1.9 KB
/
linked_list.cpp
File metadata and controls
106 lines (93 loc) · 1.9 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
#include "linked_list.h"
#include <iostream>
#include <cstdlib>
using namespace std;
/**
* Linked List Constructor
*/
List::List(string name){
head = NULL;
tail = NULL;
owner = name;
}
/**
* Insert new Node at the front (head) of the linked list
* previous head is now at second of the linked list
*/
void List::insertFront(string newData){
Node * node = new Node;
node->data = newData;
node->next = head;
head = node;
if (node->next == NULL){
tail = node;
}
length++;
}
void List::insertBack(string newData){
Node * node = new Node;
node->data = newData;
if (tail != NULL){
tail->next = node;
tail = node;
}
else tail = node;
length++;
}
void List::removeHead(){
Node * temp = head;
head = temp->next;
temp->next = NULL;
delete temp;
length--;
}
// prints all values in linked list starting from head
void List::print(){
Node * curr = head;
while (curr != NULL){
cout << curr->data << " " << endl;
curr = curr->next;
}
}
void List::changeData(string oldData, string newData){
Node * curr = head;
while (curr != NULL){
if (curr->data == oldData){
curr->data = newData;
return;
}
curr = curr->next;
}
cout << oldData << " is not in your team!" << endl;
}
void List::removeAtIndex(int index) {
Node * curr = head;
Node * pre;
for (int i = 0; i < index; i++){
pre = curr;
curr = curr->next;
}
pre->next = curr->next;
if (tail == curr){
tail = pre;
}
delete curr;
curr->next = NULL;
length--;
}
void List::incIndex() {
index++;
}
string List::getDataAtIndex() {
Node * curr = head;
for (int i = 0; i < index; i++){
curr = curr->next;
}
return curr->data;
}
int List::getIndex() {
return index;
}
int List::getLength() {
return length;
}