You are on page 1of 1

Data Structures & Algorithms Sheet #2

Cairo University
Faculty of Engineering
Computer Engineering Department
Data Structures and Algorithms
Sheet #2
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 mustn’t contain
duplicates too.

2. Given two sorted lists L1 and L2, write an efficient algorithm to print sorted union L1 ∪ L2 using
only the basic list operations.
Assume that L1 and L2 are sets (no duplicates) and the printed numbers mustn’t contain
duplicates too.

3. Write a C++ class “Complex” which represents a complex number.


a. Write C++ functions that implement addition, multiplication and magnitude calculation
b. Write a main function that creates 2 complex numbers and displays their addition,
multiplication and magnitude results

4. Write a C++ class “Student” to store student information. The class should store the following
information:
- Student Name (max. 150 characters)
- Student ID
- Student grades in 40 courses

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?

6. Write a main function for problem 5 that:


a. Defines 5 students
b. Takes their data from user
c. Searches for a student by his id and displays his data if found

7. A common way of deletion is the so-called lazy deletion.


- To delete an element from a list, we mark it as deleted (using an extra bit or bool field)
- 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
b) Write C++ classes and functions to implement lazy deletion for sequential lists
Assume that deletion is by element value.
c) Write Search function to search for an element value in the list
d) Write Display function to display the non-deleted elements
e) Write a main function that creates a lazy deletion list and tests deleting and searching
operations

8. Write an algorithm to insert an element x at location i (0-based) in an array. What parameters


does the algorithm need to know about the array to work correctly and avoid unnecessary
operations?

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.

CMP N102 1/1 Spring 2018

You might also like