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

EX - NO:04 Implementation of Java Interface For Adt Stack Aim

The document describes implementing a Java interface for an ADT stack using both arrays and linked lists. It defines methods like push, pop, and display and provides classes that implement the stack interface using each data structure. Exception handling is included and the main method allows the user to choose between the implementations and call stack operations.

Uploaded by

karen
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)
28 views5 pages

EX - NO:04 Implementation of Java Interface For Adt Stack Aim

The document describes implementing a Java interface for an ADT stack using both arrays and linked lists. It defines methods like push, pop, and display and provides classes that implement the stack interface using each data structure. Exception handling is included and the main method allows the user to choose between the implementations and call stack operations.

Uploaded by

karen
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

EX.

NO:04 IMPLEMENTATION OF JAVA INTERFACE FOR ADT STACK


AIM:
To design a Java interface for ADT Stack. Develop two different classes that implement this
interface, one using array and the other using linked-list. Provide necessary exception handling
in both the implementations.
ALGORITHM:
STEP 1: Create an interface which consists of three methods namely PUSH, POP
STEP 2: Create a class which implements the above interface to implement the concept of stack
through Array
STEP 3: Define all the methods of the interface to push any element, to pop the top element and
to display the elements present in the stack.
STEP 4: Create another class which implements the same interface to implement the concept of
stack through linked list.
STEP 5: Repeat STEP 4 for the above said class also.
STEP 6: In the main class, get the choice from the user to choose whether array
implementation or linked list implementation of the stack.
STEP 7: Call the methods appropriately according to the choices made by the user in the
previous step.
STEP 8: Repeat step 6 and step 7 until the user stops his/her execution.
PROGRAM:
import java.io.*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(top==10)
System.out.println("overflow"+top);
else
{
stack[++top]=item;
System.out.println("item pushed");
System.out.println( "value of top is"+top);
}
}
public void pop()
{
if(top<=0)
System.out.println("underflow");
else
{
stack[top]=top--;
System.out.println("item popped");
}
}
public void display()
{
for(int i=1;i<=top;i++)
{
System.out.println("element:"+stack[i]);
}
}
}
class liststack implements stackoperation
{
node top,q;
int count;
public void push(int i)
{
node n=new node(i);
n.link=top;
top=n;
count++;
}
public void pop()
{
if(top==null)
System.out.println("under flow");
else
{
int p=top.data;
top=top.link;
count--;
System.out.println("popped element:"+p);
}
}
void display()
{
for(q=top;q!=null;q=q.link)
{
System.out.println("the elements are:"+q.data);
}
}
class node
{
int data;
node link;
node(int i)
{
data=i;
link=null;
}
}
}
class Demo
{
public static void main(String args[])throws IOException
{
int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream(System.in);
do
{
try
{
System.out.println("----------------------------------");
System.out.println("1.Arraystack 2.liststack 3.exit");
System.out.println("-----------------------------------");
System.out.println("enter ur choice:");
int c=Integer.parseInt(in.readLine());
Astack s=new Astack();
switch(c)
{
case 1:
do
{
if(p==1)
break;
System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value to push:");
int i=Integer.parseInt(in.readLine());
s.push(i);
break;
case 2:
s.pop();
break;
case 3:
System.out.println("the elements are:");
s.display();
break;
case 4:
p=1;
continue;
}
}while(x!=0);
break;
case 2:
liststack l=new liststack();
do
{
if(t==1)
break;
System.out.println("LIST STACK:");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter your choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value for push:");
int a=Integer.parseInt(in.readLine());
l.push(a);
break;
case 2:
l.pop();
break;
case 3:
l.display();
break;
case 4:
t=1;
continue;
}
}while(x!=0);
break;
case 3:
System.exit(0);
}
}
catch(IOException e)
{
System.out.println("io error");
}
}
while(x!=0);
}
}

You might also like