You are on page 1of 10

Faculty of Computer Science and Engineering Department of Computer Science

2
2X
3x 3

+1

3/2x -1/2

4 2 + 3x - 5 x - 3 - 4x + 3x 2 -5
19

2
x

+ 4 2 -4x

Page 1/10

Faculty of Computer Science and Engineering Department of Computer Science

a. b. c. d. e.

fk f *k f\ 10 f\ x f* f2

Page 2/10

Faculty of Computer Science and Engineering Department of Computer Science Part 2. Stack Suppose that the following algorithms are implemented: - PushStack (ref s <Stack>, val n <data>): push the value n to the stack s - PopStack(ref s <Stack>, ref x <data>): remove the top element of the stack s and assign the data of that top element to x - EmptyStack(val s <Stack>): check whether the stack s is empty Required Questions Question 3. Imagine we have two empty stacks of integers, s1 and s2. Draw a picture of each stack after the following operations:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: PushStack (s1, 1); PushStack (s1, 9); PushStack (s1, 4); PushStack (s1, 2); while (!EmptyStack (s1)) { PopStack (s1, x); PushStack (s2, x); } //while PushStack (s1, 10); PushStack (s1, 7); while (!EmptyStack (s2)) { PopStack (s2, x); PushStack (s1, x); } //while PopStack (s1, x); PushStack (s2, x); PopStack (s1, x); PushStack (s2, x);

s1{rong} s2{1,9,4,2}

s1{7,10}

s1{2,4,9,1,7,10}s2{rong}

s1{9,1,7,10} s2{4,2}

Question 4. Write an algorithm for a function called RemoveSecond that removes the second element of a stack. The order of other elements in the stack must be the same after the removal.
algorithm RemoveSecond (ref sourceStack<Stack>) This algorithm removes the second element in the sourceStack. The order of the remaing elements must be preserved after the removal. Pre None Post the sourceStack being removed its second element Return None end RemoveSecond
pop(SourceStack,x) pop(SourceStack,y)

Push(SourceStack,x)

Page 3/10

Faculty of Computer Science and Engineering Department of Computer Science Part 3. Queue Suppose that the following algorithms are implemented: - EnQueue (ref q <Queue>, val n <data>): push the value n to the queue queue - DeQueue(ref q <Queue>, ref x <data>): remove the top element of the queue q and assign the data of that top element to x - EmptyQueue(val q <Queue>): check whether the queue q is empty
- QueueRear() - QueueFront()

Required Questions Question 5. Imagine we have an empty stack of integers S, and two empty queues of integer Q1 and Q2. What would be the value of queues Q1, Q2, and stack S, after the following segment?
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: Enqueue (Q1, 5) Enqueue (Q1, 6) Enqueue (Q1, 9) Enqueue (Q1, 0) Q1={6,2,0,5,7,0,9,6,5} Enqueue (Q1, 7) Enqueue (Q1, 5) Enqueue (Q1, 0) Enqueue (Q1, 2) Enqueue (Q1, 6) while (! EmptyQueue (Q1)){ DeQueue (Q1, x) S(2,6) if (x == 0){ z = 0 while (! EmptyStack (S)){ PopStack(S, &y) z = z + y Q1=rong } q2=12,20 Enqueue (Q2, z) S=2,6 }else{ PushStack (S, x) } }

Question 6. What would be the contents of queue Q after the following code is executed and the following data are entered?
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: Q = createQueue while (not end of file){ read number if (number != 0){ EnQueue (Q, number) }else{ QueueRear (Q, x) EnQueue (Q, x) } }

5,7,12,4,4,4,6,8,67,34,23,5,5,44,33,22,6,6

The data are: 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0. Page 4/10

Faculty of Computer Science and Engineering Department of Computer Science

Question 7. What would be the contents of queue Q1 after the following code is executed and the following data are entered?
11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: Q1 = createQueue S1 = createStack while (not end of file){ read number if (number != 0){ PushStack (S1, number) }else{ PopStack (S1, x) PopStack (S1, x) while (!EmptyStack(S1)){ PopStack (S1, x) EnQueue (Q1, x) } } }

44,33,4,6,8,67,34,5,7

The data are: 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0

Question 8. 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: 3 5 7 4 13
26: 27: 28: 29: 30: 31: 32: 33: 34: 35: Q3 = CreateQueue count = 0 while(!EmptyQueue(Q1)&&!EmptyQueue(Q2)){ count = count + 1 DeQueue (Q1, x) DeQueue (Q2, y) if (y == count){ EnQueue (Q3, x) } } 31

