Application of stacks, Study notes of English

It is all about stacks in data structure. You can use it as reference to your study.

Typology: Study notes

2018/2019

Uploaded on 11/26/2019

daryl-cabalbag
daryl-cabalbag ๐Ÿ‡ต๐Ÿ‡ญ

3 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures
Lecture 4 :
The Stack
0
Dr. Essam Halim Houssein
Lecturer, Faculty of Computers and Informatics,
Benha University
http://bu.edu.eg/staff/esamhalim14
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Application of stacks and more Study notes English in PDF only on Docsity!

Data Structures

Lecture 4 :

The Stack

Dr. Essam Halim Houssein

Lecturer, Faculty of Computers and Informatics,

Benha University http://bu.edu.eg/staff/esamhalim

The Stack

3.4. APPLICATIONS OF STACKS There are a number of applications of stacks: ๏ฑ Stack is internally used by compiler when we implement (or execute) any recursive function. ๏ฑ Stack is also used to evaluate a mathematical expression and to check the parentheses in an expression.

The Stack

The Stack

Programs compiled in modern high-level languages make use of a stack

for the procedure or function invocation in memory. When any procedure or

function is called, a number of words (such as variables, return address

and other arguments and its data(s) for future use) are pushed onto the

program stack. When the procedure or function returns, this frame of data

is popped off the stack.

The Stack

// PROGRAM TO FIND FACTORIAL OF A NUMBER, RECURSIVELY

Assignment (1) within lab

The Stack

3.4.2. RECURSION vs ITERATION

Recursion of course is an elegant programming technique, but not the best way to solve a problem, even if it is recursive in nature. This is due to the following reasons:

  1. It requires stack implementation.
  2. It makes inefficient utilization of memory, as every time a new recursive call is made a new set of local variables is allocated to function.
  3. Moreover it also slows down execution speed, as function calls require jumps, and saving the current state of program onto stack before jump.

The Stack

3.4.3. EXPRESSION Another application of stack is calculation of postfix expression. There are basically three types of notation for an expression (mathematical expression; An expression is defined as the number of operands or data items combined with several operators .)

**1. Infix notation

  1. Prefix notation
  2. Postfix notation**

The infix notation is what we come across in our general mathematics, where the operator is written in-between the operands. For example : A + B In the prefix notation the operator(s) are written before the operands, like: + A B In the postfix notation the operator(s) are written after the operands, like: A B +

The Stack

The Stack

Human beings are quite used to work with mathematical expressions in infix notation , which is rather complex. Whenever an infix expression consists of more than one operator, the precedence rules ( BODMAS ) should be applied to decide which operator (and operand) is evaluated first. But in a postfix expression operands appear before the operator , so there is no need for operator precedence and other rules.

The Stack

Notation Conversions

C++ Operator Precedence

The Stack Algorithm

  1. Push โ€œ(โ€ onto stack, and add โ€œ)โ€ to the end.
  2. Scan from left to right and repeat Steps 3 to 6 for each element until the stack is empty.
  3. If an operand is encountered, add it to Postfix_Stack (Q).
  4. If a left parenthesis is encountered, push it onto stack.
  5. If an operator โŠ— is encountered, then: (a) Repeatedly pop from stack, if not its precedence higher precedence than โŠ—. (b) Else Add โŠ— to stack.
  6. If a right parenthesis is encountered, then: (a) Repeatedly pop from stack until a left parenthesis is encountered. (b) Remove the left parenthesis.
  7. Exit.

The Stack

For, example consider the following arithmetic Infix to Postfix expression

I =(A+(BC-(D/E^F)G)H) Sr. No Symbol Scanned STACK Postfix_Stack (Q) 1 A ( A 2 + (+ A 3 ( (+( A 4 B (+( AB 5 * (+( AB 6 C (+(* ABC 7 - (+(- ABC* 8 ( (+(-( ABCD 9 D (+(-( ABCD**

The Stack

3.3.2 CONVERTING POSTFIX TO INFIX EXPRESSION

The rules to be remembered during infix to postfix conversion are:

  1. Count all characters
  2. Take a stack with size equal to number of characters
  3. Write the Postfix expression like this Left most Char at top or Right most Char at bottom sequentially)
  4. Fill up the stack
  5. Apply POP on all elements one by one starting from TOP of stack

The Stack

For, example consider the following arithmetic Postfix to Infix expression

A B C * + D E / F * -