-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
81 lines (79 loc) · 1.67 KB
/
Copy pathLinkedList.cpp
File metadata and controls
81 lines (79 loc) · 1.67 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
#include "./LinkedList.h"
#include "./Node.h"
LinkedList::LinkedList(int valueP) {
Node *newNode = new Node(valueP);
head = newNode;
tail = newNode;
length = 1;
};
LinkedList::~LinkedList() {
Node *temp = head;
while (head) {
head = head->next;
delete temp;
temp = head;
}
};
void LinkedList::printList() {
Node *temp = head;
cout << "\n";
while (temp != nullptr) {
if (temp == head) {
cout << "[Head " << temp->value << "]->";
} else if (temp == this->tail) {
cout << "[" << temp->value << " Tail]";
} else {
cout << "[" << temp->value << "]-> ";
}
temp = temp->next;
}
}
void LinkedList::append(int value) {
Node *newNode = new Node(value);
if (this->length == 0) {
this->head = newNode;
this->tail = newNode;
} else {
this->tail->next = newNode;
this->tail = newNode;
}
++length;
this->printList();
cout << " ( appended ) ";
}
void LinkedList::getHead() { cout << "Head " << head->value << endl; }
void LinkedList::getTail() {
if (!this->tail) {
cout << " Tail is nullptr! " << endl;
} else {
cout << "Tail: " << tail->value << endl;
}
}
void LinkedList::getLength() {
if (length == 0) {
cout << "this linked list has no length" << endl;
}
cout << "Length: " << this->length << " --> ";
}
void LinkedList::deleteLast() {
if (length == 0)
return;
Node *temp = head;
if (length == 1) {
head = nullptr;
tail = nullptr;
} else {
Node *pre = head;
while (temp->next) {
pre = temp;
temp = temp->next;
}
tail = pre;
tail->next = nullptr;
}
delete temp;
length--;
cout << "\n";
this->printList();
cout << " ( delete ) \n";
}