Saturday, 11 February 2012

Task 1 Description:Create a class LIST which represents a generalized list (By a sequential implementation) where insertions and deletions can be done arbitrarily at any point in the list. Implement all basic operations on it. (Insert, Delete, Isempty, Display etc). Specialize this class to represent STACK data structure (By inheriting the List class) and implement all basic operations (push, pop, Isempty, Display), on it

import java.util.ArrayList;

import java.util.Scanner;

class LIST // class List
{
ArrayList Al;
LIST()
{
Al=new ArrayList();
}
public boolean push(Object ele) // Stack push function
{
Al.add(ele);
System.out.println("The added element is "+ele);
return true;
}

public boolean isEmpty() // Stack is empty function
{
if(size()==-1)
return true;
else
return false;

}

public Object pop() // Stack pop function
{

String s="Sorry Stack is empty";
if(size()==-1)

return s;
else
return Al.remove(Al.size()-1);
}

public int size() // Stack size function
{
int n=Al.size();
if(n==0)
return -1;
else
return n;
}

public void display() // Stack display function
{

int n=Al.size();
if(Al.size()!=-1)
{
for(int i=0;i {
System.out.println(Al.get(n-1));
n=n-1;

}

}
else
System.out.println("Stack is empty");
}

}

class PS_Stack extends LIST // class Main
{
public static void main(String[] args)
{
LIST l=new LIST();
int n;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("\t*************************************************\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \tPlease choose your Option: \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t1 to push element in to Stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t2 to check Stack is empty \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t3 to Pop element from stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t4 to get size of the stack \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t5 to dipslay stack elements \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t6 to exit\t\t\t\t*\n\t*\t\t\t\t\t\t*");
System.out.println("\t*************************************************");
n=sc.nextInt();
switch (n)
{
case 1:
Object ole;
System.out.println("Enter an element to push in to stack :");
ole=sc.nextInt();
l.push(ole);
break;
case 2:
System.out.println(l.isEmpty());
break;
case 3:
System.out.println(l.pop());
break;
case 4:
System.out.println(l.size());
break;
case 5:
l.display();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Choose correct option");

}

}


}// End of main
}