-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.cpp
More file actions
186 lines (158 loc) · 4.38 KB
/
Copy path7.cpp
File metadata and controls
186 lines (158 loc) · 4.38 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
class Node {
public:
int Data;
Node *Next;
Node *Back;
public:
Node(int data = 0) : Data(data), Next(NULL), Back(NULL) {}
};
class DoubleLinkedList : public Node { // ใช้ public inheritance
private:
Node *head;
Node *tail; // เพิ่ม tail เพื่อทำให้สามารถเข้าถึงท้ายได้ง่าย
public:
DoubleLinkedList() : head(NULL), tail(NULL) {}
void AddNode(int data);
void InsertNode(int data, int pos);
void DeleteNode(int pos);
void ShowFront();
void ShowBack();
};
int main() {
DoubleLinkedList A, B;
int data, pos;
// กรอกข้อมูลและดำเนินการกับ DoubleLinkedList A
printf("Add (data): ");
scanf("%d", &data);
A.AddNode(data);
printf("Add (data): ");
scanf("%d", &data);
A.AddNode(data);
A.ShowFront();
// การแทรกข้อมูล
printf("Insert (data pos): ");
scanf("%d %d", &data, &pos);
A.InsertNode(data, pos);
A.ShowFront();
// การลบข้อมูล
printf("Delete (pos): ");
scanf("%d", &pos);
A.DeleteNode(pos);
A.ShowFront();
printf("---\n");
// กรอกข้อมูลและดำเนินการกับ DoubleLinkedList B
printf("Add (data): ");
scanf("%d", &data);
B.AddNode(data);
printf("Add (data): ");
scanf("%d", &data);
B.AddNode(data);
printf("Add (data): ");
scanf("%d", &data);
B.AddNode(data);
B.ShowBack();
return 0;
}
void DoubleLinkedList::ShowFront() {
Node* current = head;
printf("Show Front : ");
while (current != NULL) {
printf("%d ", current->Data);
current = current->Next;
}
printf("\n");
}
void DoubleLinkedList::ShowBack() {
Node* current = tail;
printf("Show Back : ");
while (current != NULL) {
printf("%d ", current->Data);
current = current->Back;
}
printf("\n");
}
void DoubleLinkedList::DeleteNode(int pos) {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node* current = head;
int index = 1;
// หา node ที่ต้องการลบ
while (current != NULL && index < pos) {
current = current->Next;
index++;
}
// ถ้าตำแหน่งไม่ถูกต้อง
if (current == NULL) {
printf("Invalid position\n");
return;
}
// ถ้าคือ node แรก
if (current == head) {
head = current->Next;
if (head != NULL) {
head->Back = NULL;
}
}
// ถ้าคือ node สุดท้าย
else if (current == tail) {
tail = current->Back;
if (tail != NULL) {
tail->Next = NULL;
}
}
// ลบ node ปกติ
else {
current->Back->Next = current->Next;
if (current->Next != NULL) {
current->Next->Back = current->Back;
}
}
delete current;
}
void DoubleLinkedList::InsertNode(int data, int pos) {
Node* newNode = new Node(data);
if (head == NULL || pos == 1) {
newNode->Next = head;
if (head != NULL) {
head->Back = newNode;
}
head = newNode;
if (tail == NULL) {
tail = newNode;
}
return;
}
Node* current = head;
int index = 1;
while (current != NULL && index < pos - 1) {
current = current->Next;
index++;
}
if (current == NULL) {
printf("Invalid position\n");
return;
}
newNode->Next = current->Next;
if (current->Next != NULL) {
current->Next->Back = newNode;
}
current->Next = newNode;
newNode->Back = current;
// ถ้าแทรกที่ท้าย
if (newNode->Next == NULL) {
tail = newNode;
}
}
void DoubleLinkedList::AddNode(int data) {
Node* newNode = new Node(data);
if (head == NULL) {
head = tail = newNode;
} else {
tail->Next = newNode;
newNode->Back = tail;
tail = newNode;
}
}