Unit 4: Stacks and Queues

Unit 4: Stacks and Queues Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland June 1, 2011...
10 downloads 0 Views 930KB Size
Unit 4: Stacks and Queues Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland

June 1, 2011

ENGI 4892 (MUN)

Unit 4

June 1, 2011

1 / 24

1

Stacks

1

Stacks in STL

1

Case study: exiting a maze

1

Queues

ENGI 4892 (MUN)

Unit 4

June 1, 2011

2 / 24

Stacks A stack is a list of items accessed only through one end of the list.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack. Stack operations:

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack. Stack operations: clear() Emptys the stack.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack. Stack operations: clear() Emptys the stack. isEmpty() Checks to see if the stack is empty.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack. Stack operations: clear() Emptys the stack. isEmpty() Checks to see if the stack is empty. push(el) Puts the element el on top of the stack.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack. Stack operations: clear() Emptys the stack. isEmpty() Checks to see if the stack is empty. push(el) Puts the element el on top of the stack. pop() Takes the top-most element off the stack.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Stacks A stack is a list of items accessed only through one end of the list. It is sometimes referred to as a LIFO structure, meaning last in / first out. A stack is sometimes visualized as growing upwards, like a real “stack” of trays in a cafeteria. Hence, the accessible end of the stack is referred to as the top of the stack. Stack operations: clear() Emptys the stack. isEmpty() Checks to see if the stack is empty. push(el) Puts the element el on top of the stack. pop() Takes the top-most element off the stack. topEl() Returns the top-most element, without removing it.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

3 / 24

Demonstration of stack operations:

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

Demonstration of stack operations:

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

Demonstration of stack operations:

Stacks are useful when we need to store data and then access it again in reverse order.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

Demonstration of stack operations:

Stacks are useful when we need to store data and then access it again in reverse order. e.g. Matching delimiters in C++.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

Demonstration of stack operations:

Stacks are useful when we need to store data and then access it again in reverse order. e.g. Matching delimiters in C++. In C++ the following delimeters must exist in matched pairs: ( ) [ ] { }

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

Demonstration of stack operations:

Stacks are useful when we need to store data and then access it again in reverse order. e.g. Matching delimiters in C++. In C++ the following delimeters must exist in matched pairs: ( ) [ ] { } Matching pairs of delimeters can be nested within each other.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

Demonstration of stack operations:

Stacks are useful when we need to store data and then access it again in reverse order. e.g. Matching delimiters in C++. In C++ the following delimeters must exist in matched pairs: ( ) [ ] { } Matching pairs of delimeters can be nested within each other. We can match a pair only after all the delimeters between them have been dealt with.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

4 / 24

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found:

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file)

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file;

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch);

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure;

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop();

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match failure;

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match failure; if stack is empty

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match failure; if stack is empty success;

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match failure; if stack is empty success; else

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match failure; if stack is empty success; else failure;

The following algorithm (given in pseudocode) matches delimeters using a stack to store each delimeter until its mate is found: delimeterMatching(file) while not end-of-file read character ch from file; if ch is ‘(’, ‘[’, or ‘{’ push(ch); else if ch is ‘)’, ‘]‘, or ‘}’ if stack is empty failure; mate = pop(); if ch and mate do not match failure; if stack is empty success; else failure; (Note that this algorithm does not handle comments appropriately.)

e.g. s=t[5]+u/(v∗(w+y));

Another example application of stacks involves the addition of very large numbers. The sum,

ENGI 4892 (MUN)

Unit 4

June 1, 2011

7 / 24

Another example application of stacks involves the addition of very large numbers. The sum, 234,123,574,345,123,050,433 + 1,122,344,556,413,666,990,000,121

ENGI 4892 (MUN)

Unit 4

June 1, 2011

7 / 24

Another example application of stacks involves the addition of very large numbers. The sum, 234,123,574,345,123,050,433 + 1,122,344,556,413,666,990,000,121 ...does not fit in an int.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

7 / 24

Another example application of stacks involves the addition of very large numbers. The sum, 234,123,574,345,123,050,433 + 1,122,344,556,413,666,990,000,121 ...does not fit in an int. This can be addressed by breaking each number into its numerals and putting these numerals on a stack.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

7 / 24

Another example application of stacks involves the addition of very large numbers. The sum, 234,123,574,345,123,050,433 + 1,122,344,556,413,666,990,000,121 ...does not fit in an int. This can be addressed by breaking each number into its numerals and putting these numerals on a stack. Addition then involves an operation on two stacks with one stack as an output (representing the result).

ENGI 4892 (MUN)

Unit 4

June 1, 2011

7 / 24

Another example application of stacks involves the addition of very large numbers. The sum, 234,123,574,345,123,050,433 + 1,122,344,556,413,666,990,000,121 ...does not fit in an int. This can be addressed by breaking each number into its numerals and putting these numerals on a stack. Addition then involves an operation on two stacks with one stack as an output (representing the result). First, each number is broken into numerals and these are pushed onto the stack in order of decreasing significance.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

7 / 24

addingLargeNumbers()

addingLargeNumbers() read the first number and push onto stack1;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop();

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty result += stack2.pop();

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty result += stack2.pop(); push the unit part of result onto output stack ;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty result += stack2.pop(); push the unit part of result onto output stack ; result = the 10’s part of result;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty result += stack2.pop(); push the unit part of result onto output stack ; result = the 10’s part of result; push result onto output stack if non-zero ;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty result += stack2.pop(); push the unit part of result onto output stack ; result = the 10’s part of result; push result onto output stack if non-zero ; pop numerals from output stack and display them ;

addingLargeNumbers() read the first number and push onto stack1; read the second number and push onto stack2; result = 0; while at least one stack is not empty if stack1 is not empty result += stack1.pop(); if stack2 is not empty result += stack2.pop(); push the unit part of result onto output stack ; result = the 10’s part of result; push result onto output stack if non-zero ; pop numerals from output stack and display them ; The application of addingLargeNumbers to the addition of 592 and 3784 is shown on the following slide...

Stacks in STL

STL defines a generic stack class called stack.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

10 / 24

Stacks in STL

STL defines a generic stack class called stack. By default, it is an adaptation of the doubly-ended queue class deque.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

10 / 24

Stacks in STL

STL defines a generic stack class called stack. By default, it is an adaptation of the doubly-ended queue class deque. However, it can be customized to use a DLL (represented in STL as a list) or an array (represented in STL as a vector) as its underlying implementation:

ENGI 4892 (MUN)

Unit 4

June 1, 2011

10 / 24

Stacks in STL

STL defines a generic stack class called stack. By default, it is an adaptation of the doubly-ended queue class deque. However, it can be customized to use a DLL (represented in STL as a list) or an array (represented in STL as a vector) as its underlying implementation: stack stack1 ;

ENGI 4892 (MUN)

// deque by d e f a u l t

Unit 4

June 1, 2011

10 / 24

Stacks in STL

STL defines a generic stack class called stack. By default, it is an adaptation of the doubly-ended queue class deque. However, it can be customized to use a DLL (represented in STL as a list) or an array (represented in STL as a vector) as its underlying implementation: stack stack1 ; stack stack2 ;

ENGI 4892 (MUN)

Unit 4

// deque by d e f a u l t // v e c t o r ( i . e . a r r a y )

June 1, 2011

10 / 24

Stacks in STL

STL defines a generic stack class called stack. By default, it is an adaptation of the doubly-ended queue class deque. However, it can be customized to use a DLL (represented in STL as a list) or an array (represented in STL as a vector) as its underlying implementation: stack stack1 ; stack stack2 ; stack stack3 ;

ENGI 4892 (MUN)

Unit 4

// deque by d e f a u l t // v e c t o r ( i . e . a r r a y ) // l i s t ( i . e . DLL )

June 1, 2011

10 / 24

STL defines all of the expected stack operations.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

STL defines all of the expected stack operations. However, pop only removes the top element—it does not return it.

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

STL defines all of the expected stack operations. However, pop only removes the top element—it does not return it. The following Stack class can be used to obtain the usual popping behaviour:

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

