0% found this document useful (0 votes)
61 views34 pages

Stacks TS

The document discusses different data structures and their properties. It describes stacks as structures that allow insertions and removals only from the top. Stacks follow LIFO order. Linked lists allow insertions and removals anywhere. Binary trees enable efficient sorting and elimination of duplicates. The rest of the document focuses on stacks, providing examples of real-world stack applications and describing how stacks can be implemented and manipulated using arrays. Key stack operations include push, pop, peek and change. Recursive functions and problems like Tower of Hanoi are also discussed.

Uploaded by

Gamer Music
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views34 pages

Stacks TS

The document discusses different data structures and their properties. It describes stacks as structures that allow insertions and removals only from the top. Stacks follow LIFO order. Linked lists allow insertions and removals anywhere. Binary trees enable efficient sorting and elimination of duplicates. The rest of the document focuses on stacks, providing examples of real-world stack applications and describing how stacks can be implemented and manipulated using arrays. Key stack operations include push, pop, peek and change. Recursive functions and problems like Tower of Hanoi are also discussed.

Uploaded by

Gamer Music
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Classification

 Stacks
 Allow insertions and removals only at top of stack
 Queues
 Allow insertions at the back and removals from the front
 Linked lists
 Allow insertions and removals anywhere
 Binary trees
 High-speed searching and sorting of data and efficient
elimination of duplicate data items
Stacks

3
Introduction
 Stack is an important data structure which stores its elements in an ordered
manner.

 A stack is a linear data structure which uses the principle, i.e., the elements
in a stack are added and removed only from one end, which is called the
top.

 Hence, a stack is called a LIFO (Last-In, First-Out) data structure as the


element that is inserted last is the first one to be taken out.

4
Introduction
 Real life examples of stack:
 Suppose we have created stack of the book
 How books are arranged in the stack?
 Books are kept one above the other
 Books which are inserted first is taken out last. (brown)
 Book which is inserted lastly is served first. (light green)
 Suppose at your home you have multiple chairs then you put
them together to form a vertical pile. From that vertical pile, the
chair which is placed last is always removed first.
 Chair which was placed first will be removed
last.

5
Array representation of stacks
 In computer’s memory stacks can be represented as a linear array.
 Every stack has a variable TOP associated with it.
 TOP is used to store the address of the topmost element of the
stack. It is this position from where the element will be added or
deleted.
 There is another variable MAX which will be used to store the
maximum number of elements that the stack can hold.
 If TOP = NULL, then it indicates that the stack is empty and if
TOP = MAX -1, then the stack is full.

6
Array representation of stacks

7
Operations of Stack
 Push : inserting element onto stack.
 Pop : removing element from stack.
 Peep : returns the ith element from top element of the
stack.
 Change : changes the ith element from top of stack to the
mentioned element.

8
Visual Representation of Stack
 View 1: When stack is empty
 When stack is empty then it does not contain any element inside it.
Whenever stack is empty, the position of topmost element is -1.

9
Visual Representation of Stack
 View 2: When stack is not empty
 Whenever we add very first element then topmost position will be increment
by 1. After adding first element, top = 0.

 View 3: After deletion of 1 element top will be decremented by 1.

10
Visual Representation of Stack
 Position of top and its value:

 Values of stack and top:

11
Push Operation
 Procedure PUSH(S, TOP, X): This procedure inserts an
element X on the top of a stack which is represented by a
vector S containing N elements with a pointer TOP
denoting the top element in the stack.

Algorithm to PUSH an element in a stack

Step 1: IF TOP = N-1, then


PRINT “OVERFLOW”
Goto Step 4
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET S[TOP] = X
Step 4: END

12
Pop Operation
 Procedure POP(S, TOP): This procedure removes the
element from the stack which is represented by a vector S
and returns the element.

Algorithm to POP an element from a stack

Step 1: IF TOP = -1, then


PRINT “UNDERFLOW”
Goto Step 4
[END OF IF]
Step 2: SET TOP = TOP - 1
Step 3: Return S(TOP+1)
Step 4: END

13
Peek Operation
 Procedure PEEP(S, TOP, I): Given the vector S
(consisting of N elements) representing a sequentially
allocated stack, and a variable TOP denoting the top
element of the stack, this function returns the value of the
ith element from the top of the stack. The element is not
deleted by this function.

Algorithm to PEEP an element from a stack

Step 1: IF TOP – I < 0, then


PRINT “UNDERFLOW”
Goto Step 3
[END OF IF]
Step 2: Return S(TOP – I)
Step 3: END

14
Change Operation
 Procedure CHANGE(S, TOP, X, I): Given the vector S
(consisting of N elements) representing a sequentially
allocated stack, and a pointer TOP denoting the top
element of the stack, this procedure changes the value of
the ith element from the top of the stack to the value
contained in X.

Algorithm to CHANGE an element from a stack

Step 1: IF TOP – I < 0, then


PRINT “UNDERFLOW”
Goto Step 3
[END OF IF]
Step 2: S(TOP – I) = X
Step 3: END

15
16
Applications of Stack
 Expression conversion
 Expression evaluation
 Recursion

17
Expression conversion
 Conversion from infix to postfix expression
 Conversion from infix to prefix expression
 Conversion from prefix to infix expression
 Conversion from prefix to postfix expression
 Conversion from postfix to prefix expression
 Conversion from postfix to infix expression

18
Expressions
 Expressions is a string of operands and operators. Operands are some
numeric values and operators are of two types: Unary and binary operators.
Unary operators are ‘+’ and ‘-’ and binary operators are ‘+’, ’-’, ‘*’, ‘/’ and
exponential. In general, there are three types of expressions:
 Infix expression : operand1 operator operand2
 Postfix expression : operand1 operand2 operator
 Prefix expression : operator operand1 operand2

Infix Postfix Prefix


(a + b) ab + + ab
(a + b) * (c - d) ab + cd - * * + ab - cd
(a + b / e) * (d + f) abe /+ df + * * + a/be + df

19
Conversion from infix to postfix (without
parenthesis)

20
Conversion from infix to postfix (with
parenthesis)

21
Conversion from infix to prefix (with
parenthesis)
 Step 1: Reverse the infix expression and convert ‘(‘ to ‘)’ and ‘)’ to ‘(‘.
 Step 2: Read this reversed expression from left to right one character at a
time.
 Step 3: Rest of the steps remains same as in case of “conversion from infix
to postfix (with parenthesis)”.
 Step 4: After all the elements are popped, reverse the expression obtained in
prefix expression.

22
Expression Evaluation
 Evaluation of postfix expression
 Evaluation of prefix expression
 Evaluation of infix expression

23
Evaluation of postfix expression
 Step 1: If char read from postfix expression is an operand, push operand to
stack.
 Step 2: If char read from postfix expression is an operator, pop the first 2
operand in stack and implement the expression using the following
operations:
 pop(opr1) then pop(opr2)
 result = opr2 operator opr1
 Step 3: Push the result of the evaluation to stack.
 Step 4: Repeat steps 1 to steps 3 until end of postfix expression
 Finally, At the end of the operation, only one value left in the stack. The
value is the result of postfix evaluation.

24
Evaluation of postfix expression

25
Evaluation of prefix expression
 Step 1: Reverse the prefix expression.
 Step 2: Read this reversed prefix expression from left to right one character
at a time.
 Step 3: If char read from reversed prefix expression is an operand, push
operand to stack.
 Step 4: If char read from reversed prefix expression is an operator, pop the
first 2 operand in stack and implement the expression using the following
operations:
 pop(opr1) then pop(opr2)
 result = opr1 operator opr2
 Step 5: Push the result of the evaluation to stack.
 Step 6: Repeat steps 3 to steps 5 until end of reversed prefix expression
 Finally, At the end of the operation, only one value left in the stack. The
value is the result of prefix evaluation.

26
Recursion

27
Recursion

28
Recursion

 Here, it is required to push the intermediate calculations till the terminal condition is
reached. In the above calculation for 5!, Steps 1 to 6 are the push operations. The
subsequent pop operations will evaluate the value of intermediate calculations till
the stack is exhausted.
 Stack for parameter(s): To store the parameter with which the recursion is defined.
 Stack for local variable(s): To hold the local variable that are used within the
definition.
 Stack to store the return address.
29
Factorial with recursion using stack

30
Tower of Hanoi Problem
 The Tower of Hanoi puzzle is solved by moving all the disks to
another tower by not violating the sequence of the arrangements.

The rules to be followed by the Tower of Hanoi are -


 Only one disk can be moved among the towers at any given time.
 Only the "top" disk can be removed.
 No large disk can sit over a small disk.

 Algorithm
 Step 1 − Move n-1 disks from source to aux
 Step 2 − Move nth disk from source to dest
 Step 3 − Move n-1 disks from aux to dest
32
Example

33
THANK YOU!!

ANY QUESTIONS??

34

You might also like