Вы находитесь на странице: 1из 8

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 java.io.*; // 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 System.out.println("overflow"); else { stack[++top]=item; // allow to add elements to stack System.out.println("item pushed"); } } //implement the pop operation public void pop() { if(stack[top]<=0) //check the stack is empty or not System.out.println("underflow"); else { stack[top]=top--; //allow to delete the element from stack System.out.println("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 System.out.println("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); 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 sample { 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); } }

Вам также может понравиться