#include <iostream>
using namespace std;
// Khai báo cấu trúc Node
struct Node {
int data;
Node* pNext;
};
// Khai báo cấu trúc LIST (danh sách liên kết đơn)
struct LIST {
Node* pHead;
Node* pTail;
};
// Khởi tạo danh sách rỗng
void KhoiTaoDanhSach(LIST &ds) {
ds.pHead = NULL;
ds.pTail = NULL;
}
// Tạo một Node mới
Node* TaoNode(int x) {
Node* p = new Node;
p->data = x;
p->pNext = NULL;
return p;
}
// Hàm chèn một phần tử vào cuối danh sách
void ChenCuoi(LIST &ds, Node *p) {
if (ds.pHead == NULL) { // Nếu danh sách rỗng
ds.pHead = ds.pTail = p;
} else { // Nếu danh sách đã có phần tử
ds.pTail->pNext = p;
ds.pTail = p;
}
}
// Hàm nhập danh sách
void NhapDanhSach(LIST &ds) {
int n, x;
cout << "Nhap so luong phan tu: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Nhap gia tri phan tu thu " << i + 1 << ": ";
cin >> x;
Node* p = TaoNode(x);
ChenCuoi(ds, p);
}
}
// Hàm xuất danh sách
void XuatDanhSach(LIST ds) {
Node* p = ds.pHead;
while (p != NULL) {
cout << p->data << " ";
p = p->pNext;
}
cout << endl;
}
// Hàm chính
int main() {
LIST ds;
KhoiTaoDanhSach(ds);
// Nhập danh sách
NhapDanhSach(ds);
// Xuất danh sách
cout << "Danh sach vua nhap: ";
XuatDanhSach(ds);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gS2hhaSBiw6FvIGPhuqV1IHRyw7pjIE5vZGUKc3RydWN0IE5vZGUgewogICAgaW50IGRhdGE7CiAgICBOb2RlKiBwTmV4dDsKfTsKCi8vIEtoYWkgYsOhbyBj4bqldSB0csO6YyBMSVNUIChkYW5oIHPDoWNoIGxpw6puIGvhur90IMSRxqFuKQpzdHJ1Y3QgTElTVCB7CiAgICBOb2RlKiBwSGVhZDsKICAgIE5vZGUqIHBUYWlsOwp9OwoKLy8gS2jhu59pIH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