-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabCycle1-4.cpp
127 lines (118 loc) · 3.12 KB
/
LabCycle1-4.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
#include "LabCycle1-4.h"
template<typename T>
LinkedList<T>::LinkedList() : head(nullptr) {}
template<typename T>
LinkedList<T>::~LinkedList() {
Node<T>* current = head;
while (current != nullptr) {
Node<T>* next = current->next;
delete current;
current = next;
}
head = nullptr;
}
template<typename T>
void LinkedList<T>::insertAtBeginning(const T& p) {
Node<T>* newNode = new Node<T>(p);
newNode->next = head;
head = newNode;
}
template<typename T>
void LinkedList<T>::insertAtEnd(const T& p) {
Node<T>* newNode = new Node<T>(p);
if (head == nullptr) {
head = newNode;
return;
}
Node<T>* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
template<typename T>
void LinkedList<T>::insertAtPosition(int pos, const T& p) {
if (pos < 0) {
cout << "Invalid position. Insertion failed." << endl;
return;
}
if (pos == 0) {
insertAtBeginning(p);
return;
}
Node<T>* newNode = new Node<T>(p);
Node<T>* current = head;
for (int i = 0; i < pos - 1 && current != nullptr; ++i) {
current = current->next;
}
if (current == nullptr) {
cout << "Invalid position. Insertion failed." << endl;
return;
}
newNode->next = current->next;
current->next = newNode;
}
template<typename T>
void LinkedList<T>::deleteFromBeginning() {
if (head == nullptr) {
cout << "List is empty. Cannot delete from beginning." << endl;
return;
}
Node<T>* temp = head;
head = head->next;
delete temp;
}
template<typename T>
void LinkedList<T>::deleteFromEnd() {
if (head == nullptr) {
cout << "List is empty. Cannot delete from end." << endl;
return;
}
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
Node<T>* current = head;
while (current->next->next != nullptr) {
current = current->next;
}
delete current->next;
current->next = nullptr;
}
template<typename T>
void LinkedList<T>::deleteFromPosition(int pos) {
if (head == nullptr || pos < 0) {
cout << "Invalid position. Deletion failed." << endl;
return;
}
if (pos == 0) {
deleteFromBeginning();
return;
}
Node<T>* current = head;
for (int i = 0; i < pos - 1 && current->next != nullptr; ++i) {
current = current->next;
}
if (current->next == nullptr) {
cout << "Invalid position. Deletion failed." << endl;
return;
}
Node<T>* temp = current->next;
current->next = current->next->next;
delete temp;
}
template<typename T>
void LinkedList<T>::display() {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
Node<T>* current = head;
cout << "List elements: ";
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}