Stack Compiled Note
Stack Compiled Note
What is a Stack?
A stack is a simple data structure used for storing data (similar to Linked Lists). In a stack, the order in
which the data arrives is important. A pile of plates in a cafeteria is a good example of a stack. The plates
are added to the stack as they are cleaned and they are placed on the top. When a plate, is required it is
taken from the top of the stack. The first plate placed on the stack is the last one to be used.
Definition: A stack is an ordered list in which insertion and deletion are done at one end, called top. The
last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First
in Last out (FILO) list.
Special names are given to the two changes that can be made to a stack. When an element is inserted in
a stack, the concept is called push, and when an element is removed from the stack, the concept is
called pop. Trying to pop out an empty stack is called underflow and trying to push an element in a full
stack is called overflow. Generally, we treat them as exceptions. As an example, consider the snapshots
of the stack.
A stack is a container of objects that are inserted and removed according to the last in first-out (LIFO)
principle. Objects can be inserted into a stack at any time, but only the most-recently inserted (that is,
“last”) object can be removed at any time. The name “stack” is derived from the metaphor of a stack of
plates in a spring-loaded cafeteria plate dispenser. In this case, the fundamental operations involve the
“pushing” and “popping” of plates on the stack.
Example: Internet web browsers store the addresses of recently visited sites on a stack. Each time a user
visits a new site, that site’s address is “pushed” onto the stack of addresses. The browser then allows
the user to “pop” back to previously visited sites using the “back” button.
Consider a working day in the office. Let us assume a developer is working on a long-term project. The
manager then gives the developer a new task which is more important. The developer puts the long
term project aside and begins work on the new task. The phone rings, and this is the highest priority as it
must be answered immediately. The developer pushes the present task into the pending tray and
answers the phone.
When the call is complete the task that was abandoned to answer the phone is retrieved from the
pending tray and work progresses. To take another call, it may have to be handled in the same manner,
but eventually the new task will be finished, and the developer can draw the long-term project from the
pending tray and continue with that.
Stack ADT
The following operations make a stack an ADT. For simplicity, assume the data is an integer type. Main
stack operations
• void push(int data): Inserts data onto stack.
• int pop(): Removes and returns the last inserted element from the stack.
Implementation
There are many ways of implementing stack ADT; below are the commonly used methods.
If we use the convention that arrays begin at index 0, then we would initialize t to -1 (for an initially
empty stack), and we can use this value to also test if a stack is empty. In addition, we can use this
variable to determine the number of elements in a stack (t+1). In this array-based implementation of
a stack, we also must signal an error condition that arises if we try to insert a new element and the
array S is full. Given this error condition, we can then implement the main methods of a stack
as described below
Algorithm push(o):
if t + 1 = N then
t←t+1
S[t] ← o
Algorithm pop():
if t < 0 then
e ← S[t]
S[t] ← null
t←t−1
return e
Turning to the analysis of this array-based implementation of a stack, it should be clear that each of
the stack methods, push and pop, runs in a constant amount of time. This is because they only
involve a constant number of simple arithmetic operations, comparisons, and assignment
statements. That is, in this array-based implementation of the stack, each method runs in O(1) time.
The array implementation of a stack is both simple and efficient, and is widely used in a variety of
computing applications. Nevertheless, this implementation has one negative aspect; it must assume
a fixed upper bound N on the ultimate size of the stack. An application may actually need much less
space than this, in which case we would be wasting memory. Alternatively, an application may need
more space than this, in which case our stack implementation may “crash” the application with an
error as soon as it tries to push its (N + 1)st object on the stack. Thus, even with its simplicity and
efficiency, the array-based stack implementation is not always ideal.
Fortunately, there is another implementation, which is to use a linked list, discussed later. Such an
implementation would not have a size limitation (other than that imposed by the total amount of
memory on our computer) and it would use an amount of space proportional to the actual number
of elements stored in the stack.
package arraystackdemo;
/*
* @author TEWODROS
*/
/*-------------------------------end of Stack
intergace-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------*/
package arraystackdemo;
/*
* @author TEWODROS
*/
if(top == a.length-1)
{ System.out.println("Stack is full");
return;
}
top++; // increment top
if( isEmpty() )
{ System.out.println("Stack is empty");
return null;
return item;
return a[top];
{ return top+1; }
if( isEmpty() )
System.out.println("Stack is empty");
else
for(int i=0;i<=top;i++){
System.out.print(a[i]+" ");
System.out.println("");
/*-------------------------------end of ArrayStack
class-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------*/
package arraystackdemo;
/**
* @author TEWODROS
*/
/*
*/
Object item;
stk.push('B');
stk.push('C');
System.out.println(" the stack elements are:");
stk.display();
stk.display();
stk.push('E');
stk.push('F');
stk.display();
stk.display();
stk.display();
Output:
the stack elements are:
ABC
size(): 3
C is deleted
AB
Stack is full
ABDE
E is deleted
ABDG
G is on top of stack
ABDG