Sunteți pe pagina 1din 8

Stack is one of the most important data structure commonly used in computer algorithms.

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

S=( a1 , a2 , , a n) , we may say that


is the element topmost element and element

a1

ai

is the bottommost element, and

is said to be on the top of the element

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.

Top(S) : which returns the top element of stack S.


Add and Delete operations for a stack are known as Push and Pop operations respectively.

Implementation of Stack:
There are two different implementations of stacks

(i)
(ii)

Linear implementation using Array


Linked list implementation

(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

and when the stack contains a single

element, top has a value of zero and so on.


Each time an element is inserted, the pointer is increased by one, before the element is placed on the
stack.
The pointer is decremented by one after each deletion.
Checking whether the stack is empty can be done by asking if (top <0)
Checking whether the stack is full can be done by asking if (top MAXSIZE1) .
C declaration of stack: To implement a stack in a computer we shall set up an array that will hold the
items. In C we can make declarations such as following , where MAXSTACK is a symbolic constant
giving the maximum size allowed for stack and item-type is the type describing the data that will be
put into the stack.
#define MAXSTACK 100
Typedef struct stack
{
item-type item[MAXSTACK];
int top;
};

Initialization of a stack:
void initialize(struct stack *s)
{
s top=-1;
}

Algorithm for inserting an element into a stack:


Algorithm PUSH(s,t)
/* Inserts an element to the top of the stack which is represented by an array S containing MAXSIZE
elements at most, and a pointer top pointing the top of the stack element*/
if(top MAXSIZE1
then write (Stack overflow);
return;
else
top top+1
S[top] t
/*end of algorithm*/
C-source code

void pus h (struct s , t )


{
if ( s top= MAXSIZE1)

{
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.

Therefore, the pop operation performs the following


(i)
(ii)
(iii)

if the stack is empty, print a message and halt execution


remove the top element from the stack.
return this element to the calling program/function

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.

NOTE: In infix notation, the operator comes before the operands.


In infix notation, the operator comes between the operands.
In postfix notation, the operator comes after the operands.
One of the disadvantage of infix notation is that we need to use parentheses to control the evaluation of
the operators. This leads to an evaluation method that used parentheses and two operator priority classes.
In the postfix and prefix notations, we do not need parentheses. Each provides only one evaluation rule.
While high level languages use the infix notation, such expressions can not be directly evaluated. Rather,
they must be analyzed to determine the order in which the expressions are to be evaluated. A common
technique is to convert them to the postfix notation before gathering the code to evaluate them.
Precedence of operators: consider the infix expression

+BC

A + BC . The evaluation of the expression

, 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 ) )

S-ar putea să vă placă și