Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Module 1_Stack Module 1 Stack notes/PPT BCS304 VTU

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Data Structures

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

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

⚫ The array is used to implement stack, but the bound


(MAX_STACK_SIZE)
should be known during compile time.
⚫ The size of bound is impossible to alter during compilation hence this can
be overcome by using dynamically allocated array for the elements and
then increasing the size of array as needed.
⚫ Stack Operations using dynamic array
1. Stack CreateS( )::= typedef struct
{
int key; /* other fields */
} element;
element *stack;
MALLOC(stack,
1
sizeof(*stack)); int capacity= 1;
3
Module 2,DS,
2. Boolean IsEmpty(/tack)::= top < 0;

3. Boolean IsFull(/tack)::= top >= capacity-


1;

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

stack so that new element can be added into the stack.


⚫ Before increasing the capacity of an array, decide what the new capacity

should be.
⚫ In array doubling, array capacity is doubled whenever it becomes necessary

to increase the capacity of an array.

void stackFull()

REALLOC (stack, 2*capacity*sizeof(*stack));

capacity *= 2;

1 Module
5 2,DS,
5.
pop( )

In this function, no changes are made.


element pop ( )
{ /* delete and return the top element from the stack */
if (top == -1)
return stackEmpty();
return stack[top--];
}

1 Module
6 2,DS,
Analysis

• In the worst case, the realloc function needs to allocate 2*capacity*sizeof


(*stack) bytes of memory and copy capacity *sizeof (*stack)) bytes of memory from
the old array into the new one.

• 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

⚫ In above expression contains operators (+, –, / , *) operands (a, b, c, d, e).

⚫ Expression can be represented in in different format such


as
1. Prefix Expression or Polish notation

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

⚫ Here, A & B are operands and + is an operator

2. Prefix or Polish Expression: In this expression, the operator appears before


its operand.
⚫ Example: +A B

⚫ Here, A & B are operands and + is operand

3. Postfix or Reverse Polish Expression: In this expression, the operator


appears after its operand.
⚫ Example: A B +

⚫ Here, A & B are operands and + is an operator

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.

⚫ The associativity column indicates how to evaluate operators with the


same precedence.
⚫ For example, the multiplicative operators have left-to-right associativity.

⚫ This means that the expression

⚫ a * b / c % d / e is equivalent to ( ( ( ( a * b ) / c ) % d ) / e )

⚫ Parentheses are used to override precedence, and expressions are always


evaluated from the innermost parenthesized expression first

2 Module
2 2,DS,
In Stack precedence (ISP) and In Coming Precedence (ICP)
Evaluating Postfix Expressions

• The standard way of writing expressions is known as infix notation

because in it we place a binary operator in-between its two operands.

• We have used this notation for all of the expressions written thus far.

• Although infix notation is the most common way of writing

expressions, it is not the one used by compilers to evaluate expressions.

• Instead compilers typically use a parenthesis-free notation referred to as

postfix. In this notation, each operator appears after its operands.


The complete declarations are:

You might also like