0% found this document useful (0 votes)
42 views

Data Structure & Algorithm CS-102: Click To Edit Master Subtitle Style

The document discusses data structures and algorithms. It provides information on linear data structures like stacks and queues. It describes the properties and operations of stacks, including push and pop. Stacks can be represented using arrays or linked lists. The document also covers converting infix expressions to postfix notation and evaluating postfix expressions.

Uploaded by

Ramesh Mohapatra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Data Structure & Algorithm CS-102: Click To Edit Master Subtitle Style

The document discusses data structures and algorithms. It provides information on linear data structures like stacks and queues. It describes the properties and operations of stacks, including push and pop. Stacks can be represented using arrays or linked lists. The document also covers converting infix expressions to postfix notation and evaluating postfix expressions.

Uploaded by

Ramesh Mohapatra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structure & Algorithm CS-102

R K Mohapatra Click to edit Master subtitle style

4/15/12

Linear Data Structures

There are certain frequent situations in computer science when one wants to restrict insertion and deletions so that they can take place only at the beginning or at the end not in the middle.
Stack Queue
22

4/15/12

Stack

4/15/12

33

Stack

A Stack is a list of elements in which an element may be inserted or deleted only at one end, call top of the Stack Two basic operations are associated with Stack

Push : Insert an element into a stack Pop : Delete an element from a stack
44

4/15/12

Stack

Stores a set of elements in a


55

4/15/12

Last In First Out

top C B top B A A A Initial 1 2 3


4/15/12

top D C B A 4

top

66

C B A 5

Last In First Out

4/15/12

top C B top B A A A 7 6 5

top

77

Representation of Stack
Stack can be represented in two different ways : [1] Linear ARRAY [2] One-way Linked list
4/15/12 88

Array Representation of Stack


STACK A 1 B 2 TOP 3 C 3 4 5 6 7 8

MAXSTK 8

TOP = 0 or TOP = NULL will indicates that the stack is empty 4/15/12 99

PUSH Operation
Perform the following steps to PUSH an ITEM onto a Stack [1] If TOP = MAXSTK, Then print: Overflow, Exit [ Stack already filled] [2] Set TOP = TOP + 1 [3] Set STACK[TOP] = ITEM [Insert Item into new TOP Position] [4] Exit 4/15/12
1010

POP Operation
Delete top element of STACK and assign it to the variable ITEM [1] If TOP = 0, Then print Underflow and Exit [2] Set ITEM = STACK[TOP] [3] Set TOP = TOP -1 [4] Exit
4/15/12 1111

Linked List Representation of Stack


TOP Head CC

Top Of Stack

B B

Bottom Of Stac

A X A

4/15/12

1212

PUSH Operation

Push operation into the stack is accomplished by inserting a node into the front of the list [Insert it as the first node in the list] CC B B DD
1313

TOP A X A

PUSH DD into STACK


4/15/12

PUSH Operation
STACK before PUSH Operation TOP B A X B A STACK After PUSH Operation D D
4/15/12

CC

TOP

C C

B B
1414

A X A

PUSH Operation
[1] NEW->INFO = ITEM [2] NEW->LINK = TOP [3] TOP = NEW [4] Exit

4/15/12

1515

POP Operation

POP operation is accomplished by deleting the node pointed to by the TOP pointer [Delete the first node in the list]

4/15/12

1616

POP Operation
STACK before POP Operation TOP D D TOP CC
4/15/12

C C

B B

A X A

STACK After POP Operation B B


1717

A X A

POP Operation
[1] IF TOP == NULL Then Write Overflow and Exit [2] Set ITEM = TOP->INFO [3] Set TOP = TOP->LINK [4] Exit
4/15/12 1818

Arithmetic Expression; Polish Notation

Let Q be an arithmetic expression involving constant and operations Find the value of Q using reverse Polish (Postfix) Notation

4/15/12

1919

Polish Notation

Evaluate the following parenthesis-free arithmetic expression 2 3 + 5 * 2 2 12 / 6

Evaluate the exponentiation to obtain 8 + 5 * 4 12 /6 Evaluate Multiplication and Division


4/15/12 2020

Infix notation [Operator symbol is placed between two Operand] A + B , C D , E * F , G /H (A + B) * C and A + (B*C) Polish Notation [Operator symbol is placed before its operand] +AB, -CD, *EF , /GH

Polish Notation

Polish Notations are frequently called


4/15/12 2121

Polish Notation

Infix expression to Polish Notation

[ ] to indicate a partial translation (A+B)*C = [+AB]*C = *+ABC A+(B*C) = A+[*BC] = +A*BC


4/15/12 2222

Polish Notation

The fundamental property of Polish notation is that the order in which the operations are to be performed is completely determined by the positions of the operators and operand in the expression. One never needs parenthesis when writing expression in Polish notations
4/15/12 2323

Reverse Polish Notation

Operator symbol is placed after its two operand AB+, CD-, EF*, GC/

One never needs parenthesis to determine the order of the operation in any arithmetic expression written in reverse Polish notation.
4/15/12 2424

Computer usually evaluates an arithmetic expression written in infix notation in two steps: First Step: Converts the expression to Postfix notation Second Step: Evaluates the Postfix expression.
4/15/12 2525

Algorithm to find the Value of an arithmetic expression P Written in Postfix

Evaluation of Postfix Expression

[1] Add a right parenthesis ) at the end of P. [This act as delimiter] [2] Scan P from left to right and repeat Steps 3 and 4 for each element of P until the delimiter ) is encountered
4/15/12 2626

[3] If an operand is encountered, put it on STACK [4] If an operator is encountered, then (a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element (b) Evaluate B
4/15/12

Evaluation of Postfix Expression

A
2727

(c ) Place the result of (b) on STACK

Evaluation of Postfix Expression


[5] Set Value equal to the top element of STACK [6] Exit

4/15/12

2828

Example

P = 5, 6, 2, + , *, 12, 4, /, - [Postfix] Q = 5 * ( 6 + 2) 12 / 4 [Infix] P: 5,


(1) 6, 2, +, *, 12, 4, /, -, )

(2) (3) (4) (5) (6)

(7) (8) (9) (10)

4/15/12

2929

5, (1)

6,

2,

+,

*,

12,

4,

/,

-,

(2) (3) (4) (5) (6) (7) (8) (9) (10) Symbol Scanned STACK (1) (2) (3) (4) (5) (6) (7) (8) 5 6 2 + * 12 4 / 5 5, 5, 5, 40 40, 40, 40, 12 12, 3 4
3030

6 6, 8 2

4/15/12

Infix to Postfix

Q is an arithmetic expression written in infix notation , * , / , + , Three level of precedence

4/15/12

3131

Infix to Postfix

Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix notation

[1] Push ( onto STACK and ) to the end of Q [2] Scan Q from Left to Right and Repeat Steps 3 to 6 for each element of Q until the STACK is empty
4/15/12 3232

[3] If an operand is encountered, add it to P [4] If a left parenthesis is encountered, push it onto STACK [5] If an operator is encountered, then: (a) Repeatedly pop from STACK and to P each operator (on the top of STACK) which has same precedence as or higher 4/15/12 3333 precedence than .

(b) Add

to STACK

[6] If a right parenthesis is encountered, then (a) Repeatedly pop from the STACK and add to P each operator (on top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add it to P]
4/15/12 3434

Example

Q:A+(B*C(D/E F)*G)*H

A + ( B * C - ( D / E F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20

4/15/12

3535

A + ( B * C - ( D / E F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 Symbol STACK Scanned 1 A ( 2 + (+ 3 ( (+ 4 B 5 * 6 C 7 4/15/12 A A ( (* (* (Expression P 20

( (+ (+ (+ (+

A A A A

B B BC BC*

3636

A + ( B * C - ( D / E F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 Symbol STACK Scanned ( 9 D 10 / 11 E 12 13 F 14 ) 4/15/12


8

20 Expression P

(+ (+ (+ (+ (+ (+ (+

(((((((-

( ( (/ (/ (/ (/

ABC* ABC*D ABC*D ABC*DE ABC*DE ABC*DEF ABC*DEF/

3737

A + ( B * C - ( D / E F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 Symbol STACK Scanned


15 16 17 18 19 20

20 Expression P

* G ) * H )

(+ (+ (+ (+ (+

ABC*DEF/ A B C * D E F /G ABC*DEF/G** ABC*DEF/G** ABC*DEF/G*-H A B C * D E F / G * - H * +


3838

(-* (-*

4/15/12

You might also like