Stack Isi
#include<iostream.h>
#include<conio.h>
struct stack{
int top;
int data[5];
};
stack s;
void inisialisasi()
{ s.top = -1;}
void push (int nilai)
{s.top ++;
s.data[s.top]=nilai;}
void pop()
{s.data[s.top]=NULL;
s.top --;}
void print()
{
for (int i=s.top; i>-1; i--)
{cout<<s.data[i];}
}
main()
{
int pilihan,data;
inisialisasi();
do
{
clrscr();
cout<<"1.push data"<<endl;
cout<<"2.pop data"<<endl;
cout<<"3.print data"<<endl;
cout<<endl;
cout<<"pilihan=";
cin>>pilihan;
clrscr();
switch (pilihan)
{
case 1:
{
if (s.top==5-1)
{
cout<<"stack penuh";
getch();
}
else
{
cout<<"data yg akan di-push = ";
cin>>data;
push(data);
}
break;
}
case 2:
{
if (s.top==1)
{
cout<<"stack kosong";
getch();
}
else
{
pop();
cout<<"data teratas telah terhapus";
getch();
}
break;
}
case 3:
{
if (s.top==-1)
{
cout<<"stack kosong";
getch();
}
else
{
print();
getch();
}
break;
}
default:
{
cout<<"tidak ada dalam pilihan"<<endl;
}
}
}while (pilihan>=1 && pilihan<=3);
getch();
}
Comments
Post a Comment