Program on Stacks Using Linked Lists

import java.io.*; class Node { int n; Node prev; } class stackLL { public static void main(String args[ ])throws IOExcep...


import java.io.*;
class Node
{
int n;
Node prev;
}
class stackLL
{
public static void main(String args[ ])throws IOException
{
BufferedReader br= new BufferedReader (new  InputStreamReader (System.in));
int ch;
Node p,top=null;
do
{
System.out.println(‘1: push  2:pop 3: display”);
System.out.println(“enter ur choice”);
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
p= new Node();
System.out.println(“enter data”);
p.n= Integer.parseInt (br.readLine());
p.prev=top;
top=p;
break;

case 2:
if(top!=null)
{
System.out.println(top.n+”elemet deleted”);
top=top.prev;
}
else
{
System.out.println(“stack is underflow”);
}
break;

case 3:
if(top== null)
{
System.out.println(“stack is underflow”);
break;
}
else
{
for(p=top;p!=null;p=p.prev)
{
System.out.println(“data=”+p.n);
}
}
break;
}
}while( ch < 4 );
}

}

Related

Data Structures 3956037333330300558

Post a Comment

emo-but-icon

item