Extends the Vector class. Used to create a simple last-in-first-out stack.
· An item is stored on a stack by ‘pushing’ it into the stack.
· An item may subsequently be ‘popped’ off the stack and used.
· A Stack object will grow in size as new items are pushed onto it.
Example:
import java.util.EmptyStackException;
import java.util.Stack;
public class StackDemo {
Stack s;
public StackDemo()
{
s = new Stack();
}
{
s = new Stack();
}
public static void main(String args[])
{
{
StackDemo objStackDemo = new StackDemo();
objStackDemo.pushItem("Harry");
objStackDemo.pushItem("Emaini");
objStackDemo.pushItem("Voldmort");
objStackDemo.pushItem("Snape");
objStackDemo.pushItem("Dumbuldore");
System.out.println();
objStackDemo.popItem();
objStackDemo.popItem();
objStackDemo.popItem();
objStackDemo.popItem();
objStackDemo.popItem();
try
{
{
objStackDemo.popItem();
} catch(EmptyStackException e) {
System.out.println("Exception : Empty Stack");
}
}
void pushItem(String str)
{
{
s.push(str);
System.out.println("Pushed : " + str);
System.out.println("Stack has : " + s);
}
void popItem()
{
{
String str = (String)s.pop();
System.out.println("Popped : " + str);
System.out.println("Stack has : " + s);
}
}
Queue Interface
public interface Queue extends Collection
– A collection designed for holding elements prior to processing.
– Queues typically order elements in a FIFO (first-in-first-out) manner.
– In a FIFO queue, all new elements are inserted at the tail of the queue.
0 comments :
Post a Comment