// Program name : Stack Manager
// Description : Manage Stacks based on tables.
// Creator : By Mohamed K. Nasrallah
// Company : Dreamwood Studios - SmartScript Inc.
// Credits : All rights are reserved, (c) 2003 SmartScript Inc.
// Comment : Free of charge source code -dreamoudi@linuxmail.org
#include <iostream.h>
#define max 99
#define min 0
struct pile
{
int data[max+1];
int top;
};
pile*CreatPile(pile*loader)
{
loader = new pile;
loader->top = -1;
return loader;
}
int IsEmpty(pile*loader)
{
if(loader->top < min)
return 1;
else return 0;
}
void PrintPile(pile*loader)
{
if(loader && (IsEmpty(loader) == 0))
{
int i;
for(i = loader->top; i>=0 ; i--)
cout <<endl<<"["<<loader->data[i]<<"]"<<endl;
}
else
cout <<endl<<"Can't print, stack is empty"<<endl;
}
int PushData(pile*loader,int data)
{
if(loader && (loader->top < max))
{
loader->top++;
loader->data[loader->top] = data;
return 1;
}
else
{
return 0;
}
}
int PopData(pile*loader)
{
if(IsEmpty(loader))
{
return 0;
}
else
{
loader->top--;
return 1;
}
}
int PileFree(pile*loader)
{
if(IsEmpty(loader) == 0)
{
loader->top = -1;
return 1;
}
else return 0;
}
int SpyOnTop(pile*loader)
{
if(loader && (loader->top >= min))
{
int spyed = loader->data[loader->top];
return spyed;
}
else return NULL;
}
void main()
{
pile*loader = NULL;
double data;
int choice = 0;
int status;
while(choice!=6)
{
cout <<endl
<<"Choose an option "<<endl
<<" "<<endl
<<" 1- Push to stack "<<endl
<<" 2- Pop from stack "<<endl
<<" 3- Spy on top "<<endl
<<" 4- Clear stack "<<endl
<<" 5- Print Stack "<<endl
<<" 6- Exit "<<endl<<endl;
cout << "CHOICE\\>: ";
cin >> choice;
switch(choice)
{
case 1:
{
if(loader == NULL)
loader = CreatPile(loader);
cout <<endl<<endl<<"Enter a value: ";
cin >>data;
status = PushData(loader,data);
if (status == 1)
cout<<endl<<"Data was pushed succefully"<<endl;
else
cout<<endl<<"Stack is full"<<endl;
break;
}
case 2:
{
if(loader)
{
status = PopData(loader);
if(status == 1)
cout <<endl<<"Stack was been poped 1 time"<<endl;
else
cout <<endl<<"No data in stack"<<endl;
break;
}
else
cout <<endl<<"No stack"<<endl;
break;
}
case 3:
{
status = SpyOnTop(loader);
if(status != NULL)
cout <<endl<<"Top value is ["<<status<<"]"<<endl;
else
cout <<endl<<"Stack is empty, or no stack"<<endl;
break;
}
case 4:
{
if(loader)
{
status = PileFree(loader);
if(status == 1)
cout <<endl<<"Stack cleared"<<endl;
else
cout <<endl<<"Already cleared"<<endl;
}
else
cout <<endl<<"No stack to clear"<<endl;
break;
}
case 5:
{
PrintPile(loader);
break;
}
case 6:
{
break;
}
default:
{
cout <<endl<<"\aThis is an illegal choice, note that you must"<<endl
<<"read well the menu.\a"<<endl;
break;
}
}
}
delete loader;
}