Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- #define Max 100
- using namespace std;
- typedef int item;
- struct Queue
- {
- int Front, Rear;
- item Data[Max];
- int count;
- Queue() {
- Front = 0;
- Rear = -1;
- count = 0;
- }
- };
- int Isempty(Queue Q)
- {
- if (Q.count == 0)
- return 1;
- return 0;
- }
- int Isfull(Queue Q)
- {
- if (Q.count == Max)
- return 1;
- return 0;
- }
- void Push(Queue& Q, item x){
- if (Isfull(Q))
- cout << "Hang doi day !\n";
- else {
- Q.Data[++Q.Rear] = x;
- Q.count++;
- }
- }
- int Pop(Queue& Q)
- {
- if (Isempty(Q)) cout << "Hang doi rong !\n";
- else
- {
- item x = Q.Data[Q.Front];
- for (int i = Q.Front; i < Q.Rear; i++)
- Q.Data[i] = Q.Data[i + 1];
- Q.Rear--;
- Q.count--;
- return x;
- }
- }
- item Qfront(Queue Q)
- {
- if (Isempty(Q)) cout << "Hang doi rong !\n";
- else return Q.Data[Q.Front];
- }
- void Push_Circular(Queue& Q, item x)
- {
- if (Isfull(Q)) cout << "Hang doi day !\n";
- else
- {
- Q.Data[(++Q.Rear) % Max] = x;
- Q.count++;
- }
- }
- int Pop_Circular(Queue& Q)
- {
- if (Isempty(Q)) cout << "Hang doi rong !\n";
- item x = Q.Data[Q.Front];
- Q.Front = (Q.Front++) % Max;
- Q.count--;
- return x;
- }
- void Input(Queue& Q)
- {
- int n;
- item x;
- do
- {
- cout << "Nhap so phan tu cua Queue: ";
- cin >> n;
- } while (n > Max || n < 1);
- for (int i = 1; i <= n; i++)
- {
- cout << "Nhap phan tu thu " << i << " : ";
- cin >> x;
- Push(Q, x);
- }
- }
- void Output(Queue Q)
- {
- if (Isempty(Q)) cout << "Hang doi rong !\n";
- else
- {
- for (int i = Q.Front; i <= Q.Rear; i++)
- cout << Q.Data[i] << endl;
- }
- }
- int main()
- {
- Queue Q;
- Input(Q);
- Output(Q);
- int lua_chon;
- cout << "1.Kiem tra Queue rong hay khong?\n"<<
- "2.Kiem tra Queue day hay khong ? \n"<<
- "3.Them phan tu vao Queue.\n" <<
- "4.Xoa phan tu trong Queue.\n" <<
- "5.Xuat Queue.\n"<<
- "6.Thoat." << endl;
- do
- {
- cout << "Nhap lua chon cua ban: ";
- cin >> lua_chon;
- switch (lua_chon)
- {
- case 1: // if( luachon == 1)
- {
- if (Isempty(Q)) cout << "Queue rong !\n";
- else cout << "Queue khong rong !\n";
- break;
- }
- case 2:// if( luachon == 2)
- {
- if (Isfull(Q)) cout << "Queue day !\n";
- else cout << "Queue khong day !\n";
- break;
- }
- case 3: // if( luachon == 3)
- {
- item x;
- cout << "Nhap phan tu can chen vao Queue: ";
- cin >> x;
- Push(Q, x);
- Output(Q);
- break;
- }
- case 4:// if( luachon == 4)
- {
- Pop(Q);
- Output(Q);
- break;
- }
- case 5:
- {
- Output(Q);
- break;
- }
- default: break;
- }
- } while (lua_chon != 6);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement