Import Javaio
Import Javaio
*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.println(data+" ");
}
}
class Stack
{
public Node first;
public Stack()
{
first=null;
}
public boolean isEmpty()
{
return(first==null);
}
public void push(int value)
{
Node newnode=new Node(value);
newnode.next=first;
first=newnode;
}
public int pop()
{
if(isEmpty())
{
System.out.println("\n Stack is Empty...");
return 0;
}
else
{
int r1=first.data;
first=first.next;
return r1;
}
}
public void display()
{
Node current=first;
while(current!=null)
{
System.out.print(" "+current.data);
current=current.next;
}
System.out.println("");
}
}
class StackList
{
public static void main(String args[]) throws IOException
{
Stack S=new Stack();
int ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("\nMENU");
System.out.println("----------");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the number of elements to
push: ");
int n1=Integer.parseInt(br.readLine());
System.out.println("\nEnter the elements: ");
for(int i=0; i<n1; i++)
{
S.push(Integer.parseInt(br.readLine()));
}
break;
case 2:
System.out.println("\nEnter the number of elements to
pop: ");
int n2=Integer.parseInt(br.readLine());
for(int i=0; i<n2; i++)
{
System.out.println(S.pop()+" is popped from
stack... ");
}
break;
case 3:
System.out.println("\nStack elements are:");
S.display();
break;
default:
break;
}
}
while(ch!=4);
}
}