|
|
package asd_library.stack;
import java.util.*;
/**
* Implementation of the abstract data type stack
*/
public class Stack implements Stack_adt, Cloneable{
private java.util.LinkedList list= new java.util.LinkedList();
public Stack() {
}
/**
* Verifies if the stack is empty.
* @return true if the stack is empty, false otherwise.
*/
public boolean isEmpty(){
return list.isEmpty();
}
/**
* Retrieves the item from the stack head but doesn't remove it.
* @return the object on the top of the stack.
*/
public Object top(){
return list.getFirst();
}
/**
* Retrieves an item and deletes it from the stack.
* @return the object deleted from the stack.
*/
public Object pop(){
return list.removeFirst();
}
/**
* Stores an item on the stack.
* @param el the object to store.
*/
public void push(Object el){
list.addFirst(el);
}
/**
* Returns a string representation of the stack.
* The string representation consists of a list of the collection's elements
* in the reverse order they are returned by its iterator, enclosed in square brackets
* ("[]"). Adjacent elements are separated by the characters ", " (comma and
* space). Elements are converted to strings as by String.valueOf(Object).
* @return a <b>String</b> that represents the queue.
*/
public String toString(){
String str = list.toString();
/*char[] carray = str.toCharArray();
for (int old = str.length()-2, nu=1;
old >=1;
old--, nu++)
{
carray[nu] = str.charAt(old);
}
str = new String(carray);*/
return str;
}
/**
* Returns a shallow copy of this Stack. (The elements themselves are not cloned.)
* @return a shallow copy of this Stack instance.
*/
public Object clone(){
try{
Stack s = (Stack)super.clone();
s.list = (java.util.LinkedList) list.clone();
return s;
}catch(CloneNotSupportedException e){
//It mustn't happen
System.out.println("Cloning not allowed");
return this;
}
}
/**
* Compares the specified object with this stack for equality.
* Returns true if and only if the specified object is also a stack, both
* stacks have the same size, and all corresponding pairs of elements in the
* two stacks are equal. (Two elements e1 and e2 are equal
* if (e1==null ? e2==null : e1.equals(e2)).) In other words, two stacks are
* defined to be equal if they contain the same elements in the same order.
* @param q the object to be compared for equality with this stack
* @return true if the specified object is equal to this stack.
*/
public boolean equals(Stack s)
{
return list.equals(s.list);
}
/**
* Returns a list-iterator of the elements in this queue (in proper sequence)
* @return a ListIterator of the elements in this queue.
*/
public ListIterator listIterator(){
return this.list.listIterator();
}
}
|
|
|