Write the program for implement dynamically allocated stack containing names of country
Answers
Explanation:
Code for Program to create a stack using dynamic memory allocation in C++ Programming
# include <iostream.h>
# include <process.h>
# include <conio.h>
# include <malloc.h>
struct link_list
{
int no;
struct link_list *next;
};
class stack
{
link_list *list,*head;
public:
stack()
{
head=NULL;
free(list);
}
void push();
void peep();
void pop();
};
void stack :: push()
{
link_list *newnode;
newnode=new link_list;
cout<<"Enter Number to push :";
cin>>newnode->no;
list=head;
if(head==NULL)
{
head=newnode;
newnode->next=NULL;
}
else
{
newnode->next=list;
list=newnode;
head=newnode;
}
}
void stack :: peep()
{
cout<<endl;
if(head==NULL)
{
cout<<"Empty Stack !!!";
return;
}
list=head;
while(list!=NULL)
{
cout<<list->no<<"\t";
list=list->next;
}
}
void stack :: pop()
{
cout<<endl;
if(head==NULL)
{
cout<<"Empty Stack !!!";
return;
}
else
{
cout<<"Top : "<<head->no<<endl;
head=head->next;
}
}
int display_menu()
{
int ch;
clrscr();
cout<<endl;
cout<<"[ 1 ] Push"<<endl;
cout<<"[ 2 ] Pop"<<endl;
cout<<"[ 3 ] Peep"<<endl;
cout<<"[ 4 ] Exit"<<endl;
cout<<"Enter your choice :";
cin>>ch;
return ch;
}
void main()
{
stack s1;
while(1)
{
switch(display_menu())
{
case 1:s1.push();
break;
case 2:s1.pop();
getch();
break;
case 3:s1.peep();
getch();
break;
case 4: exit(1);
}
}
}
a stack using dynamic memory allocation ... free(list); } void push(); void peep (); void pop(); }; void stack :: push() { link_list *newnode