Transcript
Page 1: Linked lists - Exercises

Linked lists

Exercises

Eleonora Ciceri, Politecnico di Milano

Email: [email protected]

Page 2: Linked lists - Exercises

Simple exercises

Page 3: Linked lists - 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

Page 4: Linked lists - Exercises

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;

Page 5: Linked lists - Exercises

Maximum value in a list (recursive)

Write a recursive function that retrieves the maximum element

in a list

Base case?

Recursion step?

Page 6: Linked lists - Exercises

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));

}

Page 7: Linked lists - Exercises

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

Page 8: Linked lists - Exercises

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;

}

Page 9: Linked lists - Exercises

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

Page 10: Linked lists - Exercises

Reverse print of the list

void inversePrint(ListNode* head) {

if (head == NULL)

return;

else {

inversePrint(head->nextPtr);

std::cout << "- " << head->data << std::endl;

}

}

Page 11: Linked lists - Exercises

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

Page 12: Linked lists - Exercises

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;

}

Page 13: Linked lists - Exercises

Complex exercises

Page 14: Linked lists - 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

Page 15: Linked lists - Exercises

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

Page 16: Linked lists - Exercises

Polygonal chain:

Shortcut

Same starting

pointSame ending

point

Shorter path

Page 17: Linked lists - Exercises

Polygonal chain:

Shortcut(extreme points)

LineNode* findFirstElement(LineNode* list) {

return list;

}

LineNode* findLastElement(LineNode* list) {

while (list->nextPtr != NULL)

list = list->nextPtr;

return list;

}

Page 18: Linked lists - Exercises

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;

}

Page 19: Linked lists - Exercises

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;

}

Page 20: Linked lists - Exercises

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;

}

Page 21: Linked lists - Exercises

Polygonal chain:

Disjointed chains

Page 22: Linked lists - Exercises

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;

}

Page 23: Linked lists - Exercises

Polygonal chain:

Contained chain

Page 24: Linked lists - Exercises

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;

}

Page 25: Linked lists - Exercises

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;

}

Page 26: Linked lists - Exercises

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


Top Related