0% found this document useful (0 votes)
4 views5 pages

Stack

The document contains a Java implementation of a custom stack with basic operations such as push, pop, display, and peek. It defines an interface for stack operations and a class that implements these operations, including error handling for stack overflow and underflow. A main method provides a menu-driven interface for users to interact with the stack functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Stack

The document contains a Java implementation of a custom stack with basic operations such as push, pop, display, and peek. It defines an interface for stack operations and a class that implements these operations, including error handling for stack overflow and underflow. A main method provides a menu-driven interface for users to interact with the stack functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.util.

*;
import java.io.*;

interface StackOperations
{
void push(int item);
int pop();
void display();
int peek();
}

class CustomStack implements StackOperations


{
int size;
int[] st;
private int top;
public CustomStack(int max)
{
size=max;
st=new int[size];
top=-1;
}
public void push(int item)
{
try
{
top=top+1;
st[top]=item;
}
catch(Exception e)
{
System.out.println("Stack full!! cannot insert element");
top--;
}
}
public int pop()
{
int item;
try{
item=st[top];
top--;
return item;
}
catch (Exception e)
{
System.out.println("Stack empty!! cannot pop element");
return -1;
}
}
public void display()
{
System.out.println("Stack contains");
for(int i=top;i>=0;i--)
{
if(top==-1)
{
System.out.println("Stack empty");
}
else if(top==(size-1))
{
System.out.println("Stack full!!");
}
else
{
System.out.println(" "+st[i]);
}
}

}
public int peek()
{
return st[top];
}
}

class StackTest
{
public static void main(String[] args)
{
char ch='n';
Scanner in =new Scanner(System.in);
CustomStack obj =new CustomStack(10);
do
{
System.out.println("Menu");
System.out.println("******************************");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. Display");
System.out.println("4. peek");
System.out.println("5. exit");
System.out.println("Enter your choice");
int ipt=in.nextInt();
switch(ipt)
{
case 1:
{
System.out.println("Enter the element");
int ip=in.nextInt();
obj.push(ip);
obj.display();
break;
}
case 2:
{
System.out.println("The popped element is:"+obj.pop());
break;
}
case 3:
{
obj.display();
break;
}
case 4:
{
System.out.println("The top most element is:"+obj.peek());
break;
}
default:
{
System.out.println("Invalid!!"+"Enter the choice from 1 to 4");
break;
}
}
System.out.println("Do you want to continue? (y/n)");
ch=in.next().charAt(0);
} while(ch=='y');
}
}

Output:

You might also like