Chapter 5 - Lists - Exercises
Chapter 5 - Lists - Exercises
1
Exercise 1: Write a method that create a list from the elements of an
array passed as parameter
return ssl;
}
Exercise 2: Write a method that searches for a specific element in the list
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++;
} }
//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
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
SinglyLinkedList<T> merge(SinglyLinkedList<T>list2) {
//create an empty list
SinglyLinkedList<T> mergedList;
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();
}
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
return 0;
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.
14