Professional Documents
Culture Documents
Chapter 3.1
Chapter 3.1
Stack: Stack is a linear data structure that stores all elements in a specific sequence. Stack has only one
end (pointer) to perform operations on stack such as insertion, deletion and traversal. This end (pointer) is
called as stack top.
As stack has only one end, insertion and deletion performed only from one end i.e. stack top, so stack
behaves in LIFO manner. LIFO is Last In first Out that means last inserted element into a stack is removed
first from the stack.
Array representation:
In the below figure, stack is declared with maximum 3 elements. First inserted element is stored at the 0th
index position; second element is stored at second index position and so on.
Push operation: It is used to insert an element into a stack. Inserting an element requires increment of
stack top pointer by one index position. Before inserting an element, memory space availability inside stack
is checked. If space is available then stack top pointer is a incremented by one and new element is inserted
at the position indicated by stack top pointer. If space is not available then push operation is discarded.
Stack full: When stack contains maximum number of elements and stack top is initialize to array size-1
(max-1) then stack is said to be full.
Stack overflow: When stack is full and push operation is called then stack is said to be in overflow state.
Algorithm for push( ) operation:
Step 1 : Start (call to push function with argument value to be stored inside the stack)
Step 2 : Check for stack overflow state
If stack top pointer is initialize to max-1 position then
Display a message as ‘stack is full’ and return to calling function
Otherwise
Go to step 3
Step 3 : Increment stack pointer by one index position
Step 4 : Store new element value at the index position indicated by stack top
Step 5 : Return to calling function.
Pop operation: It is used to delete an element from stack. When pop operation is called an element
stored at the stack top position is deleted. Deleting an element from stack, decrements stack top pointer by
one index position.
Stack empty: When stack does not contain any element and stack top is initialize to -1 value then stack is
said to be empty.
Stack underflow: When stack is empty and pop operation is called then stack is said to be in underflow
state.
2. Operations on stack:
I) Initialize: This operation is used to initialize stack top to -1 index value.
II) Stack empty: This operation is used to check stack underflow.
III) Stack full: This operation is used to check stack overflow.
IV) Push: This operation is used to insert an element in a stack.
V) Pop: This operation is used to delete an element from stack.
VI) Display/traverse: This operation is used to visit each element from stack at least once.
Stack representation: