stack uts
#include <iostream>#include <conio.h>
#include <cstdlib>
using namespace std;
int stack[11]; //There are 11 data in the data stack
void menu ()
{
cout<<"1.Push\n2. Pop\n3. Clear\n4. Print\n5. Exit\nPilihan Anda : "; //For show menu
}
int push (int j) //input data on the stack
{
if (j<10)
{
if(j==-1) // indicates data are not yet available, it must be inputted first
{
j=0; //indicators index
}
cout<<"Masukkan Data (int) = ";
cin>>stack [j] ;//stack data inputted and j as the value of the associated array indexing
cout<<"Data berhasil dipush\n";
j++; //the associated array indexing incremented by 1
}
else
{
cout<<"Stack penuh, harap lakukan pop data dahulu\n"; //to see if the sentence out //stack full
}
return j; //get value j
}
int pop (int j) //input data on the stack
{
if(j>=0) //indicates the data already exists
{
cout<<"Nilai j : "<<j<<endl; //show value j
j--; //nilai j berkurang 1
cout<<"Nilai j : "<<j<<endl;
cout<<"Data "<<stack[j]<<" Dipop\n"; //display data from the value stack indexing dg j
}
else
{
cout<<"Stack kosong,harap lakukan push data dahulu\n"; //the views that will come out when the previous conditions are not met
}
return j; //returns a value j
}
void print (int j) //display stack
{
if(j>=0) //indicates that the data is there
{
for(int i=j-1;i>=0;i--) //loop to display the stack that has been inputted data or the data//already push
{
cout<<"["<<stack[i]<<"]\n"; //Display stack
}
}
else
{
cout<<"Stack kosong,harap lakukan push terlebih dahulu\n";
}
}
main ()
{
int i=-1,pilih; //i variable declaration and select a data type integer
start:
system ("cls"); //for clear screen
menu (); //calling the function menu
cin>>pilih; //include the value of select the appropriate menu displayed
if(pilih==1) //if select equals 1
{
i=push(i); //calling the function push
cout<<"Jumlah isi stack : "<<i<<endl;
getch ();
goto start; //repeat to start
}
else if (pilih==2) //if select equals 2
{
i=pop(i); //call the function pop
cout<<"Nilai i : "<<i<<endl;
getch ();
goto start;
}
else if (pilih==3) //if select equals 3
{
for(int x=0;x<i;x++)
{
stack[x]=NULL;//NULL = kosong
}
i=-1; //change of -1 means the index becomes empty or no array -1
cout<<"Semua data terhapus\n";
cout<<stack[1];
getch();
goto start;
}
else if (pilih==4) //if select equals 4
{
print(i); //call the function print
getch();
goto start;
}
else if (pilih==5) //if select equals 5
{
cout<<"\n\nTerima Kasih\n";
}
else
{
cout<<"Input salah\n";
getch();
goto start;
}
return 0;
}