DS Module 1_Stack Module 1 Stack notes/PPT BCS304 VTU
DS Module 1_Stack Module 1 Stack notes/PPT BCS304 VTU
DS Module 1_Stack Module 1 Stack notes/PPT BCS304 VTU
STACKS
Module 2
Stack
As
stack is an ordered list in which insertions (pushes) and deletions (pops) are
made at
one end called the top.”
A stack is a linear data structure that follows the Last in, First out principle (i.e. the
last
added elements are removed first).
•Given a stack S= (a0, ... , an-1), where a0 is the bottom element, an- is the top
1
element, and ai is on top of element ai-1, 0 < i < n.
Figure: Inserting and deleting elements in a stack
• As shown in above figure, the elements are added in the stack in the order A,
B, C, D, E, then E is the first element that is deleted from the stack and the
last element is deleted from stack is A.
•Since the last element inserted into a stack is the first element removed, a stack is
also
known as a Last-In-First-Out (LIFO) list.
•In programming terms, putting an item on top of the stack is called "push"
and
removing an item is called "pop".
ARRAY REPRESENTATION OF STACKS
•Stacks may be represented in the computer in various ways such as one-way linked
list
(Singly linked list) or linear array.
•Stacks are maintained by the two variables such as TOP and MAX_STACK_SIZE.
•TOP which contains the location of the top element in the stack. If TOP= -1, then
it
indicates stack is empty.
•MAX_STACK_SIZE which gives maximum number of elements that can be stored in
stack.
•Stack can represented using linear array as shown below
STACK OPERATIONS
Implementation of the stack operations as follows.
1. Stack Create
Stack CreateS(maxStackSize )::=
#define MAX_STACK_SIZE 100 /* maximum stack
size*/ typedef struct
{
int key;
/* other fields */
} element;
element Stack[MAX_STACK_SIZE];
int top = -1;
The element which is used to insert or delete is
specified as a structure that
2. Boolean IsEmpty(Stack)::= top < 0;
3. Boolean IsFull(Stack)::= top >= MAX_STACK_SIZE-1;
The IsEmpty and IsFull operations are simple, and is implemented directly in the
program push and pop functions. Each of these functions assumes that the variables stack
and top are global.
4. Push( )
•Function push checks whether stack is full.
•If it is, it calls stackFull( ), which prints an error message and
terminates
execution.
• When the stack is not full, increment top and assign item to
stack [top]. top=-1
void push(element item)
{ /* add an item to the global stack
*/ if (top >=
MAX_STACK_SIZE-1)
stackFull();
stack[++top] = item;
}
5. Pop( )
Deleting an element from the stack is called pop operation. The element is deleted only
from
the top of the stack and only one element is deleted at a
time. element pop ( )
{ /*delete and return the top element from the stack
*/ if (top == -1)
return stackEmpty(); /*returns an error key
*/
return stack[top--];
}
6. stackFull( )
The stackFull which prints an error message and terminates
execution.
void stackFull()
{
fprintf(stderr, "Stack is full, cannot add
element"); exit(EXIT_FAILURE);
}
7 stackempty( )
The stackempty which prints an error message and terminates execution.
void stackempty()
{
fprintf(stderr,"stack is empty\
n"); return;
}
STACK USING DYNAMIC ARRAYS
4. push()
Here the MAX_STACK_SIZE is replaced with
capacity
void push(element item)
{ /* add an item to the global stack */
if (top > = capacity-1)
stackFull(); Capacity=1
stack[++top] =
item; Top=-1
}
1 Module
4 2,DS,
⚫ The new code shown below, attempts to increase the capacity of the array
should be.
⚫ In array doubling, array capacity is doubled whenever it becomes necessary
void stackFull()
capacity *= 2;
1 Module
5 2,DS,
5.
pop( )
1 Module
6 2,DS,
Analysis
• Under the assumptions that memory may be allocated in O(1) time and
that a stack element can be copied in O(1) time, the time required by array doubling
is O(capacity). Initially, capacity is 1.
1 Module
7 2,DS,
STACK APPLICATIONS: POLISH NOTATION
⚫ Expressions: It is sequence of operators and operands that reduces to a single
value after evaluation is called an expression.
⚫ X= a/ b– c+ d*e– a*c
2. Infix Expression
1
3. Postfix Expression or Reverse Polish notation
8 Module 2,DS,
1.Infix Expression: In this expression, the binary operator is placed in-between
the operand.The expression can be parenthesized or un- parenthesized.
⚫ Example: A + B
1 Module
9 2,DS,
Precedence of the operators
•The first problem with understanding the meaning of expressions and statements is finding
out
the order in which the operations are performed.
Example: assume that a =4, b =c =2, d =e =3 in below expression
• The first answer is picked most because division is carried out before
subtraction, and multiplication before addition.
•If we wanted the second answer, write expression differently using parentheses to
change
2 Module
the order
0
of evaluation
2,DS, X= ((a / ( b – c + d ) ) * ( e – a ) * c
In C, there is a precedence hierarchy that determines the order in which operators are
evaluated.
2 Module
1 2,DS,
⚫ The operators are arranged from highest precedence to lowest. Operators
with highest precedence are evaluated first.
⚫ a * b / c % d / e is equivalent to ( ( ( ( a * b ) / c ) % d ) / e )
2 Module
2 2,DS,
In Stack precedence (ISP) and In Coming Precedence (ICP)
Evaluating Postfix Expressions
• We have used this notation for all of the expressions written thus far.