0% found this document useful (0 votes)
90 views4 pages

cs214 - Mid - ModelAnswer

This document appears to be a midterm exam for a data structures course covering topics like stacks, queues, linked lists, and sorting algorithms. It contains 4 multiple choice questions worth 14 marks, 2 short answer questions worth 6 marks, a diagram question analyzing a linked list worth 15 marks, and a question worth 5 marks asking to write a function to reverse an array using a stack. The exam is out of 40 total marks and is proctored for 1 hour.

Uploaded by

azer elsaied
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)
90 views4 pages

cs214 - Mid - ModelAnswer

This document appears to be a midterm exam for a data structures course covering topics like stacks, queues, linked lists, and sorting algorithms. It contains 4 multiple choice questions worth 14 marks, 2 short answer questions worth 6 marks, a diagram question analyzing a linked list worth 15 marks, and a question worth 5 marks asking to write a function to reverse an array using a stack. The exam is out of 40 total marks and is proctored for 1 hour.

Uploaded by

azer elsaied
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/ 4

Student name: _________________________________________________Student ID: _________________

Cairo University Mid-term exam


Faculty of Computers and Artificial Intelligence Semester: 1st
Subject: Data Structures Date: 22/11/2022 /40
Subject Code: CS214 Duration: 1 hour
Examiner(s): Cherry Ahmed
/20
Question 1: Multiple Choice: [14 marks]
1. For a stack implemented based on a linked list, where should the pushes and pops
be performed for the best efficiency?
a) Push at the head, pop from the head b) Push at the tail, pop from the tail
c) Push at the head, pop from the tail d) Push at the tail, pop from the head

2. For the insertion sort algorithm, what is the best-case scenario for an array of size n?
a) The array is sorted
b) The array is reversely sorted
c) The array is not sorted
d) Both (a) and (b)

3. Which of the following algorithms runs in N.log N average time but quadratic worst-
case time?
a) Insertion sort b) Merge sort
c) Quick sort d) Shell sort

4. The best case complexity of Insertion Sort is:


a) O(N2) b) O(N.logN)
c) O(N) d) O(logN)

5. If a header node is used, which of the following indicates a list with one item?
a) Head != NULL b) Head != NULL && Head->next ==NULL
c) Head == NULL d) Head != NULL && Head->next !=NULL &&
Head->next->next ==NULL
6. Which of the following operations is performed more efficiently by doubly linked list
than by singly linked list?

a) Searching of an unsorted list for a given item


b) Deleting the tail node
c) Inserting a node at a given location
d) Traversing a list to process each node

7. Which of the following statements inserts a new node of value val, right before
current?

a) current = new IntNode(val);


b) current->next = new IntNode(val);
c) IntNode * n = new IntNode(val, current->next);
d) IntNode * n = new IntNode(val, current);

1/4
Question 2: Answer the following: [6 marks]

A. Apply the Bubble sort algorithm on the following array showing every swap: (5
marks)
20 10 90 7 50
10 20 90 7 50

10 20 7 90 50

10 20 7 50 90

10 7 20 50 90

7 10 20 50 90

B. If the array [3, 6, 9, 10] was given to be sorted by bubble sort algorithm, how many
swaps will be done? What about number of swaps if the same array was given to
selection sort? (1 mark)
Bubble sort: 0 swaps
Selection sort: 3 swaps

Question 3: Answer the following [15 marks]


In the following diagram a pointer is indicated by an arrow, the list nodes have an info
variable containing an integer and a next variable containing a pointer (both are public
members), and list, A, and B are pointers to a list node.

A. Give the values of the following expressions: (4 marks)

i. A->info 32

ii. list->next->info 32

iii. list->next == A true

iv. B->next->next->next NULL

2/4
B. Write one statement to do each of the following: (3 marks)

i. Make pointer B point to the node containing the value 25.

B= B->next;

ii. Make the value 32 change to 40 in 2 different ways (each is 1 statement).

A->info = 32;
list->next->info = 32;

template<class T>
C. Given the following class declaration of a single class SLL
linked list, implement the member function void {
Node<T> *head, *tail;
SLL::moveTailToFront(). The function moves public:
the tail node to the front of the list before the SLL(){head = tail = 0;}
head node. You may call other member void addToHead(T);
void addToTail(T);
functions in your code. (8 marks) void moveTailToFront();
};
Example: For the linked list L:
90 95 80 60

head tail

After calling L.moveTailToFront() 60 90 95 80

head tail

template<class T>

void SLL<T>::moveTailToFront()
{
//if list is emoty or has 1 node only, do nothing
if(head == tail) return;

Node<T>* prevNode = head;


//a loop to stop before tail
while(prevNode->getNext() != tail)
{
prevNode = prevNode->getNext();
}
//prev's next will be NULL since it will be the tail node
prevNode->setNext(0);

//our target node, which is the tail node will point to head
tail->setNext(head);

//now make the moved node the new head


head = tail;

3/4
//change the tail to be prev
tail = prevNode;
}

Question 4: Answer the following [5 marks]


Based on the given stack declaration, write a function template<class T>
reverseArray(int A[ ], int n) that uses a stack to reverse class Stack
the elements of the array A of size n. So, if array A has {
the elements [5,4,3,2], it will change to be [2,3,4,5] (5 public:
Stack();
marks)
void clear();
bool isEmpty();
T& top();
T pop();
void push(const T&);
};

void reverseArray(int A[], int _size)

{
MyStack<int> s;
for(int i=0;i< _size;i++)
s.push(A[i]);
for(int i=0;i< _size;i++)
A[i] = s.pop();
}

4/4

You might also like