View RSS Feed

My Java Tips

Implementing LIFO principle

Rate this Entry
by , 11-03-2011 at 12:04 PM (2140 Views)
Stack is a special type of Vector as it extends Vector class. Stack works on last-in-first-out (LIFO) principle. Stack class provides 5 operations to treat the vector in a special way.


Java Code:
boolean empty()
Object peek()
Object pop()
Object push(Object item)
int search(Object o)
Lets try push and pop operation on a Stack

Java Code:
Stack newStack = new Stack();
newStack.push("10");
newStack.push("20");
newStack.push("30");
newStack.push("40");
while(!newStack.empty())
{
System.out.println(newStack.pop());
}
Output:


40
30
20
10

Submit "Implementing LIFO principle" to Facebook Submit "Implementing LIFO principle" to Digg Submit "Implementing LIFO principle" to del.icio.us Submit "Implementing LIFO principle" to StumbleUpon Submit "Implementing LIFO principle" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments