0% found this document useful (0 votes)
14 views14 pages

Chapter 5 - Lists - Exercises

The document contains exercises related to data structures and algorithms, specifically focusing on lists. It includes methods for creating lists from arrays, searching for elements, inserting elements in sorted order, finding maximum values, and various recursive operations. Additionally, it presents exercises for dividing lists, merging ordered lists, deleting nodes with minimum values, and printing list elements backward.

Uploaded by

hadytarabay12
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)
14 views14 pages

Chapter 5 - Lists - Exercises

The document contains exercises related to data structures and algorithms, specifically focusing on lists. It includes methods for creating lists from arrays, searching for elements, inserting elements in sorted order, finding maximum values, and various recursive operations. Additionally, it presents exercises for dividing lists, merging ordered lists, deleting nodes with minimum values, and printing list elements backward.

Uploaded by

hadytarabay12
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/ 14

Data Structures and Algorithms

Chapter 5 – Lists - Exercises

Dr. Georges Badr

1
 Exercise 1: Write a method that create a list from the elements of an
array passed as parameter

//create a list from array


SinglyLinkedList<T> array2List(T *arr, int n) {
SinglyLinkedList<T> ssl;
for (int i = 0; i < n; i++)
ssl.insert(arr[i],i);

return ssl;
}
 Exercise 2: Write a method that searches for a specific element in the list

//search for an element and return a pointer to the node where


the element exists
Node<T>* search (T element)
{
Node<T>*cur = head;

while(cur!=NULL)
{
if(cur->getData() == element)
return cur;
cur = cur->getNext();
}

return NULL;
}
 Exercise 3: Write a method insertSorted that allows the insertion of an element in its
correct place in a sorted list. (we will consider the list sorted in ascending order)
void insertSorted (T element)
{
Node<T>*n = new Node<T>(element);
if(empty()) //search for the position
{ Node<T>* cur = head;
head = tail = n; while(cur->getNext()!=NULL && cur->getNext()->getData() <
sz=1; element)
return; {
} cur = cur->getNext();
}
//check if element < head
if(element<head->getData()) //insert after cur by adjusting the links
{ n->setNext(cur->getNext());
push_front(element); cur->setNext(n);
return; sz++;
} }

//check if element > tail


if(element > tail->getData())
{
push_back(element);
return;
}
 Exercise 4: Write a method returns the maximum of a list
//find the maximum value. The method returns the value of the max
T getMax ()
{
Node <T>* cur;
T max = head ->getData();
for(cur = head->getNext(); cur!=NULL; cur = cur->getNext())
{
if(cur->getData() > max)
max = cur->getData();
}
return max;
}

//find the maximum node. The method returns a pointer to the node holding the maximum value
Node<T>* getMaxp()
{
Node <T>* cur;
Node <T>* max = head;
for(cur = head->getNext(); cur!=NULL; cur = cur->getNext())
{
if(cur->getData() > max->getData())
max = cur;
}
return max;
}
Exercise 1
• Write a method void divide (SinglyLinkedList &oddList,
SinglyLinkedList &evenList) that divides the current list into 2 separate
lists. The first one contains the odd number and the second contains even
numbers.

Exercise 2
• Write a method that merges two ordered list of integers into a single
ordered list of integers. The functions takes one list as parameter and returns
a new list containing the elements of the current object merged with the
elements of the list passed as parameter

Exercise 3
• Write a method that deletes the node with the minimum value from a
doubly linked list

6
Exercise 4
• Write a Recursive method that calculates the sum of the elements in a list
(consider that the elements have number type

Exercise 5: Recursively Print a List Backward)


• Write a method printListBackward that recursively outputs the items in a
linked list object in reverse order.

Exercise 6: Recursively Search a List


• Write a member function searchList that recursively searches a linked list
object for a specified value. The function should return a pointer to the value if it is
found; otherwise, NULL should be returned.

7
Solutions

8
Exercise 1
• Write a method void divide (SinglyLinkedList &oddList,
SinglyLinkedList &evenList) that divides the current list into 2 separate
lists. The first one contains the odd number and the second contains even
numbers.
// Method to divide the list into odd and even lists
void divide(SinglyLinkedList<T> &oddList, SinglyLinkedList<T> &evenList) {
Node<T>* current = head;

while (current!=NULL) {
if (current->data % 2 == 0) {
evenList.push_front(current->data);
} else {
oddList.push_front(current->data);
}
current = current->next;
}
}

9
Exercise 2
• Write a method that merges two ordered list of integers into a single ordered
list of integers. The functions takes one list as parameter and returns a new
list containing the elements of the current object merged with the elements
of the list passed as parameter

// Method to merge the current list with another list (list2)


//suppose you 2 lists, list1 and list2 sorted that will be merged in an empty list mergedList
//list1 will be the object calling the method with list2 passed as parameter
//the call for this method will be: mergedList = list1.merge(list2)

SinglyLinkedList<T> merge(SinglyLinkedList<T>list2) {
//create an empty list
SinglyLinkedList<T> mergedList;

//initialize the iterators to the beginning of each list


Node<T>* current1 = head; //the current object
Node<T>* current2 = list2.begin();

10
//while there exist data in the current object AND list2
while (current1!=NULL && current2!=NULL) {
//compare the elements pointed by the iterators (current1, current2) of each list
//insert the smaller element
//move the iterator to the next node in the list
if (current1->getData() <= current2->getData()) {
mergedList.push_back(current1->getData());
current1 = current1->getNext();} else {
mergedList.push_back(current2->getData());
current2 = current2->getNext();
}
}

//when we exit the previous loop, either the current object or list2 is totally parsed.
// Add remaining elements from current1, if any
while (current1!=NULL) {
mergedList.push_back(current1->getData());
current1 = current1->getNext();
}

// Add remaining elements from current2, if any


while (current2!=NULL) {
mergedList.push_back(current2->getData());
current2 = current2->getNext();
}

//return the merged list


return mergedList;
}

11
Exercise 3
• Write a method that deletes the node with the minimum value from a doubly
linked list

void deleteMin()
{
//find the minimum value
//call remove method
T minimum = head->getData();
Node<T>* cur = head;
while(cur !=NULL)
{
if (cur->getData() < minimum)
{
minimum = cur->getData();
}
cur = cur->getNext();
}
remove(minimum);
}

12
Exercise 4
• Write a Recursive method that calculates the sum of the elements in a list
(consider that the elements have number type

T Sum (Node<T>* cur) {

if (cur==NULL) // Base case: if cur is null, return 0

return 0;

return cur->getData() + Sum(cur->getNext()); // Recursive case

13
Exercise 5: Recursively Print a List Backward)
• Write a method printListBackward that recursively outputs the items in a
linked list object in reverse order.

void printListBackward(Node<T>* cur) {


if (cur==NULL) // Base case: if cur is null, return
return;

printListBackward(cur->getNext()); // Recursive call for the next node


cout << cur->getData() << " "; // Print the current node after the recursive call
}

14

You might also like