Skip to content

Instantly share code, notes, and snippets.

@xXFracXx
Last active August 29, 2015 13:56
Show Gist options
  • Save xXFracXx/8921226 to your computer and use it in GitHub Desktop.
Save xXFracXx/8921226 to your computer and use it in GitHub Desktop.
C) Stack [Array]
//Stack as an array
#include<iostream.h>
#include<string.h>
#define MAX 100
#include<process.h>
int top;
struct abc
{
char ename[20];
int enumber;
float esal;
}stack[MAX];
void push(abc stack[],int &top)
{
if(top==MAX-1)
{
cout<<"stack full";
}
else
{
top=top+1;
cout<<"\nEnter Employee Name: ";
cin>>stack[top].ename;
cout<<"Enter Employee Number: ";
cin>>stack[top].enumber;
cout<<"Enter Employee Salary: ";
cin>>stack[top].esal;
}
}
int pop(abc stack[],int &top)
{
int value;
if(top<0)
{
cout<<"Stack Empty";
value=-1;
}
else
{
value=top;
top=top-1;
}
return(value);
}
void show(abc stack[],int top)
{
int i,j;
if(top<0)
{
cout<<"Stack Empty";
}
i=top;
cout<<"The array is:";
do
{
cout<<"\n\nEmployee Name: "<<stack[i].ename;
cout<<"\nEmployee Number"<<stack[i].enumber;
cout<<"\nEmployee Salary"<<stack[i].esal;
i=i-1;
}while(i>=0);
cout<<endl;
cin>>j;
}
int main()
{
int val;
top=-1;
int ch;
char opt='y';
do
{
system("cls");
cout<<"MAIN MENU";
cout<<"\n1.Add an element";
cout<<"\n2.Delete an element";
cout<<"\n3.Display the array";
cout<<"\n4.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
do
{
push(stack,top);
cout<<"Do you wish to add more?(y/n): ";
cin>>opt;
}while(opt=='y');
break;
case 2:
opt='y';
do
{
val=pop(stack,top);
if(val!=-1)
cout<<"The topmost stack is now deleted";
cout<<"\nDo you wish to delete more values?";
cin>>opt;
}while(opt=='y');
break;
case 3:
show(stack,top);
break;
case 4:
exit(0);
break;
default: 0;
}
}while(ch!=4);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment