CSC508 Test 1 - Set 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CONFIDENTIAL CS/DEC 2022/CSC508/TEST 1

UNIVERSITI TEKNOLOGI MARA


TEST 1 (SET 2)

COURSE : DATA STRUCTURES


COURSE CODE : CSC508
DATE : DEC 2022
DURATION : 1 HOUR

INSTRUCTION TO CANDIDATES
1. This question paper consists of four (3) questions.
2. Answer ALL questions in your answer sheet. Start each answer on a new page. Write your
name, id and group on the answer sheet.
3. Please scan and save your answers as ONE pdf file.
4. No discussion and do not share your answers with other students. Any copying of or
plagiarized answers will be awarded 0 mark.
5. Answer ALL questions in English.

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO

This examination paper consists of 5 printed pages


CONFIDENTIAL 2 CS/DEC 2022/CSC508/TEST 1

Answer ALL questions.

QUESTION 1 (10 marks)

(a) Describe the relationship between data structures and algorithm.


(2 marks)
(b) Differentiate linear and non-linear data structures
(4 marks)

(c) Describe the implementation of queue using linked list


(4 marks)

QUESTION 2 (12 marks)

class patient{
String ID;
String name;
int age;
patient link;
}

class clinicQueue{
//Attribute 1
//Attribute 2
//Attribute 3

clinicQueue() {...}
//constructor, initialize the queue

public void enqueue(...) {...}


//add a new patient into the queue

public Object dequeue() {...}


//call a patient for treatment

public int waiting() {...}


//return the number of patient waiting in queue

The above program is use define a queue management system (QMS) in a clinic, based on
linked list. The class patient is defines the node, and clinicQueue defines linked list.

(a) Declare the three attributes for class clinicQueue, the efficiency enqueue() and
waiting() is O(1).
(3 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/DEC 2022/CSC508/TEST 1

(b) Define the constructor clinicQueue () to initialize the queue


(3 marks)

(c) The clinic management wanted to give priority for patients aged 60 years and above,
where they can skip three patients younger than them at the end of the queue. Define the
method enqueue(), with appropriate parameters, to suit this rule.
(6 marks)

QUESTION 3 (8 marks)

import java.util.Scanner;

class stack {
int []arrStack;
int top;

public stack(){
arrStack = new int[10];
top = -1;}

public void push(int x) {


top++;
arrStack[top] = x;}

public void pop() {


top--;
}

public boolean isEmpty() {


if (top < 0)
return true;
else
return false;}
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/DEC 2022/CSC508/TEST 1

public class Question1 {


public static void main(String[] args) {
int i;
stack match = new stack();
String str;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
str = sc.nextLine();
System.out.println ("Initial Top : " + match.top);
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == '(')
match.push(i);
if (str.charAt(i) == ')'){
match.pop();
}
System.out.println ("Top : " + match.top);
}

if (match.isEmpty())
System.out.println("MATCHED");
else
System.out.println("NOT MATCHED");
}
}
The above program is intended to check for parenthesis matching.

(a) Trace the output of the program when the input (a(bc)d)efg(h))is entered.
(4 marks)

(b) Based on your finding in (a), suggest an update to improve the program.

(4 marks)

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like