Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 1/10
x
x
4
3
2
- 5
+
x
2
+ 4
x
2
4x
3
-
3x
2
-5
+
-
-4x
19
2X
2
+1
3/2x
3x
3
-1/2
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 2/10
a. f – k
b. f *k
c. f\ 10
d. f\ x
e. f* f
2
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 3/10
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: PushStack (s1, 1);
2: PushStack (s1, 9);
3: PushStack (s1, 4);
4: PushStack (s1, 2);
5: while (!EmptyStack (s1)) {
6: PopStack (s1, x);
7: PushStack (s2, x);
8: } //while
9: PushStack (s1, 10);
10: PushStack (s1, 7);
11: while (!EmptyStack (s2)) {
12: PopStack (s2, x);
13: PushStack (s1, x);
14: } //while
15: PopStack (s1, x);
16: PushStack (s2, x);
17: PopStack (s1, x);
18: PushStack (s2, x);
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
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}
pop(SourceStack,x)
pop(SourceStack,y)
Push(SourceStack,x)
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 4/10
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: Enqueue (Q1, 5)
2: Enqueue (Q1, 6)
3: Enqueue (Q1, 9)
4: Enqueue (Q1, 0)
5: Enqueue (Q1, 7)
6: Enqueue (Q1, 5)
7: Enqueue (Q1, 0)
8: Enqueue (Q1, 2)
9: Enqueue (Q1, 6)
10: while (! EmptyQueue (Q1)){
11: DeQueue (Q1, x)
12: if (x == 0){
13: z = 0
14: while (! EmptyStack (S)){
15: PopStack(S, &y)
16: z = z + y
17: }
18: Enqueue (Q2, z)
19: }else{
20: PushStack (S, x)
21: }
22: }
Question 6.
What would be the contents of queue Q after the following code is executed and the
following data are entered?
1: Q = createQueue
2: while (not end of file){
3: read number
4: if (number != 0){
5: EnQueue (Q, number)
6: }else{
7: QueueRear (Q, x)
8: EnQueue (Q, x)
9: }
10: }
The data are: 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0.
Q1={6,2,0,5,7,0,9,6,5}
5,7,12,4,4,4,6,8,67,34,23,5,5,44,33,22,6,6
Q1=rong
q2=12,20
S=2,6
S(2,6)
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 5/10
Question 7.
What would be the contents of queue Q1 after the following code is executed and the
following data are entered?
11: Q1 = createQueue
12: S1 = createStack
13: while (not end of file){
14: read number
15: if (number != 0){
16: PushStack (S1, number)
17: }else{
18: PopStack (S1, x)
19: PopStack (S1, x)
20: while (!EmptyStack(S1)){
21: PopStack (S1, x)
22: EnQueue (Q1, x)
23: }
24: }
25: }
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: Q3 = CreateQueue
27: count = 0
28: while(!EmptyQueue(Q1)&&!EmptyQueue(Q2)){
29: count = count + 1
30: DeQueue (Q1, x)
31: DeQueue (Q2, y)
32: if (y == count){
33: EnQueue (Q3, x)
34: }
35: }
31
44,33,4,6,8,67,34,5,7
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 6/10
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. Let’s 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.
Example 2. Let’s consider the following piece of pseudo code representing a method
in the class List implementing a linked list
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
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 7/10
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
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 8/10
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.
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 9/10
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
Faculty ofComputerScienceandEngineering
Department ofComputerScience
Page 10/10
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
. Faculty of Computer Science and Engineering Department of Computer Science Page 1/10 x x 4 3 2 - 5 + x 2 + 4 x 2 4x 3 - 3x 2 -5 + - -4x 19 2X 2 +1 3/2x 3x 3 -1 /2 Faculty of Computer Science. 34, 23 , 5, 0, 44, 33, 22 , 6, 0. Q1={6 ,2, 0,5,7,0,9,6,5} 5,7, 12, 4,4,4,6,8,67,34 ,23 ,5,5,44,33 ,22 ,6,6 Q1=rong q2= 12, 20 S =2, 6 S (2, 6) Faculty of Computer Science and Engineering Department of Computer. Science and Engineering Department of Computer Science Page 2/ 10 a. f – k b. f *k c. f 10 d. f x e. f* f 2 Faculty of Computer Science and Engineering Department of Computer Science