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

LAB EXERCISE 3 (SECJ2013)

DATA STRUCTURE AND ALGORITHM


SECTION 04, SEM 1, 2021/2022

INSTRUCTIONS TO THE STUDENTS


 This exercise must be done individually.
 Please refer to the list of question sets to find out your set of questions.
 Your program must follow the input and output as required in the text and shown in the examples.
You must test the programs with (but not limited to) all the input given in the examples.
 Any form of plagiarisms is NOT ALLOWED. Students who copied other students’ codes will get
ZERO marks (both parties, students who copied, and students that share their work).
 Please insert your name, matrics number, and date as a comments in your program.

SUBMISSION PROCEDURE
 Please submit this exercise no later than December 15, 2021, Wednesday (14:00 MYT).
 Only one file is required for the submission which is the source code (the file with the extension
.cpp).
 Submit the exercise via the UTM’s e-learning system.

QUESTION
Given a class diagram for List as in Figure 1. Complete a linked list program given in
Program 1. Define List class and write the implementation for all the function members for
List to accomplish each of the following tasks:

List
- head: Node*
- tail: Node*
+ funct1(Node*): void
+ funct2(Node*): void
+ funct3(Node*, int): void
+ funct4(int): void
+ funct5(): void
+ funct6(): void

Figure 1: List class

1 //Program 1
2 class Node {
3 public:
4 string name;
5 float price;
6 Node *after, *before;
7 Node(string n=NULL, float p=0) {
8 name = n;
9 price = p;
10 after = NULL;

LMY 2021/2022 1
11 before = NULL;
12 }
13 };

Task 1: Define a function named funct1. The function should able to insert a new item node
into an empty list. Figure 2 shows the result of this task after the execution of the
funct1 function.

before name price after

Durian 9.59

head tail

Figure 2: Current node in the list after insertion node in the empty list

Task 2: Define a function named funct2. The function should able to insert a new item node
in front of the list. Figure 3 shows the result of this task after the execution of the
funct2 function.

before name price after before name price after

Orange 3.45 Durian 9.59

head tail

Figure 3: Current nodes in the list after insertion node in front of the list

Task 3: Define a function named funct3. The function should able to insert a new item node
at the middle of the list. The position of the new node in the list depends on the value
of the second argument of this function. Assume the value of the second argument
of this function is 2. This means that the new node will be the second node in the list
(being in the second position in the list). Figure 4 and Figure 5 show the result of this
task after the execution of the funct3 function with 2 is the value for second
argument. Note: In this task, you should use a loop.

LMY 2021/2022 2
before name price after before name price after before name price after
Orange 3.45 Mango 5.69 Durian 9.59

head tail

Figure 4: Current nodes in the list after the first insertion node at the second position in the
list

before name price after before name price after before name price after before name price after
Orange 3.45 Apple 2.25 Mango 5.69 Durian 9.59

head tail

Figure 5: Current nodes in the list after the second insertion node at the
second position in the list

Task 4: Define a function named funct4. The function should able to delete the item node at
the middle of the list. The position of the node to delete in the list depends on the
value of the argument of this function. Assume the value of the argument of this
function is 2. This means that the node that will be deleted is the second node in the
list. Figure 6 shows the result of this task after the execution of the funct4 function
with 3 is the value for the argument. Note: In this task, you should use a loop.

before name price after before name price after before name price after
Orange 3.45 Apple 2.25 Durian 9.59

head tail

Figure 6: Current nodes in the list after the deletion of the second node in the list

Task 5: Define a function named funct5. The function should able to delete the last node in
the list. Figure 7 shows the result of this task after the execution of the funct5
function.

LMY 2021/2022 3
before name price after before name price after

Orange 3.45 Apple 2.25

head tail

Figure 7: Current nodes in the list after the deletion of the last node in the list

Task 6: Define a function named funct6. The function should able to print all data in the list
in backward order. Figure 8 shows the example result of this task after the execution
of the funct6 function. Note: In this task, you should use a loop. (6 marks)

Print backward:
[Durian 9.59] [Orange 3.45]

Figure 8: Sample output after the execution of the funct6 function

After completing all tasks, write your main function. Please make sure your program should
be able to produce the output as shown in Figure 9.

Print backward:
[Durian 9.59]

Print backward:
[Durian 9.59] [Orange 3.45]

Print backward:
[Durian 9.59] [Mango 5.69] [Orange 3.45]

Print backward:
[Durian 9.59] [Mango 5.69] [Apple 2.25] [Orange 3.45]

Print backward:
[Durian 9.59] [Apple 2.25] [Orange 3.45]

Print backward:
[Apple 2.25] [Orange 3.45]

Figure 9: The output of completed Program 1

LMY 2021/2022 4

You might also like