Tuesday, August 30, 2005

Write a class in Java to implement a stack using the List Interface as the internal data structure. Create a subclass of Stack called UniqueStack which allows only unique elements to be inserted.


public class Node extends Object{
int data;
public Node(){} //constructor
public Node(int i){ //constructor: set data to i
data=i;
}
public int getData(){ //return int data type
return data;
}
public void setData(int i){
data=i;
}
}


import java.util.*;

public class LList extends AbstractSequentialList
implements List, Cloneable, java.io.Serializable
{
public transient Entry header = new Entry(null, null, null);
public transient int size = 0;

public LList() {
header.next = header.previous = header;
}
public Object removeFirst() {
Object first = header.next.element;
remove(header.next);
return first;
}
public void addFirst(Object o) {
addBefore(o, header.next);
}
public int size() {
return size;
}

public boolean add(Object o) {
addBefore(o, header);
return true;
}
// Positional Access Operations

public Object get(int index) {
return entry(index).element;
}

public void add(int index, Object element) {
addBefore(element, (index==size ? header : entry(index)));
}
public Object remove(int index) {
Entry e = entry(index);
remove(e);
return e.element;
}

public Entry entry(int index) {
if (index < 0 index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}

// Search Operations

public int indexOf(Object o) {
int index = 0;
if (o==null) {
for (Entry e = header.next; e != header; e = e.next) {
if (e.element==null)
return index;
index++;
}
} else {
for (Entry e = header.next; e != header; e = e.next) {
if (o.equals(e.element))
return index;
index++;
}
}
return -1;
}

public ListIterator listIterator(int index) {
return new ListItr(index);
}

public class ListItr implements ListIterator {
public Entry lastReturned = header;
public Entry next;
public int nextIndex;
public int expectedModCount = modCount;

ListItr(int index) {
if (index < 0 index > size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
if (index < (size >> 1)) {
next = header.next;
for (nextIndex=0; nextIndex<index; nextIndex++)
next = next.next;
} else {
next = header;
for (nextIndex=size; nextIndex>index; nextIndex--)
next = next.previous;
}
}

public boolean hasNext() {
return nextIndex != size;
}

public Object next() {
checkForComodification();
if (nextIndex == size)
throw new NoSuchElementException();

lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.element;
}

public boolean hasPrevious() {
return nextIndex != 0;
}

public Object previous() {
if (nextIndex == 0)
throw new NoSuchElementException();

lastReturned = next = next.previous;
nextIndex--;
checkForComodification();
return lastReturned.element;
}

public int nextIndex() {
return nextIndex;
}

public int previousIndex() {
return nextIndex-1;
}

public void remove() {
checkForComodification();
try {
LList.this.remove(lastReturned);
} catch (NoSuchElementException e) {
throw new IllegalStateException();
}
if (next==lastReturned)
next = lastReturned.next;
else
nextIndex--;
lastReturned = header;
expectedModCount++;
}

public void set(Object o) {
if (lastReturned == header)
throw new IllegalStateException();
checkForComodification();
lastReturned.element = o;
}

public void add(Object o) {
checkForComodification();
lastReturned = header;
addBefore(o, next);
nextIndex++;
expectedModCount++;
}

final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

public static class Entry {
Object element;
Entry next;
Entry previous;

Entry(Object element, Entry next, Entry previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}

public Entry addBefore(Object o, Entry e) {
Entry newEntry = new Entry(o, e, e.previous);
newEntry.previous.next = newEntry;
newEntry.next.previous = newEntry;
size++;
modCount++;
return newEntry;
}

public void remove(Entry e) {
if (e == header)
throw new NoSuchElementException();

e.previous.next = e.next;
e.next.previous = e.previous;
size--;
modCount++;
}
}

import java.io.*;

public class Stack{
LList list;
static int i;
public Stack(){
list=new LList();
i=0;
}
public void push(Node n){
list.addFirst(n);
}
public int isStackEmpty(){
if(list.size()<=0)
return 1;
else
return 0;
}
public Node pop(){
Node n=new Node();
n=(Node)list.removeFirst();
return n;
}
public void display(){
Node n;
System.out.println("The data is: ");
if(isStackEmpty()==0){
for(int i=0;i<list.size();i++){
n=(Node)list.get(i);
System.out.println("\t"+n.getData());
}
}
else
System.out.println("Error: No elements in stack");
}
}

import java.io.*;

public class UniqueStack extends Stack{
public void push(Node n){
//System.out.println("I am in UniqueStack");
int flag=0;
Node n1=new Node();

if(isStackEmpty()==0){
for(int i=0;i<list.size();i++){
n1=(Node)list.get(i);
if(n1.getData()==n.getData()){
flag=1;
break;
}
}
}
if (flag==1)
System.out.println("Error: The element already exists in the list");
else
super.push(n);
}
public void menu() throws IOException{
int choice=0;
while(true){
System.out.println("1) Push element into Stack");
System.out.println("2) Pop element from Stack");
System.out.println("3) Display Stack");
System.out.println("4) Exit program");
System.out.println("Enter your choice: ");
DataInputStream in=new DataInputStream(System.in);
choice=Integer.parseInt(in.readLine());
if(choice==1){

Node n=new Node(Integer.parseInt(in.readLine()));
push(n);
}
else
if(choice==2){
if(isStackEmpty()==0){
Node n=new Node();
n=(Node)pop();
System.out.println("Data poped from stack is: "+ n.getData());
}
else
System.out.println("Error: Stack is empty");
}
else
if(choice==3){
display();
}
else if(choice==4)
System.exit(1);
}
}
}


import java.io.*;

public class StackMain{
static UniqueStack us=new UniqueStack();
public static void main(String []args){
try{
us.menu();
}catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}
}
}

OUTPUT:

1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
1
2
1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
1
3
1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
1
4
1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
1
2
Error: The element already exists in the list
1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
2
Data poped from stack is: 4
1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
3
The data is:
3
2
1) Push element into Stack
2) Pop element from Stack
3) Display Stack
4) Exit program
Enter your choice:
4
Press any key to continue...