• No results found

Stack

N/A
N/A
Protected

Academic year: 2020

Share "Stack"

Copied!
38
0
0

Loading.... (view fulltext now)

Full text

(1)

Stack

(2)

Introduction

A stack is a non primitive linear data

structure.

It is an ordered list in which addition of

(3)

Why stack is called LIFO?

All the deletion and insertion in a stack

is done from top of the stack.

The last added element will be the first

to be removed from the stack.

Example-plates in marriage party or

(4)

Stages of stack top during insertion and

deletion

20 20 13 20 13 68 20 13 68 20 13 20 Stack Empty First element second element

0 0 0 1 2 1 Third element 0 2 1 1 0

(5)

Stack implementation

Static

■ Using array

■ Size of stack has to be declared during program

design. Problem in insertion and deletion.

Dynamic

(6)

Stack Operations

Push

■ Process of adding a new element to the top

of stack. Top is increment after push

operation. In case of stack full called stack overflow condition.

Pop

■ Process of deleting an element from the

(7)

Stack as an abstract data type

■ Stack can also be defined as abstract data

types.

■ A stack of elements of any particular type is a

finite sequence of elements of that type together with the following operations:

■ Initialize stack to be empty

■ Determine whether stack is empty or not ■ Determine if stack is full or not

■ If stack is not full, then add a new node at the

end

■ If stack is not empty, then delete the node at its

(8)

Algorithm for Push operation

Push (stack[maxsize], item)

1. Set top=-1

2. Repeat steps 3to 5 until top<maxsize-1 3. Read item

4. Set top=top+1

(9)

Algorithm for Pop operation

Pop (stack[maxsize], item)

1.

Repeat steps 2to 4 until top>=0

2.

Set item =stack[top]

3.

Set top=top-1

4.

Print, no deleted is, item

(10)

Drawbacks of sequential

representation of stack data structure

Finite capacity of the stack

Checking for STACK_FULL condition

(11)

Linked Stack

Linked stack is a linear list of elements

commonly implemented as a singly

(12)

Linked Stack

C B A

a b c c b a

Top

Top

(13)

Linked stack operations

b

d c b a

c b a

Top

Push ‘d’ into stack

Top Top

a Top

Top

(14)

Algorithm for Push operation on a

linked stack

Push_linkstack(Top, Item)

1.

Call GETNODE(x)

/allot a node of the desired str. And

address of the node viz. x/

2

Data(x)=Item

3

Link(x)=Top

(15)

Algorithm for Pop operation on a

linked stack

Pop_linkstack(Top, Item)

1. If (top=0)

Call Linkstack_empty /linked stack is empty/

2 Else

Temp=Top

Item=Data(Top) Top=Link(Top)

(16)

Applications

■ Stack frame

■ Reversing a string

■ Calculation of postfix expression

Note:-(OPERATOR PRECEDENCE) ^

(17)

+,-Infix, Postfix, Prefix

■ Infix

■ Operator is written in-between the operands. A+B

■ Prefix

■ Operator is written before the operands, also

called polish notation. +AB

■ Postfix

■ Operators are written after the operands, also

(18)

Rules for infix to postfix conversion

■ Parenthesize the expression starting from left

to right

■ During parenthesizing the expression, the

operands associated with operator having higher precedence are first parenthesized.

■ The sub expression which has been

converted into postfix is to be treated as single operand

■ Once the expression is converted to postfix

(19)

infix to postfix

(A+B)*C/D AB+*C/D AB+C*/D AB+C*D/

(A+B)*C/D+E^F/G

AB+*C/D+E^F/G

AB+*C/D+EF^/G

AB+C*/D+EF^/G

AB+C*D/+EF^G/

(20)

infix to prefix

A*B+C

*AB+C

+*ABC

A/B^C+D

A/^BC+D

/A^BC+D

+/A^BCD

A/B^C-D

? (A*B+(C/D))-F

(A*B+/CD)-F

(*AB+/CD)-F

+*AB/CD-F

(21)

Algorithm for converting infix to

postfix

Postfix(Q, P)

Suppose Q is an arithmetic expression in infix and equivalent postfix expression P

1. Push “(” into stack and add “)” 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

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) add Ø to stack

(b) Repeatedly pop from stack and add P each operator which has the same

(22)

Algorithm for converting infix

to postfix

6. If a right parenthesis is encountered then,

(a) Repeatedly pop from stack and add to P each operator until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to P.]

