Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Preprosesor
- #include<iostream>
- #include<conio.h>
- #include<stdio.h>
- #include<windows.h>
- using namespace std;
- //deklarasi stack dengan struct dan array
- struct QUEUE_STACK
- {
- string data_queue[100];
- string data_stack[100];
- int top_queue;
- int top_stack;
- string kata_queue[100];
- string kata_stack[100];
- };
- //deklarasi variabel antrian dari struct
- QUEUE_STACK antrian;
- QUEUE_STACK tumpukan;
- //deklarasi fungsi operasi stack
- void inisialisasi_stack();
- void inisialisasi_queue();
- int IsEmpty_queue();
- int IsFull_stack();
- int IsEmpty_stack();
- int IsFull_queue();
- void enqueue(string data_queue);
- void dequeue();
- void push(string data_stack);
- void pop();
- //fungsi main program
- int main()
- {
- int pilih, i;
- string input;
- inisialisasi_queue();
- inisialisasi_stack();
- do{
- system("cls");
- cout<<"|QUEUE"<<endl;
- cout<<"|1. Enqueue"<<endl;
- cout<<"|2. Dequeue"<<endl;
- cout<<"|3. Print Queue"<<endl;
- cout<<"|4. Clear Queue"<<endl;
- cout<<"|STACK"<<endl;
- cout<<"|5. Push"<<endl;
- cout<<"|6. pop"<<endl;
- cout<<"|7. Print Stack"<<endl;
- cout<<"|8. Clear Stack"<<endl;
- cout<<"|9. Exit"<<endl;
- cout<<endl;
- cout<<"Pilih : ";
- cin>>pilih;
- switch(pilih)
- {
- case 1:
- {
- if(IsFull_queue() == 1)
- {
- cout<<"Antrian penuh !";
- }
- else
- {
- cout<<"Data yang akan di Enqueue : ";
- cin>>input;
- enqueue(input);
- }
- cout<<endl;
- getch();
- break;
- }
- case 2:
- {
- if(IsEmpty_queue() == 1)
- {
- cout<<"Antrian kosong !";
- }
- else
- {
- cout<<"Data yang akan di Dequeue = "
- <<antrian.data_queue[0]<<endl;
- dequeue();
- }
- cout<<endl;
- getch();
- break;
- }
- case 3:
- {
- if(IsEmpty_queue() == 1)
- {
- cout<<"Antrian kosong !"<<endl;
- }
- else
- {
- cout<<"Data : ";
- for(i=0;i<=antrian.top_queue;i++)
- {
- cout<<antrian.data_queue[i]<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- getch();
- break;
- }
- case 4:
- {
- inisialisasi_queue();
- cout<<"Antrian sudah kosong !"<<endl;
- cout<<endl;
- getch();
- break;
- }
- case 5:
- {
- if(IsFull_stack() == 1)
- {
- cout<<"Tumpukan penuh !";
- }
- else
- {
- cout<<"Data yang akan di push : ";
- cin>>input;
- push(input);
- }
- cout<<endl;
- getch();
- break;
- }
- case 6:
- {
- if(IsEmpty_stack() == 1)
- {
- cout<<"Tumpukan kosong !";
- }
- else
- {
- cout<<"Data yang akan di Pop = "
- <<tumpukan.data_stack[tumpukan.top_stack]<<endl;
- pop();
- }
- cout<<endl;
- getch();
- break;
- }
- case 7:
- {
- if(IsEmpty_stack() == 1)
- {
- cout<<"Tumpukan kosong !"<<endl;
- }
- else
- {
- cout<<"Data : ";
- for(i=0;i<=tumpukan.top_stack;i++)
- {
- cout<<tumpukan.data_stack[i]<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- getch();
- break;
- }
- case 8:
- {
- inisialisasi_stack();
- cout<<"Tumpukan sudah kosong !"<<endl;
- cout<<endl;
- getch();
- break;
- }
- case 9:
- {
- cout<<"Program berhenti";
- return 0;
- }
- break;
- default:
- {
- cout<<"Tidak ada dalam pilihan"<<endl;
- getch();
- }
- }
- }while(pilih>=1);
- getch();
- }
- // fungsi inisialisasi stack = kosong
- void inisialisasi_queue()
- {
- antrian.top_queue = -1;
- }
- void inisialisasi_stack()
- {
- tumpukan.top_stack = -1;
- }
- //fungsi mengecheck apakah stack kosong
- int IsEmpty_queue()
- {
- if (antrian.top_queue == -1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int IsEmpty_stack()
- {
- if (tumpukan.top_stack == -1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //fungsi mengecheck apakah stack penuh
- int IsFull_queue()
- {
- if (antrian.top_queue == 100-1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int IsFull_stack()
- {
- if (tumpukan.top_stack == 100-1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //fungsi untuk menyisipkan data ke stack
- void enqueue(string data_queue)
- {
- antrian.top_queue++;
- antrian.data_queue[antrian.top_queue] = data_queue;
- }
- void push(string data_stack)
- {
- tumpukan.top_stack++;
- tumpukan.data_stack[tumpukan.top_stack] = data_stack;
- }
- //fungsi untuk mengeluarkan data dari stack
- void dequeue()
- {
- for(int i=0; i<100; i++)
- {
- antrian.data_queue[i] = antrian.data_queue[i+1];
- }
- antrian.top_queue = antrian.top_queue - 1;
- if(antrian.top_queue<0)
- {
- antrian.top_queue = -1;
- }
- }
- void pop()
- {
- tumpukan.top_stack = tumpukan.top_stack - 1;
- if(tumpukan.top_stack<0)
- {
- tumpukan.top_stack = -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement