Skip to content

Instantly share code, notes, and snippets.

@xXFracXx
Created February 10, 2014 18:11
Show Gist options
  • Save xXFracXx/8921178 to your computer and use it in GitHub Desktop.
Save xXFracXx/8921178 to your computer and use it in GitHub Desktop.
A) Stacks [Linked List]
//Stacks using linked list
#include<iostream.h>
struct node
{
char name[20];
int age;
node *link;
};
class stack
{
node *top;
public: stack()
{
top=NULL;
}
void stackpush();
void stackpop();
void display();
}obj;
void stack::stackpush()
{
node *temp;
temp=new node;
cout<<"\nEnter the Name: ";
cin>>temp->name;
cout<<"Enter the Age: ";
cin>>temp->age;
temp->link=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->link=top;
top=temp;
}
}
void stack::stackpop()
{
node *temp;
if(top==NULL)
{
cout<<"Stack empty";
}
else
{
temp=top;
top=top->link;
temp->link=NULL;
delete temp;
}
}
void stack::display()
{
node *temp;
temp=top;
int i=0;
while(temp!=NULL)
{
i++;
cout<<"\nRecord "<<i<<" (From top to bottom)";
cout<<"\nName: "<<temp->name;
cout<<"\nAge: "<<temp->age<<"\n\n\n";
temp=temp->link;
}
}
int main()
{
int ch;
char opt='y';
do
{
cout<<"MAIN MENU"<<endl;
cout<<"1.Add an element"<<endl;
cout<<"2.Delete an element"<<endl;
cout<<"3.Display the stack"<<endl;
cout<<"4.Exit"<<endl;
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
do
{
obj.stackpush();
cout<<"Do you wanna enter data for more employees?(y/n): ";
cin>>opt;
}while(opt=='y');
break;
case 2:
opt='y';
do
{
obj.stackpop();
cout<<"Do you wanna continue?(y/n): ";
cin>>opt;
}while(opt=='y');
break;
case 3:
obj.display();
break;
case 4:exit(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