0% found this document useful (0 votes)
115 views3 pages

Java Interface For ADT Stack: Ex. No: 5

The document describes designing a Java interface for an ADT stack and implementing it using both arrays and linked lists. It defines a Stack interface with push and pop methods. Then it creates IntStack and StackArr classes that implement the interface using a linked list and array respectively. A TestSt class is used to test pushing and popping values from both implementations and outputs the results.

Uploaded by

Praveen Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views3 pages

Java Interface For ADT Stack: Ex. No: 5

The document describes designing a Java interface for an ADT stack and implementing it using both arrays and linked lists. It defines a Stack interface with push and pop methods. Then it creates IntStack and StackArr classes that implement the interface using a linked list and array respectively. A TestSt class is used to test pushing and popping values from both implementations and outputs the results.

Uploaded by

Praveen Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Ex.

No: 5 Java interface for ADT Stack

Aim:
To design a Java interface for ADT Stack and use that for stack using array and the other using
linked-list.

Algorithm:

Step 1. Start
Step 2. Create an interface Stack with the declaration of 2 functions Push and Pop.
Step 3. Create a class IntStack which implements the interface Stack. Add the
definition for the functions Push and Pop which is implemented using
linked list.
Step 4. Create a class StackArr which implements the interface Stack. Add the
definition for the functions Push and Pop which is implemented using
array.
Step 5. Create a class TestSt. Create objects for IntStack and StackArr.Call the
corresponding push and pop functions.
Step 6. Display the outputs
Step 7. Stop.

Program:

/*Stack.java*/

public interface Stack{


public void push(int i);
public void pop();
}

/*IntStack.java*/

public class IntStack implements Stack {


public class Node {
Node next;
Object data;
public Node(Object data) {
this.data = data;
}
}
Node stack;

IntStack() {
this.stack = null;
}
public void push(int val) {
Node value = new Node(val);
value.next = this.stack;
this.stack = value;
}

public void pop() {


if (stack == null) {
System.out.println("No value");
}
else {
Node value = this.stack;
this.stack = this.stack.next;
System.out.println(“Popped value”+value.data);
}
}}

/*StackArr.java*/

public class StackArr implements Stack{


int stck[] = new int[10];
int tos;
// Initialize top-of-stack
public StackArr() {
tos = -1;
}
// Push an item onto the stack
public void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
public void pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
}
else
System.out.println("Popped value:"+stck[tos--]);
}}

/*TestSt.java*/

public class TestSt{


public static void main(String args[]){
IntStack stack=new IntStack();
StackArr stack1=new StackArr();
System.out.println("Push and pop using Stack array:");
stack.push(4);
stack.push(5);
stack.push(6);
stack.pop();
stack.pop();
stack.pop();
System.out.println("Push and pop using Stack Linked list:");
stack1.push(3);
stack1.push(2);
stack1.push(1);
stack1.pop();
stack1.pop();
stack1.pop();
}}

Output:

C:\Program Files\Java\jdk1.6.0\bin>javac Stack.java

C:\Program Files\Java\jdk1.6.0\bin>javac IntStack.java

C:\Program Files\Java\jdk1.6.0\bin>javac StackArr.java

C:\Program Files\Java\jdk1.6.0\bin>javac TestSt.java

C:\Program Files\Java\jdk1.6.0\bin>java TestSt


Push and pop using Stack array:
Popped value6
Popped value5
Popped value4
Push and pop using Stack Linked list:
Popped value:1
Popped value:2
Popped value:3

Result:
Thus a Java interface for ADT Stack was designed and used for stack using array and the other
using linked-list.

You might also like