Top Banner
CS302 - Data Structures using C++ Kostas Alexis Topic: The ADT Stack
47

CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Oct 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

CS302 - Data Structuresusing C++

Kostas Alexis

Topic: The ADT Stack

Page 2: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stack Concept

• Last-in, first-out (LIFO) property

• Last item placed on stack will be first item removed

• Items placed and removed on top of stack

• Analogies

• Books on a desk

• Dishes in a Cafeteria

• Boxes in an attic

A stack analogy

Page 3: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stack Concept

• Last-in, first-out (LIFO) property

• Last item placed on stack will be first item removed

• Items placed and removed on top of stack

• Analogies

• Books on a desk

• Dishes in a Cafeteria

• Boxes in an attic

A stack analogy

Add item

Page 4: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stack Concept

• Last-in, first-out (LIFO) property

• Last item placed on stack will be first item removed

• Items placed and removed on top of stack

• Analogies

• Books on a desk

• Dishes in a Cafeteria

• Boxes in an attic

A stack analogy

Add item

Page 5: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stack Concept

• Last-in, first-out (LIFO) property

• Last item placed on stack will be first item removed

• Items placed and removed on top of stack

• Analogies

• Books on a desk

• Dishes in a Cafeteria

• Boxes in an attic

A stack analogy

Remove item

Page 6: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stack Concept

• Last-in, first-out (LIFO) property

• Last item placed on stack will be first item removed

• Items placed and removed on top of stack

• Analogies

• Books on a desk

• Dishes in a Cafeteria

• Boxes in an attic

A stack analogy

Remove item

Page 7: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stacks are heavily used in CS

• Examples?

Page 8: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Stacks are heavily used in CS

• Reverse a word

• Undo mechanism in text editors

• Backtracking – when you need to access the most recent data element in a serious of elements

• Function Call

• A stack is used to keep information about the active functions or subroutines

• Language processing:

• Space for parameters and local variables is created internally using a stack

• Compilet’s syntax check for matching braces is implemented by using a stack

• Support for recusion

Page 9: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

Restricts access “from top”

Page 10: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

Restricts access “from top”

If one wants to access

the 6th- from top entry?

Page 11: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 12: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 13: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 14: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 15: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 16: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 17: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 18: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 19: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 20: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

pop

If one wants to access

the 6th- from top entry?

Page 21: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

A stack analogy

peek

If one wants to access

the 6th- from top entry?

Get a copy of

Page 22: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Collection of items in reverse chronological order with the same data type

• ADT Stack operations

• Add a new item to the stack: push(ItemType newEntry)

• Remove item that was added most recently: pop()

• Retrieve item that was added most recently: ItemType peek()

• Determine whether a stack is empty: boolean isEmpty()

Page 23: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Collection of items in reverse chronological order with the same data type

• ADT Stack operations

• Add a new item to the stack: push(ItemType newEntry)

• Remove item that was added most recently: pop()

• Retrieve item that was added most recently: ItemType peek()

• Determine whether a stack is empty: boolean isEmpty()

Only returns a copy – the

item is not removed from

the stack

Page 24: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Collection of items in reverse chronological order with the same data type

• ADT Stack operations

• Add a new item to the stack: push(ItemType newEntry)

• Remove item that was added most recently: pop()

• Retrieve item that was added most recently: ItemType peek()

• Determine whether a stack is empty: boolean isEmpty()

Stack

+isEmpty(): boolean

+push(newEntry: ItemType): boolean

+pop(): boolean

+peek(): ItemType

Page 25: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

The ADT Stack

• Collection of items in reverse chronological order with the same data type

• ADT Stack operations

• Add a new item to the stack: push(ItemType newEntry)

• Remove item that was added most recently: pop()

• Retrieve item that was added most recently: ItemType peek()

• Determine whether a stack is empty: boolean isEmpty()

/** @file StackInterface.h */

#ifndef STACK_INTERFACE_

#define STACK_INTERFACE_

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

#endif

Page 26: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Page 27: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Page 28: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jim

Page 29: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jess

Jim

Page 30: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jill

Jess

Jim

Page 31: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jane

Jill

Jess

Jim

Page 32: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Joe

Jane

Jill

Jess

Jim

Joe is at the top of the stack

Page 33: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jane

Jill

Jess

Jim

Joe is at the top of the stack

Joe is removed from the stack

Page 34: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jane

Jill

Jess

Jim

Joe is at the top of the stack

Joe is removed from the stack

Jane is at the top of the stack

Page 35: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);

stringStack->push(“Jess”);

stringStack->push(“Jill”);

stringStack->push(“Jane”);

stringStack->push(“Joe”);

std::string top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

top = stringStack->peek();

std::cout << top << “ is at the top of the stack \n”;

if(stringStack->pop())

std::cout << top << “ is removed from the stack \n”;

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

virtual ~StackInterface() { }

}; // end StackInterface

Jill

Jess

JimJoe is at the top of the stack

Joe is removed from the stack

Jane is at the top of the stack

Jane is removed from the stack

Page 36: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

Page 37: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions { [ ( ) ( ) ] ( ) } [ ( ] )

OK NOT OK

Page 38: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ) / 2 – f ] + 1 }

Page 39: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

• When open delimiter is encountered

• push it on the stack

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ) / 2 – f ] + 1 }

Page 40: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

• When open delimiter is encountered

• push it on the stack

• When close delimiter is encountered

• Check to see if it matches top of stack

• If yes, pop off top of stack

• If not, expression is not balanced

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ) / 2 – f ] + 1 }

Page 41: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

• When open delimiter is encountered

• push it on the stack

• When close delimiter is encountered

• Check to see if it matches top of stack

• If yes, pop off top of stack

• If not, expression is not balanced

• If braces are balanced

• Stack is empty when expression is done

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ) / 2 – f ] + 1 }

Page 42: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

• When open delimiter is encountered

• push it on the stack

• When close delimiter is encountered

• Check to see if it matches top of stack

• If yes, pop off top of stack

• If not, expression is not balanced

• If braces are balanced

• Stack is empty when expression is done

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ) / 2 – f ] + 1 }

Page 43: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

• When open delimiter is encountered

• push it on the stack

• When close delimiter is encountered

• Check to see if it matches top of stack

• If yes, pop off top of stack

• If not, expression is not balanced

• If braces are balanced

• Stack is empty when expression is done

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ] / 2 – f ) + 1 }

Page 44: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack

• Checking for balanced expressions

• Scan expression:

• Discard characters that are not delimiters

• When open delimiter is encountered

• push it on the stack

• When close delimiter is encountered

• Check to see if it matches top of stack

• If yes, pop off top of stack

• If not, expression is not balanced

• If braces are balanced

• Stack is empty when expression is done

{ [ ( ) ( ) ] ( ) } [ ( ] )

a { b [ c ( d + e ] / 2 – f ) + 1 }

Page 45: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack (of characters)

• Checking for balanced expressions

// Returns true if the given characters, open and close, form a

// pair of parentheses, brackets, or braces

bool isPaired(char open, char close)

{

return (open == ‘(’ && close == ‘)’) ||

(open == ‘[’ && close == ‘]’) ||

(open == ‘{’ && close == ‘}’);

}; // end isPaired

Helper method

Page 46: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Using the ADT Stack (of characters)

• Checking for balanced expressions

bool checkBalance(string expression)

{

Stack<char> openDelimiterStack = new Stack<char>();

int characterCount = expression.length();

bool isBalanced = true;

int index = 0;

char nextCharacter = ‘ ’;

// Returns true if the given characters, open and close, form a

// pair of parentheses, brackets, or braces

bool isPaired(char open, char close)

{

return (open == ‘(’ && close == ‘)’) ||

(open == ‘[’ && close == ‘]’) ||

(open == ‘{’ && close == ‘}’);

}; // end isPaired

while (isBalanced && (index < characterCount)) {

{

nextCharacter = expression.charAt(index);

switch (nextCharacter)

{

case ‘(’: case ‘[’: case ‘{’:

openDelimiterStack->push(nextCharacter)

break;

case ‘)’: case ‘]’: case ‘}’:

if (openDilimiterStack->isEmpty())

isBalanced = false;

else

{

char openDilimeter = openDilimiterStack->peek();openDelimiterStack->pop();

isBalanced = isPaired(openDilimiter,nextCharacter);

} // end if

break;

default:

break;

} // end switch

index++;

} // end while

if (!openDilimeterStack->isEmpty())

isBalanced = false;

return isBalanced;

} // end checkBalance

Page 47: CS302 – Data Structures · •Undo mechanism in text editors •Backtracking –when you need to access the most recent data element in a serious of elements •Function Call •

Thank you