Page 67 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 67
Advanced Data Structure and Algorithms
Notes struct stk *next;
};
struct stk *stack;
void push(struct stk *p, T e)
{
struct stk *x;
x = new(stk);
x.element = e;
x.next = NULL;
p = x;
}
Here the stack full condition is checked by the call to new which would give an error if no memory
space could be allocated.
T pop(struct stk *p)
{
struct stk *x;
if (p == NULL)
printf(“Stack is empty”);
else
{ x = p;
x = x.next;
return(p.element);
}
boolean empty(sstruct stk *p)
{
if (p == NULL)
return(true);
else
return(false);
}
void initialize(struct stk *p)
{
p = NULL;
}
3.3 Applications of Stacks
There are numerous applications of the stack data structure in computer algorithms. It is used to
store return information in the case of function/procedure/subroutine calls. Hence, one would
find a stack in architecture of any Central Processing Unit (CPU). In this section, we would just
illustrate a few of them.
62 LOVELY PROFESSIONAL UNIVERSITY