You are on page 1of 3

Data Structures & Algorithms Sheet #2 - Solution

Cairo University
Faculty of Engineering
Computer Engineering Department
Data Structures and Algorithms
Sheet #2 - Solution
Classes and Lists
1. Given two sorted (ordered) sequential lists (arrays) L1 and L2, write an efficient algorithm to
print sorted intersection L1 ∩ L2 using only the basic list operations.
Assume that L1 and L2 are sets (no duplicates) and the printed numbers should not contain
duplicates too.

Pre: L1 and L2 are sorted with no duplicates.


Post: printing the sorted intersection on the screen
Input: L1List, L2List, N size of L1, Msize of L2
Return: None

Algorithm
Set I=0, J=0
While (I<N AND J<M)
IF(L1[I] == L2[J]) //Two items are equal  print one of them
Print L1[I]
Increment I
Increment J
ELSE IF (L1[I] < L2[J]) //Move I as long as L1 items are smaller than L2
Increment I
ELSE //Which means: If L1[I] > L2[J]
Increment J
END IF Commented [E1]:
END WHILE [Magnitude]:
#include <math.h> or #include <cmath>
END Algorithm magnitude = sqrt (pow(real,2) + pow(imag,2));

2. Given two sorted lists L1 and L2, write an efficient algorithm to print sorted L3 = L1 ∪ L2 using Commented [E2]: Complete C++ code of problems 4 to 6
only the basic list operations. is provided with sheet solution in folder:
“Sheet_2_Answer_Code”
Assume that L1 and L2 are sets (no duplicates) and the printed numbers should not contain
duplicates too. class Student
{
char name[150];
3. Write a C++ class “Complex” which represents a complex number. int id;
a. Write C++ functions that implement addition, multiplication and magnitude calculation float grades[40];
public:
b. Write a main function that creates 2 complex numbers and displays their addition, ...
multiplication and magnitude results };
Commented [E3]:
4. Write a C++ class “Student” to store student information. The class should store the following Prototype:
Student SearchByID(Student students[], int n, int id);
information: Function Output:
- Student Name (max. 150 characters) Could be one of the following:
- Student ID 1- Student: the found student
or a student with ID -1 if not found
- Student grades in 40 courses 2- int: the index of the found student
or -1 if not found
output 2 is better because less in memory.
5. Write a global C++ function that searches an array of Student class objects (as specified in
problem 4) for a student with a specified ID. What should be the output of that function? Commented [E4]:
Notes:
- cin/cout of array is made by a loop to enter each element
6. Write a main function for problem 5 that: except char array; could be taken or displayed by its name
a. Defines 5 students directly:
cout<<s.name;
b. Takes their data from user cin>>s.name;
c. Searches for a student by his id and displays his data if found - cin char array doesn’t accept spaces,
need to use: cin.getline(s.name, 150);

CMP N102 1/3 Spring 2018


Data Structures & Algorithms Sheet #2 - Solution

7. A common way of deletion is the so-called lazy deletion. Commented [E5]: Complete C++ code of problem 7 is
- To delete an element from a list, we mark it as deleted (using an extra bit or bool field) provided with sheet solution in folder :
“Sheet_2_Answer_Code”
- The number of deleted and non-deleted elements is kept as a part of the data structure
- If there are as many deleted elements as non-deleted elements, we traverse the list
performing the standard deletion algorithm on all marked nodes
a) List the advantages and disadvantages of lazy deletion Commented [E6]:
b) Write C++ classes and functions to implement lazy deletion for sequential lists Advantages:
-Not shifting or worse reallocating the array with smaller
Assume that deletion is by element value. size multiple times after each deletion
c) Write Search function to search for an element value in the list Disadvantages:
-[time-wise] more checks:
d) Write Display function to display the non-deleted elements checking if element is not deleted before any operation on it
e) Write a main function that creates a lazy deletion list and tests deleting and searching -[memory-wise] more locations than what actually needed
operations

8. Write an algorithm to insert an element x at location i (0-based) in an array. What parameters Commented [E7]:
does the algorithm need to know about the array to work correctly and avoid unnecessary [Parameters]:
Size: current no of elements that actually exist in the array
operations? Capacity: maximum allocated size

if(size == capacity)
Pre: ---- Reallocate with double size
Post: ‘element’ is inserted in location ‘index’ of list L Then Insert in the input location
Input: list L, L’s size, L’s capacity, ‘element’, ‘index’
Output: no return
Assumption: extending the list is by factor 2

Algorithm: InsertAt(L1, size, capacity, element, index)


IF ( size == capacity ) // Reallocation with larger capacity is needed
// Let’s say with double capacity
Allocate listCopy with same capacity of L
Copy L list elements in listCopy
Reallocate L list with: capacity * 2
Copy listCopy elements (from 0 to index-1) to the new L list (from 0 to index-1)
Insert ‘element’ at location index’ of list L
Copy listCopy elements (from index to size-1) to the new L list (from index+1 to size)
Update L’s capacity variable with the new capacity: capacity * 2
ELSE
Shift element from L (from index to size-1) one slot right
Insert ‘element’ at location index’ of list L
END IF
Update L’s size variable with the new size: size + 1
END Algorithm

Note: Some of the above algorithm steps need to be written in more details

CMP N102 2/3 Spring 2018


Data Structures & Algorithms Sheet #2 - Solution

9. Write an algorithm to insert an element x in a sorted array. The element should be inserted in
the position that keeps the array sorted. If the element already exists in the array, it should not
be inserted.

Note: The solution provided here assumes that the allocated capacity (max size) of the array
is much larger than the actual number of existing elements, n, so NO re-allocation is needed.

However, you should write the 2 cases in your final solution; the case if (n == capacity) and
the case if (n < capacity).

Pre: input array is sorted


Post: if element x is not found, element x is inserted in location that keeps array sorted
Input: array, array size n, element x
Output: the updated n (array size)

Algorithm SortedInsert(array, n, x)
IF ( last array element < x ) // so all array elements are less than x too
Insert x after the last array element
Increment n
Return
END IF
FOR each element in array
IF ( x == element )
Return
ELSE IF ( x > element )
Shift array elements, from current index to last, one slot right
Insert x in current index
Increment n
Return
END IF
END FOR
END Algorithm

Note: Some of the above algorithm steps need to be written in more details

CMP N102 3/3 Spring 2018

You might also like