pseocode dari stack(tumpukan)

12 March 2013 19:25:30 Dibaca : 1327

z#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#define size 5

struct stack
{

int a[size];
int top;
};

typedef struct stack STACK;
void inisialisasi(STACK *p)
{
p -> top=0;
}
int kosong(STACK *p)
{
return(p -> top==0);
}
int penuh(STACK *p)
{
return(p -> top==size);
}
void push(STACK *p,int x)
{
if(p->top==size)
cout<<"Tumpukan sudah penuh ";
else
p->a[++p->top]=x;
}

int pop(STACK *p)
{
if (p->top==-1)
{
cout<<"\nTumpukan masih kosong bro...\n";
cout<<"Silahkan masukan data dengan memilih Operasi Push";

return -1;
}
else
return p->a[p->top--];
}

void display (STACK *p)
{ int i;
if(p->top==-1)
cout<<"\nTumpukan masih kosong bro...\n"<<"\nSilahkan masukan data dengan memilih Operasi Push";
else
cout<<"\nSTACK is\n";
for (i=p->top;i>=0; --i)
cout<<p->a[i]<<"\n";
}

void main()
{

STACK s ;
int x,c,i;
s.top=-1;
cout<<"\t=======================================================\n";
cout<<"\t# PROGRAM STACK #\n";
cout<<"\t# ASNI PAGU #\n";
cout<<"\t# #\n";
cout<<"\t=======================================================\n";
cout<<endl;

do
{
cout<<"\n1: Operasi PUSH\n";
cout<<"2: Operasi POP\n";
cout<<"3: Tampilkan Stack\n";
cout<<"4: Hapus stack\n";
cout<<"5: Keluar\n";
cout<<"\n\n Masukan pilihan anda \n";cin>>c;

switch(c)
{
case 1: cout<<"\nMasukan data: ";cin>>x;
push (&s,x);
break;
case 2: x=pop(&s);
if(x!=-1)
cout<<"\nData yang dihapus adalah="<<x;
break;
case 3: display(&s);
break;
case 4: if(s.top==-1)
printf("\nTumpukan dalam keadaan kosong\n");
else
printf("\nFormat Perintah salah\n");

for (i=s.top;i>=0; --i)
printf("Data yang dihapus adalah %d\n",pop(&s));
s.top=-1;
} printf("\n\n");}
while(c!=5);
clrscr();
cout<<"\n\n\n\n\n\n";
cout<<"\t==============================================================\n";
cout<<"\t# Terima kasih telah menggunakan program dari saya ^_^ #\n";
cout<<"\t==============================================================\n";
cout<<"\t# Created by #\n";
cout<<"\t# Hasan Patue #\n";
cout<<"\t==============================================================\n";
getch();
}

Kategori

  • Masih Kosong

Blogroll

  • Masih Kosong