Professional Documents
Culture Documents
National University
Page
As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems (or parts of problems) can be thought of as stack problems, queue problems, or other problems that can be solved with the help of another (perhaps user-defined) ADT
You need to decide what ADTs you need, build them, test them, then tackle your larger problem
National University
Page
Stack ADT
A stack is an ADT with specific properties and operations Stack properties are: Only one item can be accessed at a time Always the last item inserted is the first to come out (LIFO)
Stack Operations
Stack operations are: Push: Puts a new item on the top of the stack. Cant push into a full stack Pop: Removes the top item from stack. Cant pop an empty stack Top: Examine top item without modifying stack. Cant top an empty stack Init: Set existing stack to empty Full: Returns true if the stack is full Empty: Returns true if the stack is empty
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 4
This program assumes bool BracketMaching (string expression) that we have the { following routines char curr, temp; available Stack bracket_stack; for (int i=0; i< stg.size(); i++) bool empty (); { push (item); curr = stg[i]; char top (); if (curr == ( || curr == [ || curr == {) bracket_stack.push (curr); else if (curr == ) || curr == ] || curr == }) if (bracket_stack.empty() ) then return false; else if (bracket_stack.pop() does not match with curr) return false; end if end if } // for loop
return true; }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 7
Infix 6-1 (4 + 3) * 2 (2 + 3) / (9 - 4)
Its easy to evaluate postfix and prefix expressions using a stack in part because no brackets are necessary
National University
Page
prefix *+432 evaluate 4 7 + 3 2 * 14 2 Push 2 Push 3 Push 4 Pop 4 Pop 3 Apply + and get 7 Push 7 Pop 7 Pop 2 Apply * and get 14 Push 14
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University
postfix
43+2*
3 2 * 14 4 + 7 push 4 push 3 Pop 3 Pop 4 Apply addition and get 7 Push 7 Push 2 Pop 2 Pop 7 Apply * and get 14 Push 14
Page 10
Algorithm for evaluating postfix: Parse expression from left to right When an operand is encountered, push it onto the stack
When an operator is encountered, pop the top two operands, apply the operator, and push the result onto the stack
When the expression is completely scanned, the final result will be on the stack What if we want to convert an expression from infix to postfix? For example: 2 + 3 * [(5 - 6) / 2] becomes 2356-2/*+
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 11
Consider the following example that is scanned from left to right Expression: 2+3*[(56)/2]
Stack
Output:
Output:
+ is an operator and it is placed in the stack. Note there is no operator on the top of the stack with priority >= the + operator
Page 13
National University
Expression: 2+3*[(56)/2]
Stack
Output:
23
Expression: 2+3*[(56)/2]
Stack
Output:
23
* +
A.R. Hadaegh Dr. Ahmad R. Hadaegh
* is an operator and it is placed in the stack without taking anything out of the stack. Note that * has higher priority than +
Page 14
National University
Expression: 2+3*[(56)/2]
Stack
Output:
23
[ * +
Left brackets, no matter what type of bracket it is, it is placed in the stack
Expression: 2+3*[(56)/2]
Stack ( [ * +
Output:
23
National University
Page
15
Expression: 2+3*[(56)/2]
Stack
Output:
235
( [ * +
Expression: 2+3*[(56)/2]
Stack Output: 235 ( Minus sign (-) is placed in the [ stack. Note that the top of the * stack is not an operator to + compare minus sign with it
National University Page 16
Expression: 2+3*[(56)/2]
Expression: 2+3*[(56)/2]
Stack
Output:
2356-
[ * +
A.R. Hadaegh Dr. Ahmad R. Hadaegh
( is a closing round bracket. Keep on popping from the stack till we you find the matching opening round bracket. DO NOT COPY THE OPEN ROUND BRACKET TO THE OUTPUT
Page 17
National University
Expression: 2+3*[(56)/2]
Stack / [ * +
Output:
2356-
Division (/) is placed in the stack. Note that the top of the stack is not an operator to compare division sign with it
Expression: 2+3*[(56)/2]
Stack / [ * +
Output:
2356-2
National University
Page
18
Expression: 2+3*[(56)/2]
Stack
Output:
2356-2/
* + Stack
] is a closing square bracket. Keep on popping from the stack till we you find the matching opening square bracket. DO NOT COPY THE OPEN SQUARE BRACKET TO THE OUTPUT
Expression: 2+3*[(56)/2]
Output:
23562/*+
We are done. So keep popping from the stack and place the characters to the output line
2 +4 6
9 +8 17
1 5 +7 13
1 +3 4
2 Stack 1 9 5
9 5
+ =6
4 Stack 2 8 7 3 8 7 3
+ =17
7 3
+ =13
+ =4
Result Stack
A.R. Hadaegh Dr. Ahmad R. Hadaegh
7 6
National University
3 7 6
4 3 7 6
Page 22
When a cell is visited, it is marked with . so that the mouse does not try that again.
In general, the mouse always takes right cell first, if it cannot succeed, it takes left and if it still fails, it takes down and finally it tries the upper cell to reach its destination. As long as it finds a space it moves in that path and tries its best to find the exit door. We represent the mouse with M and exit door with E
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 23
In order to ensure that the mouse does not fall off the maze, we create a wall around the maze.
Thus the mouse will be trapped in the maze until it finds the door.
If the mouse tries all the possible solutions and still cannot find the exit door, we report a failure; otherwise, we report a success.
The algorithm is:
Initialize stack, exitCell, entryCell, currentCell = entryCell; Create a wall of 1s around the maze While currentCell is not exitCell Mark currentCell as visited; Push onto the stack the unvisited neighbors of current Cell in the order of up, down left and right If stack is empty Report failure Else Pop off a cell from the stack and make it currentCell success
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 24
0 1 2 3 4 5
Start with the following maze where the mouse is at position (3, 3) and the exit door is at (2, 4)
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1 1 o o o E 1 1 o o M 1 1 1 1 1 1 1 1
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1 1 o o o E 1 1 o o M 1 1 1 1 1 1 1 1
Make the current position visited (.) Pop the first element of the stack Move the mouse to that position
(3, 2) (2, 3)
National University
Page
25
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1
Make the current position visited (.) Pop the first element of the stack Move the mouse to that position
1 o o o E 1
1 o M . 1 1 1 1 1 1 1 1
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1 1 o o o E 1 1 M . . 1 1 1 1 1 1 1 1
National University
Page
26
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1
Make the current position visited (.) Pop the first element of the stack Move the mouse to that position
1 M o o E 1
1 . . . 1 1 1 1 1 1 1 1
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1 1 . 1 . M o E 1 . . 1 1
1 1 1 1 1 1
National University
Page
27
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 1 1 o o 1
Make the current position visited (.) Pop the first element of the stack Move the mouse to that position
1 .
1 .
.
.
M E 1
. 1 1
1 1 1 1 1 1
0 1 2 3 4 5
0 1 2 3 4
1 1 1 1 1 1 1 o 1 . 1 . . . . .
1 o 1 1
1 1 1 1
E/M 1
1 1 1 1
Page
28
Queue
A queue is an ADT with the following specific properties
A queue contains zero or more, normally homogenous, items Data items are added to the tail (back) of the queue and are removed from the head (front) of the queue
The items in the queue are maintained in FIFO (first-infirst out) order
The first item placed on the queue is always the first one removed
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 29
Possible operations Init: Initializes a queue so as to be empty Insert: Adds a new item to the tail of the queue Remove: Removes the item at the head of the queue
Data In
Data Out
Page
30
Queues are common in computing Scheduling queues Operating systems support multi-tasking by sharing scarce resources such as CPU time Each task that requests CPU resources might be put in a queue Tasks are given cycles of time If task finishes . good -- if not, task goes to the back of queue for additional cycles Lets discuss some implementations of Queue
National University
Page
31
Therefore, to optimize space, we can turn our queue into a circular queue so that when we reach the end of the queue, we can still insert some element if there is empty space in the head
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 32
Thus, the circular queue is full under when one of the following conditions are true: The head is at position 0 and tail is at position n-1 (last position) The head is at position of tail+1 in the queue The first case is shown in the following example. Suppose, the queue has 4 positions and we insert 10, 20, 30, and 40 without removing any element
head
head head tail 10 head tail 10 20 tail 10 20 30
tail
10 20 30 40
National University
Page
33
Now suppose after inserting 30 we pop 10 and we insert 40 and 50. Again the queue is full and head is tail+1 enqueue 10 enqueue 20
enqueue 30 head
10 20 30
dequeue 10 10 20 30
head tail
10
head tail
10 20
tail
head tail
enqueue 40
tail head
A.R. Hadaegh Dr. Ahmad R. Hadaegh
40 10 20 30
National University
The queue is initially empty. The initial value of head and tail is -1 and if during the operation we pop one element from the queue and we find out that the head and tail becomes equal
void Queue::enqueue(int e1) { if (! isFull()) { if (last == size -1 || last == -1) { storage[0] = e1; last = 0; if (first == -1) first = 0; } else { last++; storage[last] = e1; } } else cout <<Queue is full" << endl; }
class Queue { public: Queue(){last = first = -1, size =4;} void enqueue(int); int dequeue(); bool isFull() { return ( (first==0 && last == size-1) || (first==last+1) ); } bool isEmpty(){return first==-1;} private: int first, last, size; int storage[4]; }; int Queue::dequeue() { int tmp; if (isEmpty()) { cout << "Queue is empty " << endl; return 0; } tmp = storage[first]; if (first==last) last = first = -1; else if (first==size-1) first = 0; else first++; return tmp; }
Page 35
See: example 1
A.R. Hadaegh Dr. Ahmad R. Hadaegh
National University
bool isFull() { return ( (first==0 && last == size-1) || (first==last+1) ); } bool isEmpty(){return first==-1;} private: int first, last; T storage[size]; };
National University
Page
36
See: example 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 37