Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_map>
- using namespace std;
- typedef struct NODE*node;
- typedef long long ll;
- struct NODE
- {
- int value;
- node next;
- };
- node getNODE(int data)
- {
- node p = new NODE;
- if (p == NULL)
- return NULL;
- p->value = data;
- p->next = NULL;
- return p;
- }
- void AddLast(node &head, node &tail, int data)
- {
- node p = getNODE(data);
- if (p == NULL)
- {
- cout << "Loi khong du bo nho!!!" << endl;
- exit(0);
- }
- if (head == NULL)
- {
- head = p;
- tail = p;
- }
- else
- {
- tail->next = p;
- tail = p;
- }
- }
- void PrintList(node head)
- {
- if (head == NULL)
- {
- cout << "Danh sach rong!!!" << endl;
- return;
- }
- node p = head;
- cout << "Danh sach cac phan tu la: " << endl << "\t";
- for (p; p != NULL; p = p->next)
- {
- cout << p->value << " ";
- }
- cout << endl;
- }
- void DestroyList(node head)
- {
- while (head)
- {
- node p = head;
- head = p->next;
- delete p;
- }
- }
- int Cau3(node head)
- {
- unordered_map<int, int>temp;
- node p = head, q = head;
- for (p; p != NULL; p = p->next)
- {
- temp[p->value]++;
- }
- for (q; q != NULL; q = q->next)
- {
- if (temp[q->value] >= 2)
- return q->value;
- }
- return -1;
- }
- int main()
- {
- node head = NULL, tail = NULL;
- int k;
- cout << "Nhap vao cac so nguyen, nhap mot ki tu bat ki de dung viec nhap" << endl;
- while (cin >> k)
- {
- AddLast(head, tail, k);
- }
- system("cls");
- PrintList(head);
- cout << "Phan tu trung lap dau tien: ";
- cout << Cau3(head) << endl;
- delete head;
- delete tail;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement