+ All Categories
Home > Engineering > Stack Operation In Data Structure

Stack Operation In Data Structure

Date post: 17-Aug-2015
Category:
Upload: divyeshkumar-jagatiya
View: 73 times
Download: 1 times
Share this document with a friend
Popular Tags:
29
By: Divyesh Jagatiya Hardik Gopani Keshu Odedara
Transcript
Page 1: Stack Operation In Data Structure

By:Divyesh JagatiyaHardik GopaniKeshu Odedara

Page 2: Stack Operation In Data Structure

Stacks are linear lists.All deletions and insertions occur

at one end of the stack known as the TOP.

Data going into the stack first, leaves out last.

Stacks are also known as LIFO data structures (Last-In, First-Out).

Quick Introduction

Page 3: Stack Operation In Data Structure

PUSH – Adds an item to the top of a stack.

POP – Removes an item from the top of the stack and returns it to the user.

PEEP – Find an item from the top of the stack.

Change(Update) – User can change the contents of the specific element.

Basic Stack Operations

Page 4: Stack Operation In Data Structure

Reversing Data: We can use stacks to reverse data.(example: files, strings)Very useful for finding palindromes.

Consider the following pseudo code:1) read (data)2) loop (data not EOF and stack not full)

1) push (data)2) read (data)

3) Loop (while stack not Empty)1) pop (data)2) print (data)

Stack Applications

Page 5: Stack Operation In Data Structure

Converting Decimal to Binary: Consider the following code 1) Read (number)2) Loop (number > 0)

1) digit = number modulo 22) print (digit)3) number = number / 2

// from Data Structures by Gilbert and Frozen

The problem with this code is that it will print the binarynumber backwards. (ex: 19 becomes 11001000 instead of 00010011.

)To remedy this problem, instead of printing the digit right away, we

canpush it onto the stack. Then after the number is done being

converted, we pop the digit out of the stack and print it.

Stack Applications

Page 6: Stack Operation In Data Structure

Evaluating arithmetic expressions.

Prefix: + a b Infix: a + b (what we use in grammar

school) Postfix: a b +

In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the expression to determine the order in which we evaluate it. A common technique is to convert a infix notation into postfix notation, then evaluating it.

Stack Applications

Page 7: Stack Operation In Data Structure

PUSH OPERATION

Page 8: Stack Operation In Data Structure

PUSH AlgorithmPUSH(S, Top, x)

Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit

Step 2: [Increment Top pointer By Value One]Top Top +1

Step 3: [Perform Insertion]s[Top] X

Step 4: [Finished]Exit

N

Top 0

Page 9: Stack Operation In Data Structure

PUSH AlgorithmPUSH(S, Top, x)

Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit

Step 2: [Increment Top pointer By Value One]Top Top +1

Step 3: [Perform Insertion]s[Top] X

Step 4: [Finished]Exit

N

Top A

Page 10: Stack Operation In Data Structure

PUSH AlgorithmPUSH(S, Top, x)

Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit

Step 2: [Increment Top pointer By Value One]Top Top +1

Step 3: [Perform Insertion]s[Top] X

Step 4: [Finished]Exit

N

Top

A

B

Page 11: Stack Operation In Data Structure

PUSH AlgorithmPUSH(S, Top, x)

Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit

Step 2: [Increment Top pointer By Value One]Top Top +1

Step 3: [Perform Insertion]s[Top] X

Step 4: [Finished]Exit

N

A

B

Top C

Page 12: Stack Operation In Data Structure

PUSH AlgorithmPUSH(S, Top, x)

Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit

Step 2: [Increment Top pointer By Value One]Top Top +1

Step 3: [Perform Insertion]s[Top] X

Step 4: [Finished]Exit

N Top

A

B

C

D

Page 13: Stack Operation In Data Structure

PUSH AlgorithmPUSH(S, Top, x)

Step 1: [Check For Stack Overflow]if Top >= Nthen write("Stack Overflow")Exit

Step 2: [Increment Top pointer By Value One]Top Top +1

Step 3: [Perform Insertion]s[Top] X

Step 4: [Finished]Exit

N Top

A

B

C

D

Stack Overflow

Page 14: Stack Operation In Data Structure

Pop OPERATION

Page 15: Stack Operation In Data Structure