[end of IF structure]

[End of step 2 loop]

(23)
(24)

Algorithm for converting infix

to prefix

1. Reverse the input string

2. Examine the next element in the input

3. If it is operand, add it to the output string 4. If it is closing parenthesis, push it on stack 5. If it is an operator, then

1. If stack is empty, push operation on the stack

2. If the top of stack is closing parenthesis , push operator on

stack

3. Else pop the operator from the stack and add it to output

(25)

Algorithm for converting infix

to prefix

6. If it is a opening parenthesis, pop operator

from stack and add them to S until a closing parenthesis is encountered . Pop and discard the closing parenthesis.

7. If there is more input goto step 2

8. If there is no more input, unstack the

remaining operators and add them

(26)

(A+B*C)

■ Reverse (C*B+A)

(27)

Algorithm for evaluate a

postfix expression

1. Add a right parenthesis “)” at the end of P.

2. Scan P from left to right and repeat steps 3 and 4 for each

element of P until the “)” is encountered.

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 Ø A

c) Place the result of (b) back on stack

[end of if structure] [end of step 2 loop]

5. Set value equal to the top element on stack

(28)

Evaluate expression ab+c*d

if a=2, b=3, c=4 and d=5

a a

b

5 5

c

20 20

(29)

Binary Expression Tree

Binary expression tree does not contain

parenthesis the reason that for

evaluating an expression using

(30)

Conversion with expression

tree

Prefix->Infix

■ Create the expression tree from prefix ■ Run in order traversal on the tree

Prefix-> Postfix

(31)

Algorithm for creating expression

tree from a prefix expression

1. Reverse the prefix expression

2. Examine the next element in the input

3. If it is operand then

a) Create a leaf node

b) Copy the operand in data part c) Push node’s address on stack

4. If it is an operator, then

a) Create a node

b) Copy the operator in data part

(32)

Algorithm for creating expression

tree from a prefix expression

5.

If there is no more input, pop the

address from stack, which is the

(33)

-A-BC

CB-A-B C

-A

(34)

-Algorithm for creating expression

tree from a postfix expression

1. Examine the next element in the input

2. If it is operand then

a) Create a leaf node

b) Copy the operand in data part c) Push node’s address on stack

3. If it is an operator, then

a) Create a node

b) Copy the operator in data part

(35)

Algorithm for creating expression

tree from a postfix expression

4.

If there is more input goto step 1

5.

If there is no more input, pop the

address from stack, which is the

(36)

Number System

DTOB(n)

1. Creating empty stack

2. Repeat while(n!=0) pushing remainder

1. Set rem<- n%2

2. Call push( s, rem)

3. n <- n/2

3. Repeat while(! Is empty stack)

1. Rem=pop(S)

2. Display(rem)

(37)

Assignment 3

1. Evaluate the expression 5 6 2+*12 4 /- in

tabular form showing stack after every step.

2. Write a program to convert infix to postfix

and then evaluate the postfix expression.

3. Translate by inspection and hand, each infix

expression into its equivalent postfix expression:

1. (A-B)*(D/E)

(38)

Assignment 3

4. Consider the following Stack , where STK is

allocated N=6 memory cells:

STK: PPP,QQQ, RRR,SSS,TTT describe the stack as the following operations take place:

References

Related documents

There were no violations on the total number of extra duty hours, but the restriction on the minimum number of consecutive work days was not satisfied by the work schedule

Although their physical forms showed considerable deterioration, it is clear from the TR FT-IR spectra that the polymer still displays characteristic vibrations (see

Single Node Installation runs all Open Stack services in one node including Control the node, Network, Compute node requrements, Storage services. Setup Configuaration

Bulk Liquid - General Tanker Terminal Code Remarks.. 1 There is safe access between the tanker

The IPv6 client will use this address to connect to the IPv4 server and the dual stack node will send IPv4 packets when the IPv4-mapped IPv6 address is used.. Then, IPv6 client

protocols reduce data transfer time, protocol overhead, and.

Engativá / Minuto de Dios / Elaboración de productos alimenticios y de bebidas; pro- ductos textiles; productos químicos; transformación de la madera, excepto muebles Engativá /

A BSTRACT : (Contribution to the dragonfly, aquatic beetle and caddisfly fauna of the Jászság, Hungary (Odonata, Coleoptera: Hydradephaga and Hydrophiloidea, Trichoptera))..