Page 5/10

Faculty of Computer Science and Engineering Department of Computer Science

Appendix
Formal parameters and actual parameters Simply speaking, formal parameters are those that are declared in algorithms/functions prototypes, meanwhile actual parameters are those that are passed when the algorithms/function are actually invoked. Example 1. Lets consider the following piece of pseudo-code

algorithmsearch( valn <datatype>) Return position of the list element whose data is equal to n end search //in another algorithm p = Search(number)

In this example, the algorithm search takes a formal parameter named n and returns the position of the list element whose data is equal to n. Later then, in another example algorithm, search is invoked with the actual parameter number. Notation of parameter passing When developing algorithms, we adopt the following notations regarding mechanisms of parameter passing: ref: parameters are passed by reference. When passed by reference, the actual parameter is a reference of the formal parameter. In other (and simpler) words, any value change that occurs in the formal parameter will also apply immediately on the actual parameter. val: parameters are passed by value. When the function is invoked, the value of actual parameter will be copied to that of the formal parameter. Since then, any change on formal parameters will not affect the actual parameters and vice versa. Lets consider the following piece of pseudo code representing a method in the class List implementing a linked list

Example 2.

algorithmcountPositive(val n <int>) This algorithm counts thenumber of elements whose data are positive numbers (incorrect version). Pre None Postn holdsthenumber of elements whose data are positive numbers 1. count = 0 2. pTemp = head; 3. loop (pTemp!=NULL) 1. if(pTemp->data > 0) then

Page 6/10

Faculty of Computer Science and Engineering Department of Computer Science


1. count++ 2. end if 3. pTemp = pTemp->link 4. end loop 5. n = count endcountPositive

As easily observed, this method intends to use the parameter n to return the positive data counted. However, since n is passed by value, the value cannot be passed properly to the actual parameters when countPositive is called. One way to correct the method is to take away the parameter and directly return the result as follows.
algorithmcountPositive() This algorithm counts thenumber of elements whose data are positive numbers Pre None Post None Return the number of positive elements 1. count = 0 2. pTemp = head; 3. loop (pTemp!=NULL) 1. if(pTemp->data > 0) then 1. count++ 2. end if 3. pTemp = pTemp->link 4. end loop 5. returncount endcountPositive

Alternatively, we can use the passing by reference mechanism to correct the method as follows
algorithmcountPositive(ref n<int>) This algorithm counts thenumber of elements whose data are positive numbers. Pre None Postn holdsthenumber of elements whose data are positive numbers 1. count = 0 2. pTemp = head; 3. loop (pTemp!=NULL) 1. if(pTemp->data > 0) then 1. count++ 2. end if 3. pTemp = pTemp->link 4. end loop 5. n = count endcountPositive

Method and global function Page 7/10

Faculty of Computer Science and Engineering Department of Computer Science In Example 2, we are in the situation of developing a method for the class List, thus we can assume that we can access the (private) internal member head of the class. However, in some case, you may be requested to write a global function, or function for short, which is generally unable to access internal members of the class. In that case, your algorithm should avoid touching any class properties and instead call the suitable method. Example 3. Write a function that counts in a list the number of elements whose data are positive numbers

algorithmcountPositive(ref list <Linked List>, ref n <int>) This algorithm counts thenumber of elements whose data are positive numbers Pre None Postn holdsthenumber of elements whose data are positive numbers 1. count = 0 2. i = 0 3. loop (i <list.size()) 1. list.retrieve(i, data) 2. if (data > 0) then 1. count++ 3. end if 4. i++ 4. endloop 5. n = count endcountPositive

In this example, we assume that the class List has already been implemented with methods size(), which returns the number of elements in the list, and retrieve(valpos<int>, ref dataOut<data>), which retrieves the data field of the element at the position pos in the list and assigns that data to the dataOut parameter.

Page 8/10

Faculty of Computer Science and Engineering Department of Computer Science 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 (as a circular array) A contiguous queue can be generally 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. Their values are -1 when the queue is empty. Example 4. 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; 3. if (front == -1) front = 0; //the queue was empty before => only element 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 (rear == front) {rear = front = -1} //remove the only element => empty 3. else if(front == maxSize-1) front = 0 4. else front++ 5. 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 Page 9/10

Faculty of Computer Science and Engineering Department of Computer Science

A linked queue can be generally 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 5.

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.

-- End --

Page 10/10

You might also like