Senin, 06 Februari 2012

Contoh Program Struktur Data Menggunakan Visual C++ 2008 Express Edition


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 10
void INSERT(void);
void DELETE(void);
void CETAKLAYAR(void);
void Inisialisasi(void);
void RESET(void);
int PIL,F,R;
char PILIHAN[1],HURUF;
char Q[n];
void main( )
{
      Inisialisasi();
      do
      {
            cout<<"  ANIMASI QUEUE"<<endl;
            cout<<"=============="<<endl;
            cout<<"1. INSERT"<<endl;
            cout<<"2. DELETE"<<endl;
            cout<<"3. CETAK QUEUE"<<endl;
            cout<<"4. QUIT"<<endl<<endl;
            cout<<"PILIHAN : "; cin>>PILIHAN;
            PIL=atoi(PILIHAN);
            switch (PIL)
            {
                  case 1:
                        INSERT();
                        break;
                  case 2:
                        DELETE();
                        break;
                  case 3:
                        CETAKLAYAR();
                        break;
                  default:
                        cout<<"TERIMA KASIH"<<endl;
                        break;
            }
                  cout<<"Press any key to continue"<<endl;
                  getch();
                  system("cls");
      }
      while (PIL<4);
}



void INSERT(void)
      if(R<n-1)
      {
            cout<<endl<<"MASUKKAN 1 HURUF : ";
            cin>>HURUF;
            Q[++R]=HURUF;;
      }
      else
            cout<<"Antrian Penuh"<<endl;
}

void CETAKLAYAR(void)
{
      if(F<R+1)
      {
            for (int i=F;i<=R;i++)
                  cout<<"Q["<<i<<"] = "<<Q[i]<<endl;
      }
      else
            cout<<"QUEUE Kosong"<<endl;
}

void DELETE(void)
{
      if(F<=R+1)
      {
            HURUF=Q[F];
            Q[F++]='\0';
            cout<<"Data yang diambil : "<<HURUF<<endl;
            if(F==n)
                  RESET();
      }
      else
            cout<<"Antrian Kosong"<<endl;
}

void Inisialisasi(void)
{
      F=0;
      R=-1;
}

void RESET(void)
{
      F=0;
      R=-1;
}


#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define n 10
void PUSH(void);
void POP(void);
void CETAKLAYAR(void);
void Inisialisasi(void);
void INSERT(void);
void DELETE(void);
void CETAKLAYARANTRIAN(void);
void RESET(void);
int PIL,TOP,F,R;
char PILIHAN[1],HURUF;
char Q[n];
char S[n];
using namespace std;
int main( )
{
      Inisialisasi();
      do
      {
            cout<<"  ANIMASI STACK"<<endl;
            cout<<"=============="<<endl;
            cout<<"1. PUSH"<<endl;
            cout<<"2. POP"<<endl;
            cout<<"3. CETAK STACK"<<endl;
            cout<<"4. QUIT"<<endl<<endl;

            cout<<"  ANIMASI QUEUE"<<endl;
            cout<<"=============="<<endl;
            cout<<"5. INSERT"<<endl;
            cout<<"6. DELETE"<<endl;
            cout<<"7. CETAK QUEUE"<<endl;
            cout<<"8. QUIT"<<endl<<endl;

            cout<<"PILIHAN : "; cin>>PILIHAN;
            PIL=atoi(PILIHAN);
            switch (PIL)
            {
                  case 1:
                        PUSH();
                        break;
                  case 2:
                        POP();
                        break;
                  case 3:
                        CETAKLAYAR();
                        break;
                  default:
                        cout<<"TERIMA KASIH"<<endl;
                        break;
                  case 5:
                        INSERT();
                        break;
                  case 6:
                        DELETE();
                        break;
                  case 7:
                        CETAKLAYARANTRIAN();
                        break;
                  case 8:
                        cout<<"terimakasih"<<endl;
                        break;
            }
                  cout<<"Press any key to continue"<<endl;
                  getch();
                  system("cls");
      }
      while (PIL<8);
}




void PUSH(void)
      if(TOP<n-1)
      {
            cout<<endl<<"MASUKKAN 1 HURUF : ";
            cin>>HURUF;
            S[++TOP]=HURUF;;
      }
      else
            cout<<"Stack Penuh"<<endl;
}
void CETAKLAYAR(void)
{
      for (int i=TOP;i>-1;i--)
            {
                  cout<<"S["<<i<<"] = "<<S[i]<<endl;
            }
}
void POP(void)
{
      if(TOP>-1)
      {
            HURUF=S[TOP];
            S[TOP]='\0';
TOP--;
            cout<<"Data yang diambil : "<<HURUF<<endl;
      }
      else
            cout<<"Stack Kosong"<<endl;
}
void Inisialisasi(void)
{
      TOP=-1;
      F=0;
      R=-1;
}
void INSERT(void)
      if(R<n-1)
      {
            cout<<endl<<"MASUKKAN 1 HURUF : ";
            cin>>HURUF;
            Q[++R]=HURUF;;
      }
      else
            cout<<"Antrian Penuh"<<endl;
}
void CETAKLAYARANTRIAN(void)
{
      if(F<R+1)
      {
            for (int i=F;i<=R;i++)
                  cout<<"Q["<<i<<"] = "<<Q[i]<<endl;
      }
      else
            cout<<"QUEUE Kosong"<<endl;
}
void DELETE(void)
{
      if(F<=R+1)
      {
            HURUF=Q[F];
            Q[F++]='\0';
            cout<<"Data yang diambil : "<<HURUF<<endl;
            if(F==n)
                  RESET();
      }
      else
            cout<<"Antrian Kosong"<<endl;
}


void RESET(void)
{
      F=0;
      R=-1;
}

OUTPUT





Tidak ada komentar:

Posting Komentar