Selamat Datang Di Blogg Saya

Kamis, 22 Desember 2011

contoh program Stack

 list program

#include <cstdlib>
#include <iostream>

using namespace std;

int top= -1;
char stack[2]; //asumsi max stack 100
char x;

void push()
{
    
     cout<<"masukan satu karakter = ";
     cin>>x;
    
     top++;
     stack[top]=x;

     }
    
     void pop()
     {
     if(top<0)
          {
          cout<<"Stack kosong"<<endl;
          return;
     }
                  
     x = stack[top];
     top--;
                  
                   cout<<"Karakter yang di POP adalah = "<<x<<endl;
                   }
                  
     void cetak()
     {
     if(top<0)
     {
                                
                                 cout<<"stack kosong"<<endl;
                                 return;
     }
                                
     int i=0;
     for(i=top; i>=0; i--)
     cout<<stack[i]<<endl;
     }
    
int main(){
         int input;
         cout<<"MASUKAN PILIHAN : "<<endl;
         cout<<"\tpush=1"<<endl;
         cout<<"\tpop=2"<<endl;
         cout<<"\tcetak=3"<<endl;
         cout<<"\tquit=4"<<endl;
         while(true)
         {
                    cout<<"\Masukan Pilihan : ";
                    cin>>input;
                   
                    if (input==1)
                    {push();}
                    else if(input==2)
                    {pop();}
                    else if(input==3)
                    {cetak();}
                    else if(input==4)
                    {break;}
                    else
                    {cout << " perintah '" << input << "' tidak dikenal"<<endl;}
                    }
    

    system("PAUSE");
    return EXIT_SUCCESS;
}

0 comments:

Posting Komentar