# Homework - Chapter 03 Solution.

## Presentation on theme: "Homework - Chapter 03 Solution."— Presentation transcript:

Homework - Chapter 03 Solution

Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue should now be able to use all positions to hold up elements. The complexity of each of your functions should be O(1) (exclusive of the time taken to double queue capacity when needed).

Solution Suppose lastOp is of string type. template <class T> void Queue<T>::Push(const &x) { if (rear == front && lastOp == “INSERT”) Resize(); rear = (rear + 1) % capacity; queue[rear] = x; lastOp = “INSERT”; };

Solution template <class T> void Queue<T>::Pop() { if (rear == front && lastOp == “DELETE”) //Empty throw “Queue is empty. Cannot delete.” front= (front + 1) % capacity; queue[front].~T(); lastOp = “DELETE”; };

Problem 2 Please implement the class Queue by using stacks.
Suppose that you start with an empty stack. After a sequence of n inqueues (insertion) and dequeues (deletion) operations, what is the average time complexity for each operation. please reason your answer.

Solution class Queue { public: Queue(); ~Queue();
void Push(data value); data Pop(); private: Stack S1, S2; };

Solution Queue::Queue() { Initialize S1 and S2; } Queue::~Queue() deallocate S1 and S2; void Queue::Push(data value) { S1.Push(value); } data Queue::Pop() if (S1.IsEmpty()) throw exception; while (!S1.IsEmpty()) S2.Push(S1.Pop()); data delNode = S2.Pop(); while (!S2.IsEmpty()) S1.Push(S2.Pop()); return delNode;

Solution Insertion的時間複雜度: O(1). Deletion的時間複雜度: O(S), 假設S代表queue當中的個數。

Problem 3 Given the following maze:
1 2 3 4 5 6 7 8 9 entrance exit Trace out the action of Program 3.16 on the maze and find a path through the maze Rewrite Program 3.16 so that it can find out the shortcut.

Solution The path is (1,1) →(2,2) →(2,3) →(3,3) →
4 5 6 7 8 9 The path is (1,1) →(2,2) →(2,3) →(3,3) → (4,4) →(3,5) →(2,5) →(1,6) →(2,7) → (3,6) →(4,6) →(5,5) →(6,6) →(7,7) → (8,8) →(8,9)

Solution void Path(const in m, const int p) { ….
//if a path is found, the points are stored in the stack. //Then, invoke a function to print out the shortcut. ShortCut(m, p, maze, stack); }

Shortcut(m, p, maze, stack) { //step 是一個2D的陣列，step[i][j]記錄一個整數 //用來代表(i,j)會被原路徑中會第幾個點被拜訪到 int step[m][p]; //用來標記路徑 int mark[m][p]; int length = 1; //用來記錄目前路徑走到第幾點 Stack path, result; Initialize step[i][j] with a large number (MAX) for each (i, j); Initialize mark[i][j] with 0 for each (i, j); while (!stack.IsEmpty()) { Let p=(x, y) be a point popped from stack; mark[x][y] = 1; path.Push(p); } previous = path.Pop(); //The first point result.Push(previous); while (!path.IsEmpty()) if (step[x][y] < length) then //表示有更快的方法可以走到p for (i = length; i>step[x][y]; i--) { //pop掉之前走過的 del = result.Pop(); for each t =(a, b) where t denotes any neighbor of del do if step[a][b] equals to i then Restore step[a][b] to MAX; end for length = step[x][y]; end if result.Push(p); //走到p for each q = (m, n), where q is neighbor of p do //將p的每個鄰居以現有的長度(length)來標記 if (q is not previous and mark[m][n] equals to 1 and step[m][n] >= length) step[m][n] = length;

Problem 4 Given an infix expression
6 / 2 * * 5 Use tables like Section and Figure 3.16 to describe how you convert it into postfix notation and how you evaluate its value. Write an algorithm to convert an infix expression into prefix notation.

Solution 6 / 2 * 3 + 4 * 5 Next Stack Output none empty 6 Empty / 2
6 2 * 6 2 / 3 6 2 / 3 + 6 2 / 3 * 4 6 2 / 3 * 4 +* 5 + * 6 2 / 3 * 4 5 6 2 / 3 * 4 5 * +

Solution 6 2 / 3 * 4 5 * + Operations Postfix Notation 3 = 6 / 2
3 3 * 4 5 * + 9 4 5 * + 9 = 3 * 3 20 = 4 * 5 9 20 + 29 = 29

void InfixToPrefix (expression e)
{ Stack S; Stack Output; Reverse the expression e; //for example, 2+4*3 becomes 3*4+2 for (i=0; e[i] is not the end of string; i++) { if (e[i] is an operand) Output.Push(e[i]; else if (e[i] is ‘)’) S.Push(e[i]); else if (e[i] is not ‘(’) { while (S is not empty) { y = S.Pop(); if (y is not ‘(‘ and the priority of y > the priority of e[i]) Output.Push(y); else { S.Push(y); break; } else { //e[i] == ‘(’ if (y != ‘)‘) else Pop out all the elements in S and push them into the stack Output; while (!Output.IsEmpty()) print out Output.Pop();