#include using namespace std; struct Node { int x; Node *pNext; }; struct Stack { Node *pHead; Node *pTail; }; void CreateEmptyStack(Stack &S) { S.pHead = S.pTail = NULL; } //thêm phần tử vào Stack. addhead. khúc này gồm 2 bước tạo node và addhead void Push(Stack &S, int x) { Node *p = new Node; if (p == NULL) exit(1); p->x = x; p->pNext = NULL; if (S.pHead == NULL) { S.pHead = S.pTail = p; } else { p->pNext = S.pHead; S.pHead = p; } } bool IsEmpty(Stack S) { return (S.pHead == NULL); } bool Pop(Stack &S, int &x) { Node *p = S.pHead; if (!IsEmpty(S)) { x = p->x; S.pHead = S.pHead->pNext; delete p; return true; } return false; } struct Queue { Node *pHead; Node *pTail; }; void CreateEmptyQueue(Queue &Q) { Q.pHead = Q.pTail = NULL; } //thêm phần tử vào Queue. addtail. khúc này gồm 2 bước tạo node và addhead void Push(Queue &Q, int x) { Node *p = new Node; if (p == NULL) exit(1); p->x = x; p->pNext = NULL; if (Q.pHead == NULL) { Q.pHead = Q.pTail = p; } else { Q.pTail->pNext = p; Q.pTail = p; } } bool IsEmpty(Queue Q) { return (Q.pHead == NULL); } bool Pop(Queue &Q, int &x) { Node *p = Q.pHead; if (!IsEmpty(Q)) { x = p->x; Q.pHead = Q.pHead->pNext; delete p; return true; } return false; } //Nhập cho Stack void Input(Stack &S) { cout << "Nhap den khi gia tri nhap vao bang 0\n"; int x; do { cout << "x = "; cin >> x; if (x == 0) break; Push(S, x); } while (1); } //Chuyển số chẵn từ Stack qua Queue void Input(Stack &S, Queue &Q) { int x; while (!IsEmpty(S)) { if (Pop(S, x) == true) { if (x % 2 == 0) Push(Q, x); } } } //Xuất Queue ra để xem kết quả void Output(Queue &Q) { int x; while (!IsEmpty(Q)) { Pop(Q, x); cout << x << endl; } } void main() { Stack S; Queue Q; CreateEmptyQueue(Q); CreateEmptyStack(S); Input(S); Input(S, Q); Output(Q); }