Documente Academic
Documente Profesional
Documente Cultură
Stack is a
special
kind
of
linear
list.
A linear
A=(a1 , a2 , , an ) , where
' ai '
list
of
elements
is
usually
denoted
by
denotes the ith element of the list. Two operations that are
frequently performed on a linear list are insertion into and deletion from a list list. In case of an ordinary
linear list, these two operations may be performed at any position in the list.
Definition: A stack is an ordered collection of elements into which new elements may be inserted and
from which elements may be deleted at one end called the top of the stack.
Given a stack
element
ai1 ,
an
a1
ai
2 i n . Thus if the elements are continuously inserted into a stack, then it grows at one
end. And if elements are deleted from a stack, it shrinks at the same end.
Unlike the array, the definition of the stack provides for the insertion and deletion of items, so that a stack
is dynamic, constantly changing. A single end of the stack at which items are inserted and also deleted, is
called top of the stack.
B
A
B
A
B
A
Fig above shows the expansion of an initial empty stack. Initially the stack is empty. As the elements
A,B,C are inserted the stack grows continuously upward.
Now if we remove the elements, then C will be deleted first, then B and finally A.
From the very nature of a stack, it is clear that the first element removed from a stack has been the last
element inserted into the stack. For example, in our example the first element deleted was C, which was
the last one inserted into the stack. For this reasons stacks are sometimes called LAST-IN-FIRST-OUT
structure or LIFO structure.
Abstract Definition: A stack can also be defined as an abstract data type of type T is a sequence of
elements of type T together with the following operations.
Create(S): which creates S as an empty stack of elements of type T.
Isempty(S): which returns true if S is full else false.
Add(S) which inserts the element I onto structure S.
Delete(S): which removes the top of the stack element of S.
Implementation of Stack:
There are two different implementations of stacks
(i)
(ii)
(i)
Linear implementation using arrary: the simplest way to represent a stack is by using one
dimensional array of size MAXSIZE, where MAXSIZE is the maximum number of
allowable entries.
The first element is stored at stack[0], the second is stored at stack[1] and so on.
In addition to the one dimensional array stack which is used to hold the elements of the stack,
another variable top is required to keep track of the index of the last element inserted(i.e., the
topmost element);
Initially, when the stack is empty , top has a value of
Initialization of a stack:
void initialize(struct stack *s)
{
s top=-1;
}
{
printf ( );
return ;
}
else {
s top+1;
s item[ top]=x ;
return ;
}
}
Algorithm for deleting an element from a stack:
Algorithm pop(S)
/* Removes the top element from a stack which is represented by a vector S and returns this element.
Top is a pointer to the topmost element of the stack*
if (top=1)
Then write(Stack overflow)
exit
else
top top1
return(S[top+1])
end if
Implementation of Pop operation in C:
The possibility of underflow must be considered in implementing pop operation. Inadvertent attempt to
pop an element from an empty stack should be avoided.
pop( struct s )
{
if ( empty (s ))
{
printf ( );
exit (1);
else
{
x=s item[ s top];
s top=s top1;
return(x );
}
Calling of pop function from main The programmer can declare a variable I and write i=pop(&s);
C-code to check whether a stack is empty or not
isempty (struct s)
{
if ( s top=1)
return(1) ;
else
return(0);
}
Stacktop operation: This function returns the top element of a stack without removing it from the
stack.
stacktop(struct s )
{
if (isempty ( s))
{
printf ( );
exit (1);
}
else return(s items[s top]);
}
Application of Stacks:
(I) Postfix expression evaluation: consider the sum of two variables A and B. We think of applying the
operation + to the operands A and B and write the sum as A+B. This is our convensional way of
writing an expression, i.e., the operator is placed between the operands. This particular representation
is called infix expression.
There are two other alternative notations for expressing the sum of Ab and B using the symbols A , B
+ . These are
and
(i)
+ A B i.e., place the operator before the operands known as prefix notation.
(ii)
A B+
i.e., place the operator after the operands, known as postfix notation.
+BC
, as written in standard infix notation, requires knowledge of which the two operations + and
* is to be performed first.
Translation from infix to postfix notation:
There are two methods for converting an infix expression to its equivalent postfix expression.
i
ii
Manual method
Using stack
Manual method; Steps needed to convert an infix expression to a postfix expression are
(i) Fully parenthesize the expression considering the precedence of operators.
(ii) Move all the operators so that they replace their corresponding right parentheses.
iii Delete all parentheses.
For example, consider the following expression
ABC
When fully parenthesized, it yields ((A*B)-C). Now move all operators, so that they replace their
corresponding right parentheses.
( ( AB )C )
Now remove all parentheses which yields,
ABC
Example: Convert the following infix expression to its equivalent postfix expression
( A + B )( C + D )
Step1: fully parenthesize the expression and replace the right parentheses.
Step 2:
( ( A + B )(C + D ) )