50
ASSIGNMENT PRESENTATION 1 STACK APPLICATION - PARENTHESES MATCHING TEAM – 3 Amirthavarshyny.K (13MX02) Darsini.R (13MX07) Deepa.M (13MX08) Hemashri.AVM (13MX15) Kalaivani.M (13MX18) Sindhu Bharathi.A (13MX44)

Team 3

Embed Size (px)

Citation preview

ASSIGNMENT PRESENTATION 1

STACK APPLICATION -PARENTHESES MATCHING

TEAM – 3• Amirthavarshyny.K (13MX02)• Darsini.R (13MX07)• Deepa.M (13MX08)• Hemashri.AVM (13MX15)• Kalaivani.M (13MX18)• Sindhu Bharathi.A (13MX44)

IntroductionOperations in stackStack Overflow & UnderflowPUSH AlgorithmPOP AlgorithmApplications of StackParentheses Checking or MatchingReferences

SYNOPSIS :

STACK :

It is an Abstract Data Type (ADT).

It is a linear data structure.

Follows LIFO (Last In First Out) data structure.

Only two operations are supported.

Real time examples:

a stack of plates, a stack of coins, batteries in

flashlight…

OPERATIONS:

PUSH:

- It is to insert an element into the

stack.

POP:

- It is to remove the element from the

stack.

STACK OVERFLOW :

If we insert an element into stack which

is FULL, the stack is OVERFLOW.

STACK UNDERFLOW :

If we delete an element from stack which

is EMPTY, the stack is UNDERFLOW.

PUSH ALGORTHIM:

push(array,n,top,item)

{

if(top=n)

then stack full

else

top=top+1

array[top]=item

}

The Stack is

EMPTY

TOP = 0

24 3

56

1

No. of elements, n=6

PUSH an element

2

1

4 3

56 TOP = 0

item = 1

PUSH an element

2

14 3

56

TOP = 1

item = 1

top=top+1

PUSH an element

2

4 3

56

1TOP = 1

item = 2

PUSH an element

24 3

56

1

TOP = 2

item = 2

top=top+1

PUSH an element

24

3

56

1

TOP = 3

item = 3

top=top+1

PUSH an element

2

4

3

56

1

TOP = 4

item = 4

top=top+1

PUSH an element

2

4

3

5

6

1

TOP = 5

item = 5

top=top+1

PUSH an element

2

4

3

5

6

1

TOP = 6

item = 6

Now, n=TOP indicating that

STACK IS FULL

PUSH an element

2

4

3

5

6

1

TOP = 6

item = 6

If we try to insert

another Element,

then n = 7.

Thus n = TOP.

This is STACK OVERFLOW.

A

POP ALGORITHM:

pop(array,top,item)

{

if(top=0)

then stack empty

else

item=stack[top]

top=top-1

}

POP an element

2

4

3

5

6

1

TOP = 5

top=top–1

LIFO

POP an element

2

4

3

5

61

TOP = 4

top=top–1

POP an element

2

4

3

5

61

TOP = 3

top=top–1

POP an element

2

4

3

5

6

1

TOP = 2

top=top–1

POP an element

2

43 5

6

1TOP = 1

top=top–1

POP an element

2

43 5

6

1

TOP = 0

top=top–1

POP an element

2

43 5

61

TOP = 0

Now, TOP = 0 indicating that

STACK IS EMPTY

POP an element

2

43 5

61TOP = 0

If we try to delete another element, then TOP < 0. This is STACK UNDERFLOW

APPLICATIONS:

Parentheses checking or matching

Expression evaluation

Recursion

PARENTHESES MATCHING:

It is one of the applications of stack

in which we check for correct matching

of the parentheses in the expression.

It checks for the correct nesting of the

parentheses in the expression.

ALGORITHM:

STEP 1 : Start

STEP 2 : Read the expression in

string format.

STEP 3 : Scan the expression from

left right.

STEP 4 : If any operator or operand is

encountered discard it.

STEP 5 : If left or open parentheses is

encountered PUSH it to the stack.

STEP 6 : If right or closed parentheses is

encountered POP top most element from

the stack.

STEP 7 : Continue the above three steps till

the sentinel character is encountered.

STEP 8 : If the stack is empty print

PARENTHESES MATCHED.

STEP 9 : Else PARENTHESES NOT

MATCHED.

STEP 10 : Stop.

Consider the following expression as an example.

{ E.g..World,[ ] })(This is anHello

EXAMPLE:

{ E.g..World,[ ] })(This is anHello

{ E.g..World,[ ] })(This is anHello

{ {

{ E.g..World,[ ] })(This is anHello

{

{ E.g..World,[ ] })(This is anHello

[ {[

{ E.g..World,[ ] })(This is anHello

{[

{ E.g..World,[ ] })(This is anHello

( {[(

{ E.g..World,[ ] })(This is anHello

{[(

{ E.g..World,[ ] })(This is anHello

{[(

{ E.g..World,[ ] })(This is anHello

{[

(

{ E.g..World,[ ] })(This is anHello

{[

{ E.g..World,[ ] })(This is anHello

{[

{ E.g..World,[ ] })(This is anHello

{

{ E.g..World,[ ] })(This is anHello

{

{ E.g..World,[ ] })(This is anHello

Here the stack is EMPTY. Then , the result is

PARENTHESES MATCHED.

If the expression is like below,

{ E.g..World,[ ] })This is anHello

{ E.g..World,[ })(This is anHelloor

the stack will NOT be empty. Then, the result is PARENTHESES NOT MATCHED.

JUST TRY THIS:

Check the matching of the parentheses in the

following expressions.

{2*(A–B)]+(A/B)^2}

(A+B*(C–D)+F])

(((A+B)*C+D-E)/(F+G)-(H+J)*(K-L))/(M-

N)

REFERENCES: DATA STRUCTURES , ALGORITHMS AND APPLICATIONS IN C++ (2ND

EDITION) – Sartraj sahani

DATA STRUCTURES AND ALGORITHMS - CONCEPTS, TECHNIQUES AND

APPLICATIONS – G A Vijayalakshmi Pai.

PROGRAMMING AND DATA STRUCTURES – Ashok N. Kamthane.

http://

davesquared.net/2008/07/brackets-braces-parenthesis-and-other.h

tml

http://www.cs.uml.edu/~

msheldon/102/2011fall/notes/07_stacks/index.html

http://financelab.nctu.edu.tw/DataStructure/lec08.pdf

http://www.cise.ufl.edu/~sahni/cop3530/slides/lec126.pdf

QUERIES

THANK YOU…….