Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. using namespace std;
  4.  
  5. typedef struct NODE*node;
  6. typedef long long ll;
  7. struct NODE
  8. {
  9. int value;
  10. node next;
  11. };
  12.  
  13. node getNODE(int data)
  14. {
  15. node p = new NODE;
  16. if (p == NULL)
  17. return NULL;
  18. p->value = data;
  19. p->next = NULL;
  20. return p;
  21. }
  22.  
  23. void AddLast(node &head, node &tail, int data)
  24. {
  25. node p = getNODE(data);
  26. if (p == NULL)
  27. {
  28. cout << "Loi khong du bo nho!!!" << endl;
  29. exit(0);
  30. }
  31. if (head == NULL)
  32. {
  33. head = p;
  34. tail = p;
  35. }
  36. else
  37. {
  38. tail->next = p;
  39. tail = p;
  40. }
  41. }
  42.  
  43. void PrintList(node head)
  44. {
  45. if (head == NULL)
  46. {
  47. cout << "Danh sach rong!!!" << endl;
  48. return;
  49. }
  50. node p = head;
  51. cout << "Danh sach cac phan tu la: " << endl << "\t";
  52. for (p; p != NULL; p = p->next)
  53. {
  54. cout << p->value << " ";
  55. }
  56. cout << endl;
  57. }
  58.  
  59. void DestroyList(node head)
  60. {
  61. while (head)
  62. {
  63. node p = head;
  64. head = p->next;
  65. delete p;
  66. }
  67. }
  68.  
  69. int Cau3(node head)
  70. {
  71. unordered_map<int, int>temp;
  72. node p = head, q = head;
  73. for (p; p != NULL; p = p->next)
  74. {
  75. temp[p->value]++;
  76. }
  77. for (q; q != NULL; q = q->next)
  78. {
  79. if (temp[q->value] >= 2)
  80. return q->value;
  81. }
  82. return -1;
  83. }
  84.  
  85. int main()
  86. {
  87.  
  88. node head = NULL, tail = NULL;
  89. int k;
  90. cout << "Nhap vao cac so nguyen, nhap mot ki tu bat ki de dung viec nhap" << endl;
  91. while (cin >> k)
  92. {
  93. AddLast(head, tail, k);
  94. }
  95. system("cls");
  96. PrintList(head);
  97. cout << "Phan tu trung lap dau tien: ";
  98. cout << Cau3(head) << endl;
  99. delete head;
  100. delete tail;
  101. system("pause");
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement