Java Interface For ADT Stack: Ex. No: 5
Java Interface For ADT Stack: Ex. No: 5
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*/
/*IntStack.java*/
IntStack() {
this.stack = null;
}
public void push(int val) {
Node value = new Node(val);
value.next = this.stack;
this.stack = value;
}
/*StackArr.java*/
/*TestSt.java*/
Output:
Result:
Thus a Java interface for ADT Stack was designed and used for stack using array and the other
using linked-list.