Computer Science, asked by amrithaadithya9072, 1 year ago

Write the program for implement dynamically allocated stack containing names of country

Answers

Answered by Anonymous
0

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);

}

}

}

Answered by N3KKI
0

a stack using dynamic memory allocation ... free(list); } void push(); void peep (); void pop(); }; void stack :: push() { link_list *newnode

Similar questions