exercises final exam part1
DESCRIPTION
dgfgTRANSCRIPT
![Page 1: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/1.jpg)
Algorithms and Data Structures
Exercise for the Final Exam (17 June 2014)
Stack, Queue, Lists, Trees, Heap
![Page 2: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/2.jpg)
Singly linked list (1)
Data about exam results are stored into a singly linked list. Each list
element consists of:
• student name(50+1 character)
• student ID (int)
• course code (int)
• grade (int)
The list is not sorted. Write the function that removes
students with negative grades (those with grade 1) from the list. The
function returns the number of removed list members. Write the
corresponding struct.
![Page 3: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/3.jpg)
Solution (1/2)
struct record {
char name[50+1];
int id;
int code;
int grade;
};
struct at {
struct record element;
struct at *next
};
typedef struct at atom;
![Page 4: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/4.jpg)
Solution (2/2)
int removeNegative(atom **head){
int removed = 0;
atom *tmp;
while(*head) {
if ((*head)->element.grade == 1) {
removed++;
tmp = *head;
*head = (*head)->next;
free(tmp);
} else {
head = &((*head)->next);
}
}
return removed;
}
![Page 5: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/5.jpg)
Singly linked list (2)
Write the function that removes even numbers from the list that
contains integers.
At the beginning, the head from the function points to the head from the main program, then it points to pointers sljed, all of which are struct members. If the first node contains even number, then the statement:
*glava=(*glava)->sljed;
changes the pointer head from the main program, otherwise it changes pointer sljed which is the struct member.
![Page 6: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/6.jpg)
Solution 1 – with an auxiliary pointer for the previous atom
void remove Even(atom **head) {
atom *old;
atom *tmp = *head;
while ((tmp != NULL) && (tmp->element % 2 == 0)) {
tmp = (*head)->next;
free (*head);
*head = tmp;
}
while (tmp != NULL) {
while ((tmp-> next != NULL) && ((tmp-> next)->element % 2 != 0))
tmp = tmp-> next;
if (tmp-> next != NULL) {
old = tmp-> next;
tmp-> next = old-> next;
free(old);
}
else tmp = tmp-> next;
}
}
![Page 7: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/7.jpg)
Solution 2 – without an auxiliary pointer for the previous atom
void removeEven(atom **headp){
int removed = 0;
atom *tmp;
while(*headp) {
if (!((*headp)->element % 2)) {
tmp = *headp;
*headp = (*headp)->next;
free(tmp);
} else {
headp = &((*headp)->next);
}
}
}
![Page 8: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/8.jpg)
Singly linked list (3) - homework
Given the two lists sorted in an ascending order, write the recursive
function that merges the two lists into one list, also sorted in the
ascending order. Return that list into the main program.
The protptype of the function is:
atom *merge(atom *h1, atom *h2);
![Page 9: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/9.jpg)
Doubly linked list
![Page 10: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/10.jpg)
Doubly linked list contains data about students, sorted in a
descending order (from head) according to the average grades. The
list is defined with the following structures:struct record {
char name[80+1];
float avggrade;
};
struct at {
struct record element;
struct at *next;
struct at *prev;
};
typedef struct at atom;
Write the function that reverses the order of elements in the doubly
linked list. The prototype of the function is:void reverseList(atom **head, atom **tail);
Doubly linked list
![Page 11: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/11.jpg)
Solution
void reverseList(atom **head, atom **tail){
atom *aux,*aux1;
aux = *head;
*head = *tail;
*tail = aux;
while(aux != NULL){
aux1 = aux->next;
aux->next = aux->prev;
aux->prev = aux1;
aux = aux->prev;
}
}
![Page 12: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/12.jpg)
Tree containing data about products (name and price) is given with the
following structures:
typedef struct {
char name[20];
float price;
} Element;
struct n{
Element element;
struct n *left, *right;
};
typedef struct n node;
Trees
![Page 13: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/13.jpg)
Trees (1)
Write the function that will change the given tree into its mirror copy
(for each node – replace its left and right child). The prototype of the
function is:
node *mirrorTree (node *root);
![Page 14: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/14.jpg)
Solution
node *mirrorTree(node *root){
if (root){
node *tmp;
tmp = root->left;
root->left = mirrorTree(root->right);
root->right = mirrorTree(tmp);
}
return root;
}
![Page 15: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/15.jpg)
A binary search tree is sorted according to the price. Write the function
with the prototype:
int search(node *root, float price);
The function returns 1 if the tree contains the product with the
searched price, otherwise it returns 0.
Trees (2)
![Page 16: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/16.jpg)
Solution
int search(node *root, float price){
if (root){
if (price == root->element.price)
return 1;
else if (price < root->element.price)
return search(root->left, price);
else
return search(root->right, price);
}
return 0;
}
![Page 17: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/17.jpg)
Write the function with the prototype
int isHeap(node *root);
The function returns 1 if the complete binary tree (with the given root)
is heap, otherwise it returns 0.
Heap (1)
![Page 18: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/18.jpg)
Solution
int isHeap(node *root){
if (root == NULL)
return 1;
if ((root->left!=NULL) &&
(root>left->element > root->element))
return 0;
if ((root->right != NULL) &&
(root->right->element > root->element))
return 0;
return isHeap(root->left) && isHeap(root->right);
}
![Page 19: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/19.jpg)
Heap(1)
Assuming a heap is created for the following input sequence:
5, 6, 7, 7, 6, 8, 5, 9, using the algorithm with O(n) running time in the
worst case, illustrate all the steps of the creation process:
5
8
6
7 56
7
9
5
8
6
9 56
7
7
5
7
6
9 56
8
7
5
7
9
6 56
8
7
5
7
9
7 56
8
6
9
7
5
7 56
8
6
9
7
7
5 56
8
6
9
7
7
6 56
8
5
Solution:
![Page 20: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/20.jpg)
Assuming a heap is created for the following input sequence:
27, 49, 35, 19, 87, 53, 67, 29, using the algorithm with O(n) running
time in the worst case, illustrate all the steps of the creation process:
Heap (2)
27
49 35
29 87 53 67
19
27
49 67
29 87 53 35
19
27
87 67
29 49 53 35
19
87
27 67
29 49 53 35
19
Heap:
87
49 67
29 27 53 35
19
Solution:27
49 35
19 87 53 67
29
![Page 21: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/21.jpg)
Assuming a heap is created for the following input sequence:
15, 23, 7, 11, 40, 5, 90, using the algorithm with O(nlog2n) runnning
time in the worst case, illustrate all the steps of the creation process:
Heap (3)
15
23
15
15
23
15 7
23
15
11
7
23
15
4011
7
23
23
1511
7
40
23
1511
7
5
40
Solution:
![Page 22: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/22.jpg)
Heap (3)
23
1511
7
905
40
23
1511
40
75
90
![Page 23: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/23.jpg)
The given heap is stored in the array:
llustrate the ascending heapsort.
Heap (4)
93 82 47 31 64 23 17 27
![Page 24: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/24.jpg)
Solution (1/8)
Initial heap:
27
82
6431
47
1723
93
93 82 47 31 64 23 17 27
Replace the element from the top of the heap with the last element from the unsorted array part:
82
6431
47
1723
27
27 82 47 31 64 23 17 93
![Page 25: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/25.jpg)
Solution 2/8
Adjust the heap:
64
2731
47
1723
82
82 64 47 31 27 23 17 93
Replace the element from the top of the heap with the last element from the unsorted array part:
64
2731
47
23
17
17 64 47 31 27 23 82 93
![Page 26: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/26.jpg)
Solution 3/8
Adjust the heap:
Replace the element from the top of the heap with the last element from the unsorted array part:
31
2717
47
23
64
64 31 47 17 27 23 82 93
31
2717
47
23
23 31 47 17 27 64 82 93
![Page 27: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/27.jpg)
Solution 4/8
Adjust the heap:
Replace the element from the top of the heap with the last element from the unsorted array part:
31
2717
23
47
47 31 23 17 27 64 82 93
31
17
23
27
27 31 23 17 47 64 82 93
![Page 28: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/28.jpg)
Solution 5/8
Adjust the heap:
Replace the element from the top of the heap with the last element from the unsorted array part:
27
17
23
31
31 27 23 17 47 64 82 93
27 23
17
17 27 23 31 47 64 82 93
![Page 29: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/29.jpg)
Solution 6/8
Adjust the heap:
Replace the element from the top of the heap with the last element from the unsorted array part:
17 23
27
27 17 23 31 47 64 82 93
17
23
23 17 27 31 47 64 82 93
![Page 30: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/30.jpg)
Solution 7/8
Adjust the heap:
Replace the element from the top of the heap with the last element from the unsorted array part:
17
23
23 17 27 31 47 64 82 93
17
17 23 27 31 47 64 82 93
![Page 31: Exercises Final Exam Part1](https://reader033.vdocuments.net/reader033/viewer/2022051215/563dbba9550346aa9aaf2aa7/html5/thumbnails/31.jpg)
Solution 8/8
Sorted array:
17 23 27 31 47 64 82 93