You are on page 1of 4

1

Stack
A stack is a linear data structure in which insertion and deletion of an element can occur only at one end and that end is often called the top of the stack. In case of stack the elements can only be removed in the opposite order from that in which they were added to the stack. Due to this stack is referred to as Last in First out (LIFO) data structure. We encounter different stacks in our daily life like a stack of trays, stack of coins and stack of books. In general, any situation in which we can only add or remove an object at top is a stack. If you want to remove an object which is not at the top, we must first remove all the objects that lie above it. Stack is the way to group things together by placing one thing on the top of another and then removing things one at a time from the top of the stack.

Stack of coins

stack of books

A stack can be display in different ways which are as following Upward, Downward, Left, Right 10 20 30 40

40 30 20 10 Upward

10 20 30 40 right

40 30 20 10 left

downward

Basic Stack Operations


The basic operations that can be performed on stack are push, pop and peek.

Push :
The push operation inserts an element at the top of the stack. After the push operation, the element that has been pushed onto the stack becomes the top. Each push operation increases the number of elements on stack by one Stack overflow: Before performing the push operation, we must ensure that there is enough space for the new element in the stack. If there is not enough space in the stack then the new element cannot be inserted in the stack and stack is in overflow state.

Pop
The pop operation on a stack is just the reverse of the push operation. In this operation, we remove the element from the top of the stack and return it to the user. If stack contains more than one element then after removing the top element from the stack, the next older element in the stack becomes the top. Each pop operation reduces the number of elements in a stack by one. Stack underflow: On removing the last elements, the stack becomes empty. If we now perform the pop operation on the empty stack then stack is in underflow state

Peek
In some situations , you may want to inspect the element at the top of a stack without actually removing it. This is done using the peek operation. The peek operation returns the element at the top of the stack without altering the stack. After performing this operation, there is no change in the number of elements in the stack. If the peek operation is performed on an empty stack then the stack is in underflow.

Representation of the stack


A stack can be represented in memory using several data structure but usually it is represented using one-dimensional array or singly linked list. The choice of representing stack as an array or a linked list depends upon whether the size of the stack is fixed or not. If the size of the stack is fixed then the array representation of stack is more efficient as compare to linked list representation of stack.

Array representation of stack


In order to represent stack as an array, we maintain 1. An one dimension array S 2. A variable TOP which keeps track of the index of the last inserted element. Initially when the stack is empty, TOP variable has a value 0 3. A variable MAXSIZE that contain a value which identifies the maximum number of elements that a stack S can store

PUSH :
Each time we push a new element onto the stack, the variable TOP is incremented by 1 before the element is inserted onto the stack and item is inserted at S[TOP] position in array . 5 4 3 2 1 5 4 3 2 1 10 5 4 3 top 2 20 10 50 5 4 40 30 top 3 20 2 1 10

top 1

1. Initially when the stack is empty, the value of TOP variable is 0 2. On performing the push operation the element with value 10 is inserted at the index 1 and index of this array element becomes the top of stack. 3. Similarly we can insert more elements in the stack

3 4. When TOP=5 then stack is full and if we now try to insert a new element then stack become overflow

ALGORITHM
PUSH(S,TOP,MAXSIZE,ITEM) DESCRIPTION: S is an array with MAXSIZE and variable TOP holds the array index of the element at top of the stack. Variable ITEM store the value that we want to insert . 1. [CHECK FOR STACK OVERFLOW] If TOP=MAXSIZE then Write Overflow Return [END IF] 2. TOP=TOP+1 [INCREMENT TOP] 3. S[TOP]=ITEM 4. STOP POP : On performing pop operation on the stack , the element from the top of the stack is removed. Once an element is popped from the stack the element is no longer available on the stack Top 5 4 3 2 1 50 40 30 20 10 5 Top 4 3 2 1 50 40 30 20 10 5 4 Top 3 2 1 50 40 30 20 10 5 4 3 top 2 1 50 40 30 20 10

1. Initially the value of TOP variable is 5 and top element is 50 2. On performing the pop operation, TOP is decremented by one and now it contain the index of the new top element (5-1=4). This makes 40 as top element . 3. Similarly we can pop more elements from the stack 4. When TOP=0 then stack is empty and if we now try to pop an element then stack become underflow

Algorithm POP(S,TOP) 1. [CHECK FOR UNDERFLOW STATE OF STACK] If TOP=0 then Write underflow Return [END OF IF STRUCTURE] 2. ITEM=S[TOP] 3. TOP=TOP-1 [DECREMENT TOP] 4. Write ITEM 5. Stop

PEEK : the peek operation return the element at the top of the stack without altering the stack. PEEK(S,TOP) 1. [CHECK FOR UNDERFLOW STATE OF STACK] If TOP=0 then Write underflow Return [END IF] 2. ITEM=S[TOP] 3. Write ITEM 4. Stop

You might also like