STL defines all of the expected stack operations. However, pop only removes the top element—it does not return it. The following Stack class can be used to obtain the usual popping behaviour: template class Stack : public stack {

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

STL defines all of the expected stack operations. However, pop only removes the top element—it does not return it. The following Stack class can be used to obtain the usual popping behaviour: template class Stack : public stack { public : T pop ( ) { T tmp = stack : : top ( ) ;

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

STL defines all of the expected stack operations. However, pop only removes the top element—it does not return it. The following Stack class can be used to obtain the usual popping behaviour: template class Stack : public stack { public : T pop ( ) { T tmp = stack : : top ( ) ; stack : : pop ( ) ;

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

STL defines all of the expected stack operations. However, pop only removes the top element—it does not return it. The following Stack class can be used to obtain the usual popping behaviour: template class Stack : public stack { public : T pop ( ) { T tmp = stack : : top ( ) ; stack : : pop ( ) ; return tmp ; } };

ENGI 4892 (MUN)

Unit 4

June 1, 2011

11 / 24

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time.

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time. It applies the following procedure:

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time. It applies the following procedure: Try moving right, left, down, and up

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time. It applies the following procedure: Try moving right, left, down, and up If a route beginning with any one of these fails, try a new route that starts in an untried direction

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time. It applies the following procedure: Try moving right, left, down, and up If a route beginning with any one of these fails, try a new route that starts in an untried direction

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time. It applies the following procedure: Try moving right, left, down, and up If a route beginning with any one of these fails, try a new route that starts in an untried direction A stack is used to remember untried positions and to organize the order in which these positions are attempted.

Case study: exiting a maze Consider a mouse trapped in a maze, trying to find the exit:

This mouse can only move right, left, down, or up—one step at a time. It applies the following procedure: Try moving right, left, down, and up If a route beginning with any one of these fails, try a new route that starts in an untried direction A stack is used to remember untried positions and to organize the order in which these positions are attempted.

The maze is implemented as a 2D array of char’s

The maze is implemented as a 2D array of char’s

1 = wall

The maze is implemented as a 2D array of char’s

1 = wall 0 = open

The maze is implemented as a 2D array of char’s

1 = wall 0 = open m = mouse’s initial position

The maze is implemented as a 2D array of char’s

1 = wall 0 = open m = mouse’s initial position e = exit (could be anywhere)

The maze is implemented as a 2D array of char’s

1 = wall 0 = open m = mouse’s initial position e = exit (could be anywhere) . = visited location

The maze is implemented as a 2D array of char’s

1 = wall 0 = open m = mouse’s initial position e = exit (could be anywhere) . = visited location We assume the boundaries of the array are either walls or the exit.

The following is the pseudocode for the mouse’s stack-based solution:

The following is the pseudocode for the mouse’s stack-based solution: exitMaze()

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack;

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell;

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ;

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours;

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure;

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure; else currentCell = stack.pop();

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure; else currentCell = stack.pop(); success;

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure; else currentCell = stack.pop(); success; Unvisited neighbours are pushed in the following order:

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure; else currentCell = stack.pop(); success; Unvisited neighbours are pushed in the following order: up, down, left, and right.

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure; else currentCell = stack.pop(); success; Unvisited neighbours are pushed in the following order: up, down, left, and right. The order in which neighbours are visited will be opposite: right, left, down, and up.

The following is the pseudocode for the mouse’s stack-based solution: exitMaze() initialize stack; currentCell = entryCell; while currentCell != exitCell mark currentCell as visited ; push onto stack currentCell’s unvisited neighbours; if stack is empty failure; else currentCell = stack.pop(); success; Unvisited neighbours are pushed in the following order: up, down, left, and right. The order in which neighbours are visited will be opposite: right, left, down, and up. The stack stores the row and column of each cell that remains to be explored.

Consider the following example:

Consider the following example:

Here is part of the C++ implementation: void Maze : : pushUnvisited ( int row , int col ) { if ( store [ row ] [ col ] == passage | | store [ row ] [ col ] == exitMarker ) { mazeStack . push ( Cell ( row , col ) ) ; } }

ENGI 4892 (MUN)

Unit 4

June 1, 2011

16 / 24

Here is part of the C++ implementation: void Maze : : pushUnvisited ( int row , int col ) { if ( store [ row ] [ col ] == passage | | store [ row ] [ col ] == exitMarker ) { mazeStack . push ( Cell ( row , col ) ) ; } } void Maze : : exitMaze ( ) { int row , col ; currentCell = entryCell ;

ENGI 4892 (MUN)

Unit 4

June 1, 2011

16 / 24

Here is part of the C++ implementation: void Maze : : pushUnvisited ( int row , int col ) { if ( store [ row ] [ col ] == passage | | store [ row ] [ col ] == exitMarker ) { mazeStack . push ( Cell ( row , col ) ) ; } } void Maze : : exitMaze ( ) { int row , col ; currentCell = entryCell ; while ( ! ( currentCell == exitCell ) ) {

ENGI 4892 (MUN)

Unit 4

June 1, 2011

16 / 24

Here is part of the C++ implementation: void Maze : : pushUnvisited ( int row , int col ) { if ( store [ row ] [ col ] == passage | | store [ row ] [ col ] == exitMarker ) { mazeStack . push ( Cell ( row , col ) ) ; } } void Maze : : exitMaze ( ) { int row , col ; currentCell = entryCell ; while ( ! ( currentCell == exitCell ) ) { row = currentCell . x ; col = currentCell . y ;

ENGI 4892 (MUN)

Unit 4

June 1, 2011

16 / 24

Here is part of the C++ implementation: void Maze : : pushUnvisited ( int row , int col ) { if ( store [ row ] [ col ] == passage | | store [ row ] [ col ] == exitMarker ) { mazeStack . push ( Cell ( row , col ) ) ; } } void Maze : : exitMaze ( ) { int row , col ; currentCell = entryCell ; while ( ! ( currentCell == exitCell ) ) { row = currentCell . x ; col = currentCell . y ; cout