You are on page 1of 8
UNIVERSITI MALAYA UNIVERSITY OF MALAYA PEPERIKSAAN IJAZAH SARJANA MUDA KEJURUTERAAN EXAMINATION FOR THE DEGREE OF BACHELOR OF ENGINEERING SESI AKADEMIK 2018/2019 : SEMESTER II ACADEMIC SESSION 2018/2019 SEMESTER I KIE1008/KEET 1250 : PENGATURCARAAN II / STRUKTUR DATA PROGRAMMING I! /DATA STRUCTURE Jun 2019 Masa : 2 jam June 2019 Time : 2 hours ARAHAN KEPADA CALON : INSTRUCTIONS TO CANDIDATES : Jawab semua soaian. Answer all questions. (Kertas soalan ini mengandungi 3 soalan dalam 8 halaman yang dicetak) (This question paper consists of 3 questions on 8 printed pages) KIE1008 Soalan 1 Question 1 (@) Pertimbangkan kelas Li st dengan ahli-ahli private berikut: Consider the List class with the following private members: class List{ /* public members here ... */ private: J struct Node{ ListDataType item; // the data of the node Node* next: // points to the next node of the list he Node* head; // point to first node in the list |] []— Ee] Ga) oe} () Lukiskan rajah bagi senarai di atas setelah baris-baris kod di bawah dilaksanakan. Draw a diagram of the above list after the following lines of code have been executed: Node* prev = head->next; Node* nodeToInsert = new Node; nodetoinsert->item = 43 nodeToInsert->next = prev->next; prev->next = nodeToInsert; (4 markahimarks) (li) Anggapkan bahawa kod yang ditunjukkan di bahagian (i) di atas telah dilaksanakan, Apakah nilai bagi prev->item? ‘Assume that the code represented above in part (has executed. What is the value of prev->item? (2 markah/marks) (ii) Sebagai tambahan kepada kod di atas, anggapkan bahawa kod berikut dilaksanakan. Lukiskan rajah bagi senarai tersebut setelah kod itu dilakeanakan. In addition to the code above, assume the following code executes. Draw a diagram of the list after this code executes as well, prev = prev->next; prev ~ prev->next: Node curr = prev->next; prev-onext = curr-onex delete curr; curr = NULL; (4 markahimarks) 28 KIE1008 (b) Tunjukkan apa yang dihasilkan oleh kod C++ berikut. Anggapkan bahawa nod berada dalam bentuk info-link yang biasa dengan info adalah berjenis int. (List dan ptr adalah penunjuk dengan jenis nodeType) Show what is produced by the following C++ code. Assume the node is in the usual info- Link form with the info of type int. (List. and ptr are pointers of type nodetype.) list = new nodeType; list->info = 10; ptr = new nodeType; ptr->info = 13; ptr->link = NULL; list->Link = ptr; ptr = new nodeType; ptr->info = 18; ptr->link = list->link; list->link = ptr; cout << list-pinfo <<" " << ptr->info <<" "; ptr = ptr->links cout << ptr->info << endl; (3 markah/marks) (©) Pertimbangkan senarai bersambung yang ditunjukkan di bawah. Anggapka bahawa nod ini adalah dalam bentuk maklumat-link biasa. Gunakan senarai untuk menjawab soalan berikut. Isyttharkan pembolehubah tambahan jika perlu. Menganggap 1s, p, A, dan 2 adalah petunjuk jenis nodeType. Tulis kenyataan C++ berikut: (a) Consider the linked list shown below. Assume that the nodes are in the usual info-link form. Use this list to answer the following questions. Declare additional variables if necessary, Assume that list, p, A, and B are pointers of type nodeType. Write C++ statements to do the following: Las [+78] Hs [fis fe (1 [+450] 4 Aly B i. Membuat fist menunjuk ke nod yang mengandungi 43 Make list point to the node containing 43. list. [Hai ii, | Membuat B menunjuk ke nod terakhir dalam senarai. Make B point to the last node in the list. iii, Membuat fist menunjuk ke suatu senarai kosong. Make list point to an empty list. iv. Menetapkan nilai nod yang mengandungi 91 kepada 35. 318 KIE1008 Set the value of the node containing 91 to 35. Mereka dan memasukkan nod dengan info 25 selepas nod yang ditunjukkan oleh A. Create and insert the node with info 25 after the node pointed to by A. (7 markah/marks} 4/8 KIE1008 Soalan2 Question 2 () (b) Dalam mencari suatu elemen dalam suatu tatasusunan, pencarian lelurus boleh digunakan, walapun mudah untuk dilaksanakan, tetapi tidak efisien, dengan kompleksifi masa hanya O(n). Dengan menganggap tatasusunan telah tersedia tersusun, ubah fungsi search di bawah, menggunakan algoritma yang lebih bagus, supaya kompleksiti masa purata untuk fungsi pencarian itu adalah O(log n). In searching an element in an array, linear search can be used, even-though simple to implement, but not efficient, with only Ofn) time complexity. Assuming the array is already in sorted order, modify the search function below, using a better algorithm, 80 the average time comploxity for the search function is O(log n). inelude using namespace star int search(int af], int s, int v) ( 11 Modity below code: for G@nt i= 0; 4< if (a[a] = v) return i; ae) ) return -1; Ant main() int intArray(10l - ( § 7) 8, 9, 10, 12, 23, 15, 20, 3400 // Seach tor element ‘12' in 1-elements integer array. cout << search(intArray, 10, 12); // "5! will be printed out. // Search for element '35' in 10-elements integer array. cout using namespace stay void selectionSort (int a{], int s) ( / Add your codes here. int main() Ant intArray(10] = { 12, 15, 7 8, 9% 20, 15, 34, 10, 5 be selectionSort {intArray, 10); // "5 78910 12 1318 20 34" will be printed out. for (int d= 07 1.< 10 7 44+) ( cour << intArayft] << 7%} return 0; {6 markah / marks) 6/8 Soalan 3 Question 3 @ KIE1008 Untuk pekok AVL yang diberikan dalam rajah S3a, jawab soalan-soalan berikut. (Anggapkan setiap soalan adalah bebas) For the AVL tree given in figure Q3a, answer the following questions. (Assume each question is independent) Rajah S3a / Figure Q3a Apakah perbezaan antara pokok binari dan pokok AVL? What is the difference between binary tree and AVL tree? (2 markah / marks) Berikan ketiga-tiga turutan penyusuran. Give the three traversal sequences. (@ markah / marks) ‘Tunjukkan langkah-tangkah penambahan nod F. Show the steps of adding node F. (3 markah / marks) Tunjukkan langkah-langah penghapusan nod akar. Show the steps of deleting the root. (2 markah / marks) 718 KIE1008 (6) Untuk graf yang diberikan dalam rajah S3b, jawab soalan-soalan berikut. For the graph given in figure Q3b, answer the following questions. 15 Rajah S3b / Figure Q3b i, Berikan dua perbezaan antara pokok dan graf. Give two differences between a tree and a graph. (2 markah / marks) ii. Apakah itu pokok perentang? What is @ spanning trae? (2 markah / marks) iii, Rekabentukkan satu algoritma untuk mencari pokok perentang minimum (berikan pseudokod atau terangkan dalam perkataan) dan gunakan algorithma tersebut untuk meneari pokok perentang minimum untuk graf yang diberikan. Design an algorithm to find the minimum spanning tree (give pseudocode or explain in words) and apply the algorithm to find the minimum spanning tree for the graph given, (4 markah / marks) jv. Berikan satu lagi pokok perentang untuk graf yang diberikan dan tunjukkan kosnya lebih tinggi daripada kok pokok perentang minimum. Give one more spanning tree for the graph given and show that the cost is greater than the cost of minimum spanning tree. (2 markah / marks) TAMAT END a/8

You might also like