Saturday, February 7, 2009

Stacks

1. What is stacks?

A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support a read ahead (a 'peek' operation), which reads data without removing it. A stacks is a linear data structures that can be accessed only at one of its ends for storing and retrieving data. Such a stack resembles of a shuttlecock in a tube. A last shuttle put on the top of the tube will be the first out and the first in will be the last out. This structure is called LIFO (last in/first out). There are operation that can change its status and operations that check this status; a stack that is defined in terms of operations follows:

  • clear() - clear the stack
  • isEmpty() - check to see if the stack is empty
  • push(cock) - put the element cock on the top of the stack
  • pop() - take the topmost element from the stack
  • top(cock) - return the topmost element in the stack without removing it

2. Illustration of a stack.

When we insert data into a stack, it is placed at the head of a queue. This means that when we remove data, it will be in reverse order. Adding 1,2,3,4 will return 4,3,2,1. Stacks aren't the most frequently used data structure, but they are extremely useful for certain tasks.3
2
1
4
3
2
1



Figure 1.1 Stack before push operation, and stack after

4
3
2
1
3
2
1



Figure 1.2 Stack before pop operation, and stack after

Java provides a stack implementation, in the form of java.util.Stack. Stack's are a subclass of java.util.Vector, and share some similarities. A Vector, after all, is a queue, and a Stack is an ordered LIFO queue.[http://www.javacoffeebreak.com/faq/faq0037.html]


3. Reference:

DATA STRUCTURES AND ALGORITHMS IN JAVA
Second Edition
Adam Drozdek



No comments:

Post a Comment