The Stack Data Structure

Table of Contents

1. The Stack Data Structure

1.1. Definition

A linear abstract data structure where the elements could be added or deleted only at one open end, called the top of the stack.

1.2. Algorithm for PUSH and POP Operations

1.2.1. Declaration

Let S be a stack of SIZE = 100 elements, and TOP is an integer to hold the index of the last inserted element into the stack. Initially, TOP = -1

1.2.2. Algorithm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
1. PROCEDURE PUSH (v)
2.    IF TOP == SIZE  1 THEN
3.        DISPLAY "STACK OVERFLOW"
4.        EXIT PUSH
5.    END IF
6.    TOP = TOP + 1 // INCREMENT THE TOP OF THE STACK BY 1
7.    S[TOP] = v // ASSIGN THE ELEMENT AT THE TOP OF THE STACK
8. END PROCEDURE PUSH

9. PROCEDURE POP ()
10.    IF S.TOP == -1 THEN
11.        DISPLAY STACK UNDERFLOW
12.        EXIT POP
13.    END IF
14.    v = S[TOP]
15.    TOP = TOP  1
16.    RETURN v
17. END PROCEDURE POP