Problem: 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.
//Import io package import [Link].*; // Create interface for stack operation interface stackoperation { //Define push and pop method for stack operation public void push(int i); public void pop(); } //End of interface //Create class for implementing stack operations class Astack implements stackoperation { //Declare array for stack: this stack using array int stack[]; int top; // variable for top element of stack Astack() // constructor for initializing stack variables stack[10] and top=0 { stack=new int[10]; // Fix the stack size as 10 top=0; } //implement a push operations public void push(int item)
{ if(stack[top]==10) // check the stack size, it should not above 10 elements [Link]("overflow"); else { stack[++top]=item; // allow to add elements to stack [Link]("item pushed"); } } //implement the pop operation public void pop() { if(stack[top]<=0) //check the stack is empty or not [Link]("underflow"); else { stack[top]=top--; //allow to delete the element from stack [Link]("item popped"); } } // implements display method for display stack elements public void display() { for(int i=1;i<=top;i++) //read all the elements from stack array [Link]("element:"+stack[i]); //print all the elements
} } //Create node for list class node { int data; node link; node(int i) { data=i; link=null; } } //create a list stack from stack interface class liststack implements stackoperation { node top,q; //create node for list int count; public void push(int i) { node n=new node(i); [Link]=top; top=n; count++; }
public void pop() { if(top==null) [Link]("under flow"); else { int p=[Link]; top=[Link]; count--; [Link]("popped element:"+p); } } void display() { for(q=top;q!=null;q=[Link]) { [Link]("the elements are:"+[Link]); } } } class sample { public static void main(String args[])throws IOException { int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream([Link]); do { try { [Link]("----------------------------------"); [Link]("[Link] [Link] [Link]"); [Link]("-----------------------------------"); [Link]("enter ur choice:"); int c=[Link]([Link]()); Astack s=new Astack(); switch(c) { case 1: do { if(p==1) break; [Link]("ARRAY STACK"); [Link]("[Link] [Link] [Link] [Link]"); [Link]("enter ur choice:"); ch=[Link]([Link]()); switch(ch) { case 1:[Link]("enter the value to push:");
int i=[Link]([Link]()); [Link](i); break; case 2: [Link](); break; case 3: [Link]("the elements are:"); [Link](); break; case 4: p=1; continue; } }while(x!=0); break; case 2: liststack l=new liststack(); do { if(t==1) break; [Link]("LIST STACK:"); [Link]("[Link] [Link] [Link] [Link]"); [Link]("enter your choice:");
ch=[Link]([Link]()); switch(ch) { case 1: [Link]("enter the value for push:"); int a=[Link]([Link]()); [Link](a); break; case 2: [Link](); break; case 3: [Link](); break;
case 4: t=1; continue; } } while(x!=0); break; case 3: [Link](0); }
} catch(IOException e) { [Link]("io error"); } } while(x!=0); } }