No products in the cart.

Write a program to create a …

CBSE, JEE, NEET, CUET

CBSE, JEE, NEET, CUET

Question Bank, Mock Tests, Exam Papers

NCERT Solutions, Sample Papers, Notes, Videos

Write a program to create a linked stack

 

  • 1 answers

Deepa Sanil 7 years, 4 months ago

#include <iostream.h>

#include<conio.h>

#include<process.h>

struct node

{ int info;

node *next;

};

node *top,*newptr,*temp,*ptr;

node *createnode(int);

void push(node*);

void display(node*);

void pop();

void main()

{clrscr();

top=NULL;

int ch,info;

char ch1,ch2;

do {

clrscr();

cout<<"\n \n1. Push \n2. Pop\n3. Display \n4. Exit\n";

cout<<"\nEnter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"Enter the item : ";

cin>>info;

newptr=createnode(info);

push(newptr);

break;

case 2:

cout<<"Do u want to delete the first node?";

cin>>ch1;

if(ch1=='y'||ch1=='Y')

pop();

cout<<"Now the list is:\n";

display(top);

break;

case 3:

display(top);

break;

case 4:

exit(0);

default:

cout<<"Invalid choice. Please try again. \n";

} cout<<"\n\nDo u want to continue";

cin>>ch2;

} while(ch2=='y');

getch();

}

node* createnode(int n)

{

ptr=new node;

ptr->info=n;

ptr->next=NULL;

return ptr;

}

void push(node* np)

{

if(top==NULL)

top=np;

else

{

temp=top;

top=np;

np->next=temp;

}

cout<<"\nItem inserted:"<<np->info;

}

void pop()

{

if(top==NULL)

cout<<"Underflow!!\n";

else

{

ptr=top;

top=top->next;

delete ptr;

}

}

void display(node* np)

{

while(np!=NULL)

{

cout<<np->info<<"-->";

np=np->next;

}

cout<<"\n";

}

http://mycbseguide.com/examin8/

Related Questions

What is mean by buffering in file handling?
  • 1 answers
What are called tokens
  • 2 answers

myCBSEguide App

myCBSEguide

Trusted by 1 Crore+ Students

Test Generator

Test Generator

Create papers online. It's FREE.

CUET Mock Tests

CUET Mock Tests

75,000+ questions to practice only on myCBSEguide app

Download myCBSEguide App