Advertisement
Bagosep

Simple Double Stack

May 4th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //#include <conio.h>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. struct produk{
  11.     char nama[20];
  12. };
  13.  
  14. int top = -1;
  15. int n;
  16. int top2;
  17. int atas = 0, bawah = 0;
  18. produk stacks[20];
  19.  
  20. void push(){
  21.     top++;
  22. }
  23.  
  24. void push2(){
  25.     top2--;
  26. }
  27.  
  28. void pop(){
  29.     top--;
  30. }
  31.  
  32. void pop2(){
  33.     top2++;
  34. }
  35.  
  36. void input(){
  37.     push();
  38.     cin.ignore();
  39.     cout<<"Masukan Nama = ";cin.getline(stacks[top].nama, 20);
  40.     atas++;
  41. }
  42.  
  43. void input2(){
  44.     push2();
  45.     cin.ignore();
  46.     cout<<"Masukan Nama = ";cin.getline(stacks[top2].nama, 20);
  47.     bawah++;
  48. }
  49.  
  50. void tampil(){
  51.     for(int i=top; i>=0; i--){
  52.         cout<<"\nNama Pembeli = "<<stacks[i].nama;
  53.         cout<<endl;
  54.     }
  55.     for(int a=top2; a<n; a++){
  56.         cout<<"\nNama Pembeli = "<<stacks[a].nama;
  57.         cout<<endl;
  58.     }
  59. }
  60.  
  61. bool isFull(){
  62.     int penuh = atas + bawah;
  63.     if(penuh == n){
  64.         return true;
  65.     }
  66.     else{
  67.         return false;
  68.     }
  69. }
  70.  
  71. bool isEmpty(){
  72.     if(top==-1){
  73.         return true;
  74.     }
  75.     else{
  76.         return false;
  77.     }
  78. }
  79.  
  80. bool isEmpty2(){
  81.     if(top2==n-1){
  82.         return true;
  83.     }
  84.     else{
  85.         return false;
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91.     int menu;
  92.     char ulang;
  93.     cout<<"Masukan Panjang Stacks = ";cin>>n;
  94.     top2 = n;
  95.     system("cls");
  96.     do{
  97.     cout<<"\nMenu";
  98.     cout<<"\n1. Input 1";
  99.     cout<<"\n2. Input 2";
  100.     cout<<"\n3. Delete 1";
  101.     cout<<"\n4. Delete 2";
  102.     cout<<"\n5. View";
  103.     cout<<"\nPilihan anda = ";cin>>menu;
  104.     switch(menu){
  105.     case 1:
  106.         if(!isFull()){
  107.             input();
  108.         }
  109.         else{
  110.             cout<<"Stack is FULL!";
  111.         }
  112.         break;
  113.     case 2:
  114.         if(!isFull()){
  115.             input2();
  116.         }
  117.         else{
  118.             cout<<"Stacks is FULL!";
  119.         }
  120.         break;
  121.     case 3:
  122.         if(!isEmpty()){
  123.             pop();
  124.             atas--;
  125.             cout<<"\nData terhapus";
  126.         }
  127.         else{
  128.             cout<<"\nStacks IS NULL!";
  129.         }
  130.         break;
  131.     case 4:
  132.         if(!isEmpty2()){
  133.             pop2();
  134.             bawah--;
  135.             cout<<"\nData Terhapus ";
  136.         }
  137.         else{
  138.             cout<<"\nStack IS NULL!";
  139.         }
  140.         break;
  141.     case 5:
  142.         system("cls");
  143.         tampil();
  144.         break;
  145.  
  146.     }
  147.     cout<<"\n\nUlang = ";cin>>ulang;
  148.     system("cls");
  149.     }while(ulang=='y');
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement