Guest User

Untitled

a guest
Feb 3rd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream doc("input.txt");
  6.  
  7. struct Node{
  8. int number;
  9. Node* next;
  10. };
  11.  
  12. struct List{
  13. Node* head;
  14. Node* tail;
  15. };
  16.  
  17. void createEmptylist(List &list){
  18. list.head = list.tail = NULL;
  19. }
  20.  
  21. Node *createNode(int x){
  22. Node* newNode = new Node;
  23. if (newNode != NULL){
  24. newNode->number = x;
  25. newNode->next = NULL;
  26. }
  27. return newNode;
  28. }
  29.  
  30. void ATH(List &list, int x){
  31. Node *p = createNode(x);
  32. if (list.head == NULL){
  33. list.head = list.tail=p;
  34. }
  35. else{
  36. p->next = list.head;
  37. list.head = p;
  38. }
  39. }
  40.  
  41. void ATT(List &list, int x){
  42. Node *p = createNode(x);
  43. if (list.tail == NULL){
  44. list.tail = list.head= p;
  45. }
  46. else{
  47. list.tail->next = p;
  48. list.tail = p;
  49. }
  50. }
  51.  
  52. int count(List &list){
  53. int i = 0;
  54. for (Node *p = list.head; p; p = p->next){
  55. i++;
  56. }
  57. return i;
  58. }
  59.  
  60. void DT(List &list){
  61. Node *p = list.head;
  62. if (p == NULL) return;
  63. else{
  64. while (p->next != list.tail){
  65. p = p->next;
  66. }
  67. p->next = NULL;
  68. delete(list.tail);
  69. list.tail = p;
  70. }
  71. }
  72.  
  73. int search(List &list, int x){
  74. int i = 0;
  75. for (Node *p = list.head; p; p = p->next){
  76. i++;
  77. if (x == p->number){
  78. cout << "Da tim thay tai Node " << i << endl;
  79. }
  80. }
  81. return i;
  82. }
  83.  
  84. void DH(List &list){
  85. Node *p = list.head;
  86. if (p == NULL) return;
  87. else{
  88. if (count(list) == 1)
  89. {
  90. cout << "Ko the xoa duoc";
  91. }
  92. else{
  93. Node *tam = p;
  94. list.head = p->next;
  95. delete(tam);
  96. }
  97. }
  98. }
  99.  
  100. int SAD(List &list, int x){
  101. Node *p = list.head;
  102. int k = -1;
  103. while (p!=NULL && p->number != x)
  104. p = p->next;
  105. if (p->number == x && p == list.head){
  106. DH(list);
  107. }
  108. if (p->number == x && p == list.tail) DT(list);
  109. else{
  110. Node *q = list.head;
  111. while (q->next != p) q = q->next;
  112. q->next = p->next;
  113. delete(p);
  114. }
  115. return k;
  116. }
  117.  
  118. void AN(List &list, int x, int k){
  119. Node *newNode = createNode(x);
  120. int i = 1;
  121. if (k == 1){
  122. ATH(list, x);
  123. }
  124. else{
  125. Node *p = list.head;
  126. while (p != NULL && i != k - 1){
  127. i++;
  128. p = p->next;
  129. }
  130. newNode->next = p->next;
  131. p->next = newNode;
  132. }
  133. }
  134.  
  135. void xuat(List list){
  136. int i = 1;
  137. for (Node *p = list.head; p; p = p->next){
  138. cout << "Node " << i++ << ": " << p->number << endl;
  139. }
  140. }
  141.  
  142.  
  143. int menu(){
  144. cout << "1. Them vao dau danh sach";
  145. cout << "\n2. Them vao cuoi danh sach";
  146. cout << "\n3. Tim phan tu trong danh sach";
  147. cout << "\n4. Xoa phan tu dau danh sach";
  148. cout << "\n5. Xoa phan tu cuoi danh sach";
  149. cout << "\n6. Tim va Xoa phan tu";
  150. cout << "\n7. Them phan tu";
  151. cout << "\n8. Xuat danh sach";
  152. cout << "\n0. Ket thuc";
  153. cout << endl;
  154. int chon;
  155. cout << "\nMoi ban chon: ";
  156. cin >> chon;
  157. return chon;
  158. }
  159.  
  160. void main(){
  161. int x;
  162. int k;
  163. List list;
  164. createEmptylist(list);
  165. do{
  166. int chon = menu();
  167. switch (chon)
  168. {
  169. case 1:
  170. doc >> x;
  171. ATH(list, x);
  172. cout << endl;
  173. break;
  174. case 2:
  175. doc >> x;
  176. ATT(list, x);
  177. cout << endl;
  178. break;
  179. case 3:
  180. cout << "Nhap phan tu can tim: ";
  181. cin >> x;
  182. search(list, x);
  183. cout << endl;
  184. break;
  185. case 4:
  186. DH(list);
  187. break;
  188. case 5:
  189. DT(list);
  190. break;
  191. case 6:
  192. cout << "Nhap vao phan tu can tim va xoa: ";
  193. cin >>x;
  194. SAD(list, x);
  195. cout << endl;
  196. break;
  197. case 7:
  198. cout << "Nhap vao phan tu muon them va vi tri them vao: ";
  199. cin >> x >> k;
  200. AN(list, x, k);
  201. cout << endl;
  202. break;
  203. case 8:
  204. xuat(list);
  205. break;
  206. default:
  207. cout << "Tam biet.";
  208. cout << endl;
  209. break;
  210. }
  211. if (chon < 1)
  212. break;
  213. } while (1);
  214. doc.close();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment