Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1. #include <bits/stdc++.

h>
2.
3. using namespace std;
4.
5. struct node{
6. int data;
7. struct node* next;
8. };
9.
10. node* makeNode(int x){
11. //node *newNode = (node*)malloc(sizeof(node));
12. node *newNode = new node();
13. newNode->data = x;
14. newNode->next = NULL;
15. return newNode;
16. }
17.
18. int size(node *head){
19. int cnt = 0;
20. while(head != NULL){
21. ++cnt;
22. head = head->next;
23. }
24. return cnt;
25. }
26.
27. void duyet(node *head){
28. while(head != NULL){
29. cout << head->data << ' ';
30. head = head->next;
31. }
32. }
33.
34. void pushFront(node **head, int x){
35. node* newNode = makeNode(x);
36. newNode->next = (*head);
37. (*head) = newNode;
38. }
39.
40. void pushBack(node **head, int x){
41. node* newNode = makeNode(x);
42. if(*head == NULL){
43. *head = newNode; return;
44. }
45. node* tmp = *head;
46. while(tmp->next != NULL){
47. tmp = tmp->next;
48. }
49. tmp->next = newNode;
50. }
51.
52. void insert(node **head, int k, int x){
53. int n = size(*head);
54. if(k < 1 || k > n + 1) return;
55. if(k == 1){
56. pushFront(head, x); return;
57. }
58. node *temp = *head;
59. for(int i = 1; i <= k - 2; i++){
60. temp = temp->next;
61. }
62. //temp : k - 1
63. node *newNode = makeNode(x);
64. newNode->next = temp->next;
65. temp->next = newNode;
66. }
67.
68. void popFront(node **head){
69. if(*head == NULL) return;
70. node *temp = *head; // giai phong
71. *head = (*head)->next;
72. //free(temp)
73. delete temp;
74. }
75.
76. void popFront2(node *&head){
77. if(head == NULL) return;
78. node *temp = head;
79. head = head->next;
80. delete temp;
81. }
82.
83. void popBack(node **head){
84. if(*head == NULL) return;
85. node *temp = *head;
86. if(temp->next == NULL){
87. *head = NULL; delete temp;
88. return;
89. }
90. while(temp->next->next != NULL){
91. temp = temp->next;
92. }
93. node *last = temp->next; // node cuoi
94. temp->next = NULL;
95. delete last;
96. }
97.
98. void popBack2(node *&head){
99. if(head == NULL) return;
100. node *temp = head;
101. if(temp->next == NULL){
102. head = NULL; delete temp;
103. return;
104. }
105. while(temp->next->next != NULL){
106. temp = temp->next;
107. }
108. node *last = temp->next; // node cuoi
109. temp->next = NULL;
110. delete last;
111. }
112.
113. void erase(node **head, int k){
114. int n = size(*head);
115. if(k < 1 || k > n) return; // ko xoa
116. if(k == 1) popFront(head);
117. else{
118. node *temp = *head;
119. for(int i = 1; i <= k - 2; i++){
120. temp = temp->next;
121. }
122. //temp : k - 1
123. node *kth = temp->next; // node thu k
124. //cho k-1 => ket noi voi node thu k + 1
125. temp->next = kth->next;
126. delete kth; // free(kth)
127. }
128. }
129.
130. void erase1(node *&head, int k){
131. int n = size(head);
132. if(k < 1 || k > n) return; // ko xoa
133. if(k == 1) popFront(head);
134. else{
135. node *temp = head;
136. for(int i = 1; i <= k - 2; i++){
137. temp = temp->next;
138. }
139. //temp : k - 1
140. node *kth = temp->next; // node thu k
141. //cho k-1 => ket noi voi node thu k + 1
142. temp->next = kth->next;
143. delete kth; // free(kth)
144. }
145. }
146.
147.
148. int main(){
149. node *head = NULL;
150. while(1){
151. cout << "------------------------------------\n";
152. cout << "1. Them vao dau\n";
153. cout << "2. Them vao cuoi\n";
154. cout << "3. Them vao giua\n";
155. cout << "4. Xoa node khoi dau DSLK\n";
156. cout << "5. Xoa node khoi cuoi DSLK\n";
157. cout << "6. Xoa node o giua DSLK\n";
158. cout << "7. Duyet\n";
159. cout << "0. Thoat !\n";
160. cout << "------------------------------------\n";
161. cout << "Nhap lua chon :";
162. int lc; cin >> lc;
163. if(lc == 1){
164. int x; cout << "Nhap x :"; cin >> x;
165. pushFront(&head, x);
166. }
167. else if(lc == 2){
168. int x; cout << "Nhap x :"; cin >> x;
169. pushBack(&head, x);
170. }
171. else if(lc == 3){
172. int x; cout << "Nhap x :"; cin >> x;
173. int k; cout << "Nhap k :"; cin >> k;
174. insert(&head, k, x);
175. }
176. else if(lc == 4){
177. popFront2(head);
178. }
179. else if(lc == 5){
180. popBack2(head);
181. }
182. else if(lc == 6){
183. int k; cout << "Nhap vi tri can xoa :";
184. cin >> k;
185. erase(&head, k);
186. }
187. else if(lc == 7){
188. duyet(head);
189. cout << endl;
190. }
191. else{
192. break;
193. }
194. }
195. return 0;
196. }

You might also like