int stack[11];
void menu ()
{
cout<<"1.Push\n2. Pop\n3. Clear\n4. Print\n5. Exit\nPilihan Anda : "; //For Show The Menu
}
int push (int j) //Input data on the stack
{
if (j<10)
{
if(j==-1)
{
j=0;//indicator index
}
cout<<"Masukkan Data (int) = ";
cin>>stack [j];//input for data stack and j as a value index the array
cout<<"Data berhasil dipush\n";
j++;//increment for index the array
}
else
{
cout<<"Stack penuh, harap lakukan pop data dahulu\n";
}
return j;
}
int pop (int j)
{
if(j>=0)
{
j--;
cout<<"Data "<<stack[j]<<" Dipop\n";
}
else
{
cout<<"Stack kosong,harap lakukan push data dahulu\n";
}
return j;
}
void print (int j)
{
if(j>=0)
{
for(int i=j-1;i>=0;i--)
{
cout<<"["<<stack[i]<<"]\n";
}
}
else
{
cout<<"Stack kosong,harap lakukan push terlebih dahulu\n";
}
}
main ()
{
int i=-1,pilih;
start:
system ("cls");
menu ();
cin>>pilih;
if(pilih==1)
{
i=push(i);
cout<<"Jumlah isi stack : "<<i<<endl;
getch ();
goto start;
}
else if (pilih==2)
{
i=pop(i);
getch ();
goto start;
}
else if (pilih==3)
{
for(int x=0;x<i;x++)
{
stack[x]=NULL;//NULL = empty.
}
i=-1;//change the index to be -1, it means empty/ no array
cout<<"Semua data terhapus\n";
cout<<stack[1];
getch();
goto start;
}
else if (pilih==4)
{
print(i);
getch();
goto start;
}
else if (pilih==5)
{
cout<<"\n\nTerima Kasih\n";
}
else
{
cout<<"Input salah\n";
getch();
goto start;
}
return 0;
}
Home »
» UTS Stack
0 komentar:
Posting Komentar