linked lists - exercises
TRANSCRIPT
Simple exercises
Sum of list elements
Let sumElements() be the function that:
Gets as input parameters:
The list head
An integer M
Returns the sum of the values in the list that are multiples of M
Sum of list elements
ListNode* currentNode = head;
int sum = 0;
while (currentNode != NULL) {
if (currentNode->data % M == 0)
sum = sum + currentNode->data;
currentNode = currentNode->nextPtr;
}
return sum;
Maximum value in a list (recursive)
Write a recursive function that retrieves the maximum element
in a list
Base case?
Recursion step?
Maximum value in a list (recursive)
int findMax(ListNode* head) {
if (head == NULL)
return -1; // Assuming values will be all positive
else
return std::max(head->data,
findMax(head->nextPtr));
}
Even minimum number
Define a function that, given a list of integer numbers, returns
the position (pointer) of the even minimum element in the list
Even minimum number
int findEvenMin(ListNode* head) {
ListNode* currentNode = head;
int min = std::numeric_limits<int>::max();
while (currentNode != NULL) {
if (currentNode->data % 2 == 0)
min = std::min(currentNode->data, min);
currentNode = currentNode->nextPtr;
}
return min;
}
Reverse print of the list
Write a function that prints the values in the list in reverse
order with respect to the order in which they are stored in the
list
Reverse print of the list
void inversePrint(ListNode* head) {
if (head == NULL)
return;
else {
inversePrint(head->nextPtr);
std::cout << "- " << head->data << std::endl;
}
}
Find peaks
Write a function that takes a list and finds its peaks
The peaks are defined as values preceded and followed by
values that are smaller than their own half
Find peaks
std::vector<int> findPeaks(ListNode* head) {
ListNode* currentNode = head->nextPtr;
ListNode* previousNode = head;
std::vector<int> peaks;
while (currentNode != NULL) {
if (previousNode->data < (float)currentNode->data/2 &&
currentNode->nextPtr->data < (float)currentNode->data/2)
peaks.push_back(currentNode->data);
previousNode = currentNode;
currentNode = currentNode->nextPtr;
}
return peaks;
}
Complex exercises
Polygonal chain
A polygonal chain is a connected series of line segments; we suppose such segments are composed of points which are all different between each other
The length of such polygonal chain is the summation of the distances between two consecutive points
Naming convention
Two polygonal chains are disjointed if they do not share any point
B is a shortcut for A if B shares the same starting and ending point of A, but is shorter
A contains B if B is a subsequence of the points in A
Polygonal chain
Write the following functions:
Shortcut: tells if B is a shortcut of A
Disjoint: tells if A and B are disjointed
Contains: tells if A contains B
Polygonal chain:
Shortcut
Same starting
pointSame ending
point
Shorter path
Polygonal chain:
Shortcut(extreme points)
LineNode* findFirstElement(LineNode* list) {
return list;
}
LineNode* findLastElement(LineNode* list) {
while (list->nextPtr != NULL)
list = list->nextPtr;
return list;
}
Polygonal chain:
Shortcut(length)
float computeLineLength(LineNode* head) {
LineNode* currentNode = head->nextPtr;
LineNode* previousNode = head;
float length = 0;
while (currentNode != NULL) {
length += computeDistance(currentNode, previousNode);
previousNode = currentNode;
currentNode = currentNode->nextPtr;
}
return length;
}
Polygonal chain:
Shortcut(distance between two points)
float computeDistance(LineNode* firstPt, LineNode* secondPt) {
int x1 = firstPt->x;
int y1 = firstPt->y;
int x2 = secondPt->x;
int y2 = secondPt->y;
int distance = std::sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
return distance;
}
Po
lyg
ona
l ch
ain
:
Sh
ortc
ut
bool isShortcut(LineNode* A, LineNode* B) {
if (A == NULL || B == NULL)
return false;
LineNode* firstElementA = findFirstElement(A);
LineNode* lastElementA = findLastElement(A);
LineNode* firstElementB = findFirstElement(B);
LineNode* lastElementB = findLastElement(B);
if (firstElementA->x == firstElementB->x &&
firstElementA->y == firstElementB->y &&
lastElementA->x == lastElementB->x &&
lastElementB->y == lastElementB->y) {
float lengthA = computeLineLength(A);
float lengthB = computeLineLength(B);
if (lengthB < lengthA)
return true;
else
return false;
}
else
return false;
}
Polygonal chain:
Disjointed chains
Po
lyg
ona
lch
ain
:
Dis
join
ted
ch
ain
s
bool isDisjointed(LineNode* A, LineNode* B) {
bool disjointed = true;
LineNode* currentNodeA = A;
while (currentNodeA != NULL) {
LineNode* currentNodeB = B;
int xA = currentNodeA -> x;
int yA = currentNodeA -> y;
while (currentNodeB != NULL) {
int xB = currentNodeB -> x;
int yB = currentNodeB -> y;
if (xA == xB && yA == yB) {
disjointed = false;
break;
}
currentNodeB = currentNodeB -> nextPtr;
}
if (disjointed == false)
break;
currentNodeA = currentNodeA -> nextPtr;
}
return disjointed;
}
Polygonal chain:
Contained chain
Po
lyg
ona
lch
ain
:
Co
nta
ine
dch
ain
bool isEqualFromHere(LineNode* A, LineNode*B) {
LineNode* currentNodeA = A;
LineNode* currentNodeB = B;
bool equal = true;
while (currentNodeA != NULL && currentNodeB != NULL) {
int xA = currentNodeA->x;
int yA = currentNodeA->y;
int xB = currentNodeB->x;
int yB = currentNodeB->y;
if (xA != xB || yA != yB) {
equal = false;
break;
}
currentNodeA = currentNodeA->nextPtr;
currentNodeB = currentNodeB->nextPtr;
}
if (currentNodeA == NULL && currentNodeB != NULL)
equal = false;
return equal;
}
Po
lyg
ona
lch
ain
:
Co
nta
ine
dch
ain
bool isContained(LineNode* A, LineNode* B) {
LineNode* currentNodeA = A;
bool contained = false;
LineNode* currentNodeB = B;
int xB = currentNodeB->x;
int yB = currentNodeB->y;
while (currentNodeA != NULL) {
int xA = currentNodeA->x;
int yA = currentNodeA->y;
if (xA == xB && yA == yB)
contained = isEqualFromHere(currentNodeA,
currentNodeB);
if (contained == true)
break;
currentNodeA = currentNodeA -> nextPtr;
}
return contained;
}
Polygonal chain
Additional exercises:
Define the extension function, which tells if A extends B; A
extends B if A contains B and (A,B) share the ending point
Define the concatenate function, which returns the concatenation
of B after A
Define the tortuosity function, which returns the ratio between the
length of A and the distance between its extreme points