Vous êtes sur la page 1sur 4

DATA STRUCTURES & ALGORITHMS

Tutorial 3 Questions Queue and Recursion


Required Questions Question 1. 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 Question 2. The following algorithm is to reverse a queue
Algorithm reverse (val q <Queue>) Pre true Return a reversed queue of q 1 S = createStack 2 Q = createQueu 3 while (not empty(q)) 1 dequeue(q,temp) 2 pushStack(S,temp) 4 while (not empty(S)) 1 popStack(S,temp) 2 enqueu (Q,temp) 4 return Q End reverse

Develop a similar algorithem to append a stack to a queue


Algorithm append (val q <Queue>, val s <Stack>) Pre true Return elements of s are appended into q with the same order. for example if q = {1,2,3}, s = {4,5,6} then q = {1,2,3,4,5,6} after append (supposed that the queue elements are presented from front to rear and those of stack from bottom to top).

Question 3. Consider the following algorithm:


algorithm fun1 (x <integer>) 1 if (x < 6) 1 return 2 * x 2 else 1 return (2 * fun1 (x 3) + 8) 3 end if end fun1

What would be returned if fun1 is called as a. fun1 (4)? b. fun1 (10)? c. fun1 (12)? Question 4. Develop recusive algorithm for the following problems. a. Compute the sum of all numbers from 1 to n, where n is given as parameter.
Algorithm compute (val n <integer>) Pre n >=0 Return the sum 0+1+2+ 3+ ...+ n

b. find and return the minimum element in an array, where the array and its size are given as parameters.
Algorithm compute (val a <array>, val n <integer> ) Pre n >=0 Return the minimum element in a[]

Advanced Questions Question 5. Develop algorithms for the following problems. The algorithms must be fully recursive in that they contain no loops at all (neither for, while or do-while). a.
Algorithm listnumber (val start <integer>, val end <integer>) Pre start <=end Return printout the numbers from start to end to screen

b.
Algorithm mul (val a <integer>, val b <integer>) Pre a,b >=0 Return the product of two integers a and b. The only arithmetic operations that you are allowed to use in this problem are addition + and subtractions -.

c.
Algorithm pow (val a <float>, int b <integer>) Pre a,b >=0 Return the power ab. The only arithmetic operations that you are allowed to use in this problem are addition * and substration -.

Question 6. Develop fully recursive algorithms for the functions reverse and append in Question 2 Appendix Two ways of queue implementation Basically, the principle of a queue is first in first out. However, regarding practical aspect, there are two ways to get a queue implemented: using contiguous array and linked list. Queue implementation based on contiguous array A contiguous queue is generally can be declared as follows:
class Queue front <int> rear <int> array[maxSize] <data>

where front and rear keep positions of the first and the last elements of the queue respectively.

Example 1. Followings are algorithms of insert, remove and getsize methods for the contiguous queue
algorithm insert (val n <data>) Pre The queue is not full Post n will be inserted to the queue 1. if(rear == maxSize-1) rear = -1; 2. array[++rear] = j; end insert data algorithm remove Pre The queue is not empty Post Remove the first element of the queue Return The removed element 1. temp = queArray[front++] 2. if(front == maxSize) front = 0 3. return temp; end remove int algorithm getSize Pre None Post None Return The size of queue

1. if(rear >= front) size = rear-front+1; 2. else size = (maxSize-front) + (rear+1) 3. return size; end getSize

Queue implementation based on linked list A contiguous queue is generally can be declared as follows:
class Queue list <Linked_list>

Here, we use a linked list to store the queue elements. Supposed that we have a class Linked_list implemented together with class Node as follows:
class Linked_list head <Node*> class Node next <Node*> data

Example 2. Followings are algorithms of insert, remove and getsize methods for class Queue
algorithm insert (val n <data>) Pre None Post n will be inserted to the queue 1. list.insertLast(n); end insert data algorithm remove Pre The queue is not empty Post Remove the first element of the queue Return The removed element 1. return list.removeFirst(); end remove int algorithm getSize Pre None Post None Return The size of queue 1. return list.getSize(); end getSize

Of course, to complete the queue implementation, we must write the additional methods of insertLast, removeFirst and getSize methods for class Linked_list. After finishing the previous tutorials and labs, you should be able to complete these methods yourselves.

Vous aimerez peut-être aussi