POP Algorithm

N Top

A

B

C

D

POP(S , Top)

Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]

if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit

Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1

Step 3: [Return From Top Element Of The Stack]

Write(value)

Step 4: [Finished]Exit

Page 16: Stack Operation In Data Structure

POP Algorithm

N

A

B

C

POP(S , Top)

Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]

if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit

Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1

Step 3: [Return From Top Element Of The Stack]

Write(value)

Step 4: [Finished]Exit

Top

Page 17: Stack Operation In Data Structure

POP Algorithm

N

A

B

POP(S , Top)

Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]

if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit

Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1

Step 3: [Return From Top Element Of The Stack]

Write(value)

Step 4: [Finished]Exit

Top

Page 18: Stack Operation In Data Structure

POP Algorithm

N

A

POP(S , Top)

Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]

if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit

Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1

Step 3: [Return From Top Element Of The Stack]

Write(value)

Step 4: [Finished]Exit

Top

Page 19: Stack Operation In Data Structure

POP Algorithm

N

POP(S , Top)

Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]

if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit

Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1

Step 3: [Return From Top Element Of The Stack]

Write(value)

Step 4: [Finished]ExitTop 0

Page 20: Stack Operation In Data Structure

POP Algorithm

N

POP(S , Top)

Step 1: [Check For Stack Underflow Or Check Whether Stack Is Empty]

if (Top = 0) OR (Top = -1)then write("Stack Underflow")Exit

Step 2: [Decrement Top pointer / Remove The Top Information]Value S[Top]Top Top - 1

Step 3: [Return From Top Element Of The Stack]

Write(value)

Step 4: [Finished]ExitTop 0

Stack Underflow

Page 21: Stack Operation In Data Structure

PEEP OPERATION

Page 22: Stack Operation In Data Structure

PEEP Algorithm

N Top

A

B

C

DPEEP(S, Top, i)

Step 1: [Check For Stack Underflow]if (Top - i + 1) <= 0then write("Stack Underflow")Exit

Step 2: [Return The ith Element From The Top Of The Stack]X S(Top - i + 1)

Step 3: [Finished]Exit

Top D

Page 23: Stack Operation In Data Structure

CHANGE OPERATION

Page 24: Stack Operation In Data Structure

CHANGE Algorithm

N Top

A

B

C

DCHANGE(S, Top, X, i)

Step 1: [Check For Stack Underflow]if (Top - i + 1) < 0then write("Stack Underflow")Exit

Step 2: [Change The Element Value From The Top Of The Stack]S(Top - i + 1) X

Step 3: [Finished]Exit

Page 25: Stack Operation In Data Structure

CHANGE Algorithm

N Top

A

B

C

D1CHANGE(S, Top, X, i)

Step 1: [Check For Stack Underflow]if (Top - i + 1) < 0then write("Stack Underflow")Exit

Step 2: [Change The Element Value From The Top Of The Stack]S(Top - i + 1) X

Step 3: [Finished]Exit

Page 26: Stack Operation In Data Structure

POLISH NOTATION

Page 27: Stack Operation In Data Structure

Rules: Operands immediately go directly to output Operators are pushed into the stack (including parenthesis)

- Check to see if stack top operator is less than current operator- If the top operator is less, than push the current operator onto

stack- If the top operator is greater than the current, pop top operator

and push onto stack, push current operator onto stack- Priority 2: * /- Priority 1: + -- Priority 0: (

If we encounter a right parenthesis, pop from stack until we getmatching left parenthesis. Do not output parenthesis.

Infix to Postfix Conversion

Page 28: Stack Operation In Data Structure

A + B * C - D / E

Infix Stack Postfix

(a)A ( Ab)+ (+ Ac)B (+ ABd)* (+* ABe)C (+* ABCf)- (- ABC*+g)D (- ABC*+Dh)/ (-/ ABC*+Di)E (-/ ABC*+DEj)) ABC*+DE/-

Infix to Postfix Example

Page 29: Stack Operation In Data Structure

A + B * C - D / E

Label No. Postfix Stack

1 A A2 B A B3 C A B C4 * A B * C5 + A + B * C6 D A + B * C D7 E A + B * C D E8 / A + B * C D / E9 - A + B * C – D / E

Postfix Evaluation


Recommended