Stack : A stack is an ordered list in
which all insertions and deletions are made at one end, called the top. Stack is
a linear data structure which works based on the strategy last-in-first out
(LIFO). A stack can have any
abstract data type as an element, but is characterized by only two
fundamental operations: push and pop. The push operation adds an
item to the top of the stack, hiding any items already on the stack, or
initializing the stack if it is empty It is a linear data structure which is
open for operations at only one end (both insertions and deletions are defined
at only one end). One natural example of stack, which arises in computer
programming, is the processing of procedure calls and their terminations. Stacks
are generally used to remember things in reverse. It finds a major use in
backtracking approaches. Some of the functions related to a stack are
Create ( ) :- Q;
Insertion(Q, e) :- updated Q;
Deletion (Q) :- Q, e;
Isfull(Q) :- Boolean;
Isempty(Q) :- Boolean;
Top(Q) :- e;
Destroy(Q)
It has to be noted that with respect to a stack, insertion and deletion
operations are, in general, called PUSH and POP operations respectively.
Following are the algorithms for some functions of stack.
Algorithm: Create
Output: S, Stack created
Method:
Declare S[SIZE] //Array of size=SIZE
Declare and Initialize T=0 //Top pointer to remember the
number of elements
Algorithm ends
Algorithm: Isempty
Input: S, stack
Output: Boolean
Method:
If (T==0)
Return (yes)
Else
Return (no)
If end
Algorithm ends
Algorithm: Isfull
Input: S, stack
Output: Boolean
Method:
If (T==SIZE)
Return (yes)
Else
Return (no)
If end
Algorithm ends
Algorithm: Push
Input: (1) S, stack; (2) e, element to be inserted; (3) SIZE, size of the
stack;
(4) T, the top pointer
Output: (1) S, updated; (2) T, updated
Method:
If (Isfull(S)) then
Print ('stack overflow')
Else
T=T+1;
S[T] =e
If end
Algorithm ends
Algorithm: Pop
Input: (1) S, stack;
Output: (1) S, updated; (2) T, updated (3) 'e' element popped
Method:
If (Isempty(S)) then
Print ('stack is empty')
Else
e = S[T]
T=T-1;
If end
Algorithm ends
Summary
This article helps you to learn about stack
with the help of an algorithm.