0% found this document useful (0 votes)
36 views8 pages

CSCI220 Final Exam

The first document provides pseudocode to reverse the elements in a subarray from index i to j of an input array A by recursively calling the reverse function. The second document describes the steps

Uploaded by

Xavier Dong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

CSCI220 Final Exam

The first document provides pseudocode to reverse the elements in a subarray from index i to j of an input array A by recursively calling the reverse function. The second document describes the steps

Uploaded by

Xavier Dong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

int SumOddVal(int n) {
int sum = 0;
for (int i = 1; i < n; i += 2) {
sum += i;
}
return sum;
}

2.

Output from calling the main function of the Maryland class is: Read it, Ship it,
Box it, and Buy it. Because Maryland class is inherited from State class, and State
class is inherited from Region class, Region class is inherited from Place class,
Place class is inherited from Object class. All four classes of printMe() will be
called.
Code output:
Read it.
Ship it.
Buy it.
Read it.
Box it.
Read it.

3.

1. Creates a node pointer temp pointing to the head(first node) of the


list.(Node* temp = head)
2. While loop( while(temp->next->next != NULL)), if the statement is true, go
to step 3, otherwise, go to step 4.
3. States in the while loop is that the pointer temp will be moved to the next
node of the list. (temp = temp->next). Go to step 2.
4. Cout << “penultimate node found. Its element is” << temp->data;
5. Stop the function
4.

● Initially: A = {4, 3, 6, 2, 5}
● ReverseArray(A,0,4) → A = {5, 3, 6, 2, 4}; ReverseArray(A,0 + 1,4 - 1)
● ReverseArray(A,1,3) → A = {5, 2, 6, 3, 4}; ReverseArray(A,1+1,3 -1)
● ReverseArray(A,2,2) → Nothing will be changed (2 < 2 is false); Function
stop.

5.

1. Get the head of the singly linked list.


2. If the head is NULL, the number of nodes is 0, else, go to step 3.
3. If the head is not NULL, the count of nodes of the list increases by 1. calls
out the function again for the node that is next.

6.

Complexity O(n^3)(nested triple for loop)

7.
Executes a for loop that iterates n times, which n is the size of the array. In the for
loop, starting from position i = 1 of the array. Let the key equal to the value at this
position of the list, create a new variable j and let it equal to i -1. Next is to have a
while loop that checks if j >= 0 AND arr[j] > key. If so, move elements that are
greater than key one position ahead. After the while loop, let key equals arr[j + 1];
after the for loop is finished, the elements in the array are in ascending order,
therefore, execute another for loop that prints the last 10 elements of the array. It is
an insertion sort. It's time complexity is O(n^2).

8.

Operation Stack content (top to bottom → right to left)


Push(5) {5}
Push(3) {5, 3}
Pop() {5}
Push(2) {5, 2}
Push(8) {5, 2, 8}
Pop() {5, 2}
Pop() {5}
Push(9) {5, 9}
Push(1) {5, 9, 1}
Pop() {5, 9}
Push(7) {5, 9, 7}
Push(6) {5, 9, 7, 6}
Pop() {5, 9, 7}
Pop() {5, 9}
Push(4) {5, 9, 4}
Pop() {5, 9}
Pop() {5}

9.

Operation Queue contents (rear to front → left to right)


Enqueue(5) {5}
Enqueue(3) {3, 5}
Dequeue() {3}
Enqueue(2) {2, 3}
Enqueue(8) {8, 2, 3}
Dequeue() {8, 2}
Dequeue() {8}
Enqueue(9) {9, 8}
Enqueue(1) {1, 9, 8}
Dequeue() {1, 9}
Enqueue(7) {7, 1, 9}
Enqueue(6) {6, 7, 1, 9}
Dequeue() {6, 7, 1}
Dequeue() {6, 7}
Enqueue(4) {4, 6, 7}
Dequeue() {4, 6}
Dequeue() {4}

10.

Operation Deque contents(rear to front → left to right)


InsertFront(3) {3}
InsertBack(8) {8, 3}
InsertBack(9) {9, 8, 3}
InsertFront(5) {9, 8, 3, 5}
RemoveFront() {9, 8, 3}
EraseBack() {8, 3}
First() {8, 3} First() = 3
InsertBack(7) {7, 8, 3}
RemoveFront() {7, 8}
Last() {7, 8} Last() = 7
EraseBack() {8}

11.

Stack 1 → s1, Stack 2 → s2, Deque → dq.


Since Stack is a LIFO data structure, all additions and deletions can only be done at
one end, while Deque is more flexible, all additions and deletions can be done at
both ends. Therefore, in order to implement Deque ADT by using two stacks, we
should let one Stack store all the elements(s1), and make the other stack empty in
normal times. Since functions insertFront(e), eraseFront(e) and front() of Deque
ADT are doing the same task as functions push(e), pop(), and top() of Stack ADT,
no additional statements need to be written on these functions. For example:
void insertFront(e){ void eraseFront(e){ E& front(){
s1.push(e); s1.pop(); s1.top();
} } }
However, functions insertBack(e), removeBack(), and back() will be a little bit
complicated. We cannot directly store the element into the s1, because the function
top() of the s1 is corresponding to the function front() of the dq. Therefore, we
need to use s2 in order to store the element to the bottom of s1. We need to store
the element that is being inserted into s2, then extract all the elements from s1.
After s1 becomes empty, s1 then should extract all the elements back from s2 until
s2 becomes empty once again. The function removeBack() and back() execute with
the similar concept. Once the s1 is empty, execute s2.pop(), so that the back
element of the dq is being removed. Once the s1 is empty, execute s2.top(), so that
the back element of the dq is being returned. For example:
void insertBack(e){ void removeBack(){ //E& back()
while(!s1.empty()){ while(!s1.empty()){
s2.push(s1.top()); s2.push(s2.push(s1.top());
s1.pop(); s1.pop();
} }
s2.push(e); s2.pop(); //s2.top();
while(!s2.empty()){ while(!s2.empty()){
s1.push(s2.top()); s1.push(s2.top());
s2.pop(); s2.pop();
} }
} }
Function size() and empty() of dq can be determined by the number of elements
that are stored in s1.

12.

Operation Vector contents


insert(0, 4) {4}
insert(0, 3) {3, 4}
insert(0, 2) {2, 3, 4}
insert(2, 1) {2, 3, 1, 4}
insert(1, 5) {2, 5, 3, 1, 4}
insert(1, 6) {2, 6, 5, 3, 1, 4}
insert(3, 7) {2, 6, 5, 7, 3, 1, 4}
insert(0, 8) {8, 2, 6, 5, 7, 3, 1, 4}

13.
If the sequence S is implemented with a doubly linked list, then, in order to let the
element in p’s position to be printed first, we need to let the prev pointer of the
node in p position points to the header, and the next potiner of the header points to
the p(p-> next = header-> next; header->next->pre = p; header->next = p; p->pre =
header). Not only that, we also need to link the nodes that were previously linked
with p’s node together. (p->pre->next = p->next; p->next->pre = p->pre)

14.

15.

Operation Queue contents


insert(5, a) {(5,a)}
insert(4, b) {(4,b), (5,a)}
insert(7, i) {4, b), (5,a), (7, i)}
insert(1, d) {(1, d), (4, b), (5,a), (7, i)}
removeMin() {(4, b), (5,a), (7, i)}
insert(3, j) {(3, j), (4, b), (5,a), (7, i)}
insert(6, c) {(3, j), (4, b), (5,a), (6, c), (7, i)}
removeMin() {(4, b), (5,a), (6, c), (7, i)}
removeMin() {(5,a), (6, c), (7, i)}
insert(8, g) {(5,a), (6, c), (7, i), (8, g)}
removeMin() {(6, c), (7, i), (8, g)}
insert(2, h) {(2, h), (6, c), (7, i), (8, g)}
removeMin() {(6, c), (7, i), (8, g)}
removeMin() {(7, i), (8, g)}

16

A. Preorder Traversal: 1 2 4 5 3 6 7

B. Inorder Traversal: 4 2 5 1 6 3 7

C. Inorder Traversal: 4 5 2 6 7 3 1

17

You might also like