Tutorial Solution 3

8 1.1K 9
Tutorial Solution 3

Đang tải... (xem toàn văn)

Thông tin tài liệu

TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính DATA STRUCTURES & ALGORITHMS Tutorial 3 – Solutions QUEUE - RECURSION Question 1: What would be the value of queues Q1, Q2, and stack S, after the following segment? 1 S = createStack 2 Q1 = createQueue 3 Q2 = createQueue 4 enqueue (Q1, 5) 5 enqueue (Q1, 6) 6 enqueue (Q1, 9) 7 enqueue (Q1, 0) 8 enqueue (Q1, 7) 9 enqueue (Q1, 5) 10 enqueue (Q1, 0) 11 enqueue (Q1, 2) 12 enqueue (Q1, 6) 13 loop (not emptyQueue (Q1)) 1 dequeue (Q1, x) 2 if (x == 0) 1 z = 0 2 loop (not emptyStack (S)) 1 popStack(S, &y) 2 z = z + y 3 end loop 4 enqueue (Q2, z) 3 else 1 pushStack (S, x) 4 end if 14 end loop Answer: Q1: Empty Q2: Rare Font 12 20 S: 6 2 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính Question 2: What would be the contents of queue Q after the following code is executed and the following data are entered? 1 Q = createQueue 2 loop (not end of file) 1 read number 2 if (number not 0) 1 enqueue (Q, number) 3 else 1 queuerear (Q, x) 2 enqueue (Q, x) 4 end if 3 end loop The data are: 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0. Answer: Q: Font Rare 5 7 12 4 4 4 6 8 67 34 23 5 5 44 33 22 6 6 Question 3: What would be the contents of queue Q1 after the following code is executed and the following data are entered? 1 Q1 = createQueue 2 S1 = createStack 3 loop (not end of file) 1 read number 2 if (number not 0) 1 pushStack (S1, number) 3 else 1 popStack (S1, x) 2 popStack (S1, x) 3 loop (not empty S1) 1 popStack (S1, x) 2 enqueue (Q1, x) 4 end loop 4 end if 4 end loop The data are: 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0 Answer: Q1 Font Rare 7 5 34 67 8 6 4 33 44 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính Question 4: Imagine that the contents of queue Q1 and queue Q2 are as shown. What would be the contents of queues Q1, Q2 and Q3 after the following code is executed? The queue contents are shown front (left) to rear (right). Q1: 42 30 41 31 19 20 25 14 10 11 12 15 Q2: 4 5 4 10 13 1 Q3 = createQueue 2 count = 0 3 loop (not empty Q1 and not empty Q2) 1 count = count + 1 2 dequeue (Q1, x) 3 dequeue (Q2, y) 4 if (y equal count) 1 enqueue (Q3, x) 5 end if 4 end loop Answer: Q1: Q2: Empty Q3: Empty Question 5: Write an algorithm for a function called reverseQueue that reverses the order of elements on queue using additional non-array variables and a. one additional stack b. one additional queue algorithm reverseQueue (ref queue <Queue>) This algorithm reverses the order of elements on queue Pre None Post The order of elements on queue is reversed Return None end reverseQueue Answer: a. algorithm reverseQueue (ref queue <Queue>) This algorithm reverses the order of elements on queue using one additional stack. 20 25 14 10 11 12 15 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính Pre None Post The order of elements on queue is reversed Return None 1. S = createStack 2. Loop (not empty queue) 1. dequeue(queue, x) 2. pushStack(S, x) 3. End loop 4. Loop (not empty S) 1. popStack(S, x) 2. enqueue (queue,x) 5. End loop end reverseQueue b. Using recursive algorithm: algorithm reverseQueue (ref queue <Queue>) This algorithm reverses the order of elements on queue using one additional queue Pre None Post The order of elements on queue is reversed Return None 1. Q1 = createQueue 2. Loop (not empty queue) 1. dequeue(queue, x) 2. enqueue(Q1, x) 3. End loop 4. recursiveReverseQueue(Q1, queue) end reverseQueue algorithm recursiveReverseQueue(ref Q1 <Queue>, ref Q2 <Queue>) 1. if (Q1 is empty) 1. return 2. end if 3. dequeue(Q1, x) 4. recursiveReverseQueue(Q1, Q2) 5. enqueue(Q2, x) end recursiveReverseQueue Using non-recursive algorithm: algorithm reverseQueue (ref queue <Queue>) This algorithm reverses the order of elements on queue using one additional queue Pre None Post The order of elements on queue is reversed Return None TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 1. Q1 = createQueue 2. loop (not emptyQueue(queue)) 1. count = queueSize(queue) 2. loop (count > 1) 1. dequeue (queue, x) 2. enqueue (queue, x) 3. count = count - 1 3. dequeue (queue, x) 4. enqueue (Q1, x) 3. end loop 4. loop (not emptyQueue(Q1)) 1. dequeue (Q1, x) 2. enqueue (queue, x) 5. end loop end reverseQueue Question 6: Write the pseudocode for a method of the Linked Queue in the lecture notes (implemented with two pointers front and rear) that receives another linked queue and appends the input queue to the end of the current queue. The input queue will be empty afterward. algorithm appendQueue (ref in_queue <Linked Queue>) This algorithm appends the input in_queue to the end of this queue Pre in_queue is a linked queue Post the in_queue will be appended into this queue and be empty end appendQueue Answer: algorithm appendQueue (ref in_queue <Linked Queue>) This algorithm appends the input in_queue to the end of this queue Pre in_queue is a linked queue Post the in_queue will be appended into this queue and be empty 1. loop (not in_queue.Empty) 1. in_queue.QueueFront(x) 2. in_queue.Dequeue 3. this.Enqueue(x) 2. end loop end appendQueue TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính Question 7: Consider the following algorithm: What would be returned if fun1 is called as a. fun1 (4)? b. fun1 (10)? c. fun1 (12)? Answer: a. fun1(4) = 3*4 = 12 b. fun1(10) = 2 * (2 * (3 * 0) + 7) + 7 = 21 c. fun1(12) = 2 * (2 * (3 * 2) + 7) + 7 = 45 Question 8: Consider the following algorithm: What would be returned if fun3 is called as: a. fun3 (10, 4)? b. fun3 (4, 3)? c. fun3 (4, 7)? d. fun3 (0, 0)? Answer: a. fun3(10, 4) = -1 b. fun3(4, 3) = -1 c. fun3(4,7 ) = 4 * 5 * 6 * 1 = 120 d. fun3(0,0) = 1 Question 9: Write a recursive function that calculates and returns the length of a linked list. Answer: algorithm linkedListLength (val head <pointer>) This algorithm calculates and returns the length of a linked list Pre head points to the first node of a linked list or is null if list is empty Post none algorithm fun1 (x <integer>) 1 if (x < 5) 1 return 3 * x 2 else 1 return (2 * fun1 (x – 5) + 7) 3 end if end fun1 algorithm fun3 (x <integer>, y <integer>) 1 if (x > y) 1 return -1 2 elseif (x equal y) 1 return 1 3 else 1 return (x * fun3 (x + 1, y)) 4 end if end fun3 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính algorithm strToInt (val string <array of characters> val index <integer> ref mul <integer>) This algorithm converts a number string to an integer. Pre string is an array of characters (all digits) index is the current index (initial call uses index of 0) mul is the multiplier of the most recent digit Post the converted integer value is returned mul is updated to the most multiplier 1. if (index equals string length) 1. mul = 1 2. return 0 2. end if 3. cur = strToInt(string, index + 1, mul) 4. cur = cur + (string[index] - '0') * mul 5. mul = mul * 10 6. return cur end strToInt Usage: mul=1 strToInt (string, 0, mul) Return a non-negative integer number represents the length of the linked list 1. if(head is null) 1. return 0 2. end if 3. return 1 + linkedListLength(head -> link) end linkedListLength Question 10. The following recursive algorithm converts a string of numerals to an integer. For example, “43567” will be converted to 43567. Note that, the reference parameter mul is updated (in step 5) when this function is called recursively (in step 3) and is used in the caller function (in step 4). Run the algorithm step-by-step to demonstrate its correctness with the following string: a) “1234” b) “2030” Answer: a. “1234” strToInt( “1234”, 0, mul) : cur = strToInt(“1234”, 1, mul) cur = strToInt(“1234”, 2, mul) cur = strToInt(“1234”, 3, mul) cur = strToInt(“1234”, 4, mul) mul = 1; cur = 0 + 4 * 1 = 4 mul = 10 * 1 = 10 cur = 4 + 3 * 10 = 34 mul = 10 * 10 = 100 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính cur = 34 + 2 * 100 mul = 100 * 10 = 1000 cur = 234 + 1 * 1000 = 1234 mul = 1000 * 10 = 10000  Result = 1234 b. “2030” strToInt( “2030”, 0, mul) : cur = strToInt(“2030”, 1, mul) cur = strToInt(“2030”, 2, mul) cur = strToInt(“2030”, 3, mul) cur = strToInt(“2030”, 4, mul) mul = 1; cur = 0 + 0 * 1 = 0 mul = 10 * 1 = 10 cur = 0 + 3 * 10 = 30 mul = 10 * 10 = 100 cur = 30 + 0 * 100 mul = 100 * 10 = 1000 cur = 30+ 2 * 1000 = 2030 mul = 1000 * 10 = 10000  Result = 2030 End . called as: a. fun3 (10, 4)? b. fun3 (4, 3) ? c. fun3 (4, 7)? d. fun3 (0, 0)? Answer: a. fun3(10, 4) = -1 b. fun3(4, 3) = -1 c. fun3(4,7 ) = 4 * 5 * 6 * 1 = 120 d. fun3(0,0) = 1 Question. a) “1 234 ” b) “2 030 ” Answer: a. “1 234 ” strToInt( “1 234 ”, 0, mul) : cur = strToInt(“1 234 ”, 1, mul) cur = strToInt(“1 234 ”, 2, mul) cur = strToInt(“1 234 ”, 3, mul) cur = strToInt(“1 234 ”,.  Result = 1 234 b. “2 030 ” strToInt( “2 030 ”, 0, mul) : cur = strToInt(“2 030 ”, 1, mul) cur = strToInt(“2 030 ”, 2, mul) cur = strToInt(“2 030 ”, 3, mul) cur = strToInt(“2 030 ”, 4, mul)

Ngày đăng: 09/06/2015, 15:11

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan