This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Tampilkan postingan dengan label array. Tampilkan semua postingan
Tampilkan postingan dengan label array. Tampilkan semua postingan

Kamis, 02 April 2015

C++ Program to Implement Stack

Stack bisa diartikan sebagai suatu kumpulan data yang seolah-olah ada data yang diletakkan di atas data yang lain. Di dalam stack ini kita dapat menambahkan ataupun mengambil (menghapus) data melalui ujung yang sama yang disebut dengan ujung atas stack (top of stack). Sifat dari STACK adalah LIFO atau Last In First Out, dapat kita artikan yang terakhir masuk ialah yang pertama keluar.

Dalam penyajian stack, kita dapat menggunakan array, dengan anggapan bahwa banyaknya elemen maksimal dari suatu stack tidak melebihi batas maksimum banyaknya elemen array. Pada suatu ketika, ukuran stack akan sama dengan ukuran array, bila diteruskan penambahan datanya maka akan terjadi overflow. Oleh karena itu, perlu ditambahkan data untuk mencatat posisi ujung stack.
Contoh 1 :
Berikut merupakan ilustrasi stack:






      
 Listing Program STACK:

#include <iostream.h>
#include <conio.h>
#define max 10

struct Tumpukan{
int atas;
   int data[max];
   }T;

void awal(){
T.atas=-1;
   }

int kosong(){
if(T.atas==-1)
    return 1;
   else
    return 0;
      }

int penuh(){
if(T.atas==max-1)
    return 1;
   else
    return 0;
      }


void input(int data){
if(kosong()==1)
    {T.atas++;
       T.data[T.atas]=data;
       cout<<"Data "<<T.data[T.atas]<<" masuk ke stack";}
   else if(penuh()==0)
    {T.atas++;
       T.data[T.atas]=data;
       cout<<"Data "<<T.data[T.atas]<<" masuk ke stack";}

   else
       cout<<"Tumpukan penuh";
   }

void hapus(){
   if(kosong()==0){
      cout<<"Data teratas sudah terambil";
   T.atas--;
   }
   else
   cout<<"Data kosong";
   }

void tampil(){
if(kosong()==0)
   {for(int i=T.atas;i>=0;i--)
    {cout<<"\nTumpukan ke "<<i<<"="<<T.data[i];}
      }
   else
   cout<<"Tumpukan kosong";
   }

void bersih(){
T.atas=-1;

cout<<"Tumpukan kosong!";
}

void main(){
int pil,data;
awal();
do
{
clrscr();
cout<<"1. Input\n2. Hapus\n3. Tampil\n4. Bersihkan\n5. Keluar\nMasukkan pilihan :";
cin>>pil;
switch(pil)
{case 1:cout<<"Masukkan data = ";cin>>data;
     input(data);
           break;
    case 2:hapus();
     break;
    case 3:tampil();
     break;
    case 4:bersih();
     break;
    case 5: cout<<"Terimakasih, tekan enter untuk keluar";
    }
getch();     }
while(pil!=5);}

Screen Shot program sebagai berikut:








 

Contoh 2 
 #include<iostream>
#include<conio.h>
#include<string>

using namespace std;

const int maks=4;
struct Tumpukan {
    int isi[maks];
    int atas;
};
Tumpukan x;

void push(int y){
    x.atas++;
    x.isi[x.atas]=y;
}

void inisialisasi (){
    x.atas=-1;
}

void pop(){
    x.atas--;
}


void main()
{
    inisialisasi();

    int z,b;
    cin>>z>>b;

    push(z); push(b);
    cout<<x.isi[x.atas]<<" atas : "<<x.atas-1<<endl;
    cout<<x.isi[x.atas]<<" atas : "<<x.atas<<endl;

    pop();
    cout<<x.isi[x.atas]<<" atas : "<<x.atas<<endl;
    cout<<x.isi[x.atas]<<" atas : "<<x.atas+1<<endl;

    cin>>z;
    push(z);
    cout<<x.isi[x.atas]<<" atas : "<<x.atas<<endl;

    _getch();
   
}

Code for Stacks code in C++ in C++ Programming

//stack using templates
#include<iostream.h>
#include<iomanip.h>
template<class s>                                ///templats classclass stack
{
      private:
          int top;
          s item;
          s array[5];
      public:
          stack()
          {
              top=-1;
          }
          void getdata()
          {
                  cout<<"enter item to push in stack"<<endl;
                cin>>item;
             
          }
          void push()
          {
              if(top==5)
                  cout<<"overflow"<<endl;
              else
              {
                  top++;
                  array[top]=item;
              }
          }
          void pop()
          {
              if(top==-1)
                  cout<<"underflow"<<endl;
              else
              {
                  array[top]=NULL;
                 
                  top--;
              }
          }
          void disp()
          {
               for(int i=0;i<=top;i++)
               {
                     cout<<array[i]<<'\t';
               }
                cout<<endl;
          }
};
int main()
{
    stack<int>stacki;
    stack<char>stackc;
    int r;
    char ch;
    do
    {
        cout<<"integer array"<<endl;
        cout<<"press 1 to push element in stack & 2 to pop it"<<endl; 
        cout<<"    3 to display stack"<<endl;
        cin>>r;
        switch (r)
        {
          case 1:
               stacki.getdata();
               stacki.push();
               break;
          case 2:
                stacki.pop();
                break;
          case 3:
                stacki.disp();
                break;
          default:
            cout<<"bad input"<<endl;
            break;
        }
        cout<<"do you want to process more y/n"<<endl;
        cin>>ch;
    }
    while(ch!='n');
do
{   
    cout<<"character array"<<endl;
        cout<<"press 1 to push element in stack & 2 to pop it"<<endl; 
        cout<<"    3 to display stack"<<endl;
        cin>>r;
        switch (r)
        {
          case 1:
               stackc.getdata();
               stackc.push();
               break;
          case 2:
                stackc.pop();
                break;
          case 3:
                stackc.disp();
                break;
          default:
            cout<<"bad input"<<endl;
            break;
        }
        cout<<"do you want to process more y/n"<<endl;
        cin>>ch;
    }
    while(ch!='n');

    return 0;
}

C++ programs to implement the Stack ADT using an array

/* Write C++ programs to implement the Stack ADT using an array */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
             int stk[5];
             int top;
      public:
             stack()
              {
                top=-1;
               }
             void push(int x)
              {
                 if(top >  4)
                       {
                           cout <<"stack over flow";
                           return;
                       }
                 stk[++top]=x;
                 cout <<"inserted" <<x;
               }
             void pop()
               {
                  if(top <0)
                   {
                         cout <<"stack under flow";
                         return;
                    }
                    cout <<"deleted" <<stk[top--];
                }
             void display()
               {
                   if(top<0)
                    {
                            cout <<" stack empty";
                            return;
                    }
                    for(int i=top;i>=0;i--)
                    cout <<stk[i] <<" ";
                }
};
 
main()
{
     int ch;
     stack st;
     while(1)
        {
             cout <<"\n1.push  2.pop  3.display  4.exit\nEnter ur choice";
             cin >> ch;
             switch(ch)
              {
               case 1:  cout <<"enter the element";
                        cin >> ch;
                        st.push(ch);
                        break;
               case 2:  st.pop();  break;
               case 3:  st.display();break;
               case 4:  exit(0);
               }
         }
return (0);
}
OUTPUTS
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3
1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
inserted5
1.push 2.pop 3.display 4.exit
Enter ur choice3
5 2
1.push 2.pop 3.display 4.exit
Enter ur choice4
luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com