Docsity

Linked Lists - Applied Data Structures - Past Exam, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CORK INSTITUTE OF TECHNOLOGY
INSTITIÚID TEICNEOLAÍOCHTA CHORCAÍ
Autumn Examinations 2008/09
Module Title: Applied Data Structures
Module Code: COMP 6012
School: Computing
Programme Title: Bachelor of Science in Computing – Year 2
Bachelor of Science (Honours) in Software Development – Year 2
Programme Code: KCOMP_7_Y2
KSDEV_8_Y2
KCOME_6_Y2
External Examiner(s): Ms. M. Meagher
Internal Examiner(s): Ms. G. McSweeney
Instructions: Answer Question 1 and TWO other questions
Duration: 2 Hours
Sitting: Autumn 2009
Requirements for this examination:
Note to Candidates: Please check the Programme Title and the Module Title to ensure that you have received the
correct examination paper.
If in doubt please contact an Invigilator.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Linked Lists - Applied Data Structures - Past Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

CORK INSTITUTE OF TECHNOLOGY

INSTITIÚID TEICNEOLAÍOCHTA CHORCAÍ

Autumn Examinations 2008/

Module Title: Applied Data Structures

Module Code: COMP 6012

School: Computing

Programme Title: Bachelor of Science in Computing – Year 2 Bachelor of Science (Honours) in Software Development – Year 2

Programme Code: KCOMP_7_Y KSDEV_8_Y KCOME_6_Y

External Examiner(s): Ms. M. Meagher Internal Examiner(s): Ms. G. McSweeney

Instructions: Answer Question 1 and TWO other questions

Duration: 2 Hours

Sitting: Autumn 2009

Requirements for this examination:

Note to Candidates: Please check the Programme Title and the Module Title to ensure that you have received the correct examination paper. If in doubt please contact an Invigilator.

Q1 is compulsory

Section A Compare and contrast how Dynamic Memory Allocation is used in Linked Lists as distinct from Dynamic Arrays. 2 marks

Section B Data to be stored in a node of a linked list should include the following data on route information for booking flights

9 flight id 9 route id 9 number of adults 9 number of children 9 total price for flight

(i) Write the code to set up a node for a given flight 2 marks

(ii) Write a function for saving the list of bookings to a file emptying it in the process 5 marks

(iii) Write the code to search for the previous position in a list according to flight information 6 marks

(iv) Write the code for deleting a booking from a list in order of flight id ( delete a node function ) 3 marks

Section B Î 16 marks

Question 2 Binary Trees

(i) Set up a root Node as used in Q1 to hold the flight id, the route id, the number of adults, the number of children, the total cost and 2 pointers left and right 4 marks (ii) Set up the memory for the node in (i) and set the left and right pointer to NULL 3 marks

(iii) Write a function for displaying the node given in (i) to a binary tree based on the flightId 5 marks

(iv) Explain how to use recursion to add 99 to the tree in (vii) if it is already in the tree 4 marks

(v) Build up the expression given the following PostOrder traversal example 4 marks (vi) How Post Order traversal work for the diagram below 3 marks

Question 2 on Binary Tree continued

(vii) Given the following tree, demonstrate how to use recursion down to the lowest level to print out the contents of the following tree using post order traversal.

7 marks

Question 2 Î 30 marks

Root

Section C: Main Program

(i) Set up Linear Queue for code provided in section B 2 marks

(ii) While the queue is not full, read in the details of a customer account from the queue 5 marks

Section C Î 7 marks

Question 3 Î 30 marks

Question 4

Section A

(i) What is recursion? 2 marks (ii) What is the function of an anchor statement? 2 marks (iii) Write the recursion function for finding n! 3 marks (iv) Show the steps involved of finding of 5!. Show how a stack is involved in the process 8 marks

Section A Î 15 marks

Section B

Read the following code void Sort( int x[], int n) { int smallPos, smallest; for( int i=0; i < n; i++) { smallPos = i; smallest = x[ smallPos ];

for(int j=i +1 ; j < n ; j++)

if ( x[j] < smallest) { smallPos = j; smallest = x [smallPos ]; }

x[ smallPos ] = x[i ]; x[i] = smallest; } }

(i) Show the steps involved for making 2 passes through the code for sorting the following numbers 33, 22, 11, 55, 44 10 marks (ii) Explain in English how this algorithm works 2 marks (iii) Name this sort and compare with the bubble sort. 3 marks

Section B Î 15 marks

Question 4 Î 30 marks

Appendix 3 tree * remove_node( tree * parent, int num, int *success) { tree * temp; int new_num;

if( parent != NULL) { if ( num ==parent->num) { *success = 1; if (parent->left ==NULL && parent->right == NULL) { temp = parent; free(temp); parent = NULL; } else if (parent->left ==NULL) { temp = parent; parent = parent->right; free( temp); } else if (parent->right ==NULL) { temp = parent; parent = parent->left; free( temp); } else // find the left most node on the right side { parent-> right = successor(parent->right, &num); parent->num = num; } }// number not equal to parent_number

else if ( num < parent->num) parent->left = remove_node(parent->left, num, success); else parent->right = remove_node(parent->right, num, success); } else //parent ==null *success =0; return parent; }//end function

tree * successor( tree * parent, int * num) { tree * temp;

if ( parent != NULL) { if ( parent -> left == NULL) { *num = parent->num; temp = parent; parent = parent ->right; free(temp); } else parent->left = successor( parent->left, num); } return parent; }