FAKULTI TEKNOLOGI
KEJURUTERAAN KELAUTAN
DAN INFORMATIK
2019/2020
DATA STRUCTURE & ALGORITHM
Lab 2: Singly
Linked list
VERSION 1
`
STUDENT INFORMATION
PLEASE FILL IN YOUR PERSONAL DETAILS:
NAME: TEOH YI YIN
MATRIC NUMBER:S58798
GROUP:K3
LAB:CISCO
DATE:2/11/2021
i
`
TABLE OF CONTENTS
INSTRUCTIONS .......................................................................................................................... 1
TASK 1: IMPLEMENTATION OF SINGLY LINKED LIST ............................................................. 2
TASK 2: APPLICATION OF SINGLY LINKED LIST....................................................................... 7
ii
`
INSTRUCTIONS
Manual makmal ini adalah untuk kegunaan pelajar-pelajar Fakulti Teknologi Kejuruteraan
Kelautan dan Informatik, Universiti Malaysia Terengganu (UMT) sahaja. Tidak dibenarkan
mencetak dan mengedar manual ini tanpa kebenaran rasmi daripada penulis.
Sila ikuti langkah demi langkah sebagaimana yang dinyatakan di dalam manual.
This laboratory manual is for use by the students of the Faculty of Ocean Engineering Technology
and Informatics, Universiti Malaysia Terengganu (UMT) only. It is not permissible to print and
distribute this manual without the official authorisation of the author.
Please follow step by step as described in the manual.
1
`
TASK 1: IMPLEMENTATION OF SINGLY LINKED LIST
OBJECTIVE
In this lab, we will learn the following topics:
• A linked list
• Implementation of linked list using java
• Insert Node at the beginning of the list
• Traverse List
• Delete Node from a list
TASK DESCRIPTION
In each of the topics, students should implement the tasks step by steps in order to have better
understanding of singly linked list.
ESTIMATED TIME
[60 Minutes]
DEFINITION OF LINKED LIST
A linked list is just a chain of nodes, with each subsequent node being a child of the previous
one. Many programs rely on linked lists for their storage because these don't have any evident
restrictions. There is no limit (other than the amount of memory) on the number of elements
they can store.
SINGLY LINKED LISTS
Basics:
• A singly linked list is a concrete data structure consisting of a sequence of nodes
• It has a head node pointer indicating the first node in list.
• It could have optionally a tail pointer node indication the last node in list.
• Each node stores
o Element (data)
o Link to the next node
2
`
Operations:
The common operations of Singly linked list are:
1. Insertion (or Add):
• Add first
• Add last
• Add middle (after existing node); example:
Figure 1: An example of insertion a new node in the middle of linked list
2. Deletion (or Remove):
• Delete first
• Delete last
• Delete after existing node; example:
Figure 2: An example of insertion a new node in the middle of linked list
3
`
SOME COMMON HANDLING METHODS OF SINGLY/DOUBLY LINKED LIST ARE:
1. Print (Or Show):
2. Print all list elements
3. Print certain node
4. Search for an element
5. Find list size (if no size variable in list class)
6. Reverse the linked list [step 2]
ACTIVITIES
Activity 1:
Apply and test the Linked List implementation bellow:
// the code below is a simple example of a linked list that inserts a new link at the beginning of
the list, deletes from the beginning of the list and loops through the list to print the links
contained in it.
4
`
5
`
Answer:
Click or tap here to enter text.
Activity 2: Compile the program. Execute the program and record the results.
Upload the screenshot using the control box provided below:
Answer:
6
`
TASK 2: APPLICATION OF SINGLY LINKED LIST
TASK DESCRIPTION
Based on the previous code, you may use it to complete the following tasks.
ESTIMATED TIME
[120 Minutes]
1. Write the Node class consists of two components of a node (i.e.: element, next), with a
default construct and a constructor that accepts an item assigned to the initially
declared element variable.
Answer:
public class Node {
public char element;
Node next;
public Node(){
element = ' ';
next = null;
}
public Node(char e, Node n){
element = e;
next = n;
}
public void setNext(Node n){
next = n;
}
public Node getNext(){
return next;
}
public void printNode(){
System.out.print(" {" + element + "} ");
7
`
}
}
2. Write a class called MyLinkedList. The class should have the following:
a. Default constructor
b. Nodes for head and tail
Answer:
public class MyLinkedList {
//node for head and tail
private Node head;
private Node tail;
//default constructor
public MyLinkedList(){
head = null;
tail = null;
}
}
3. Implement the following methods (some methods had already been shown in Task 1)
a. public void addFirst(Node e)
b. public void addLast(Node e)
c. public void add(int index, Node e)
d. public Node removeFirst()
e. public Node removeLast()
f. public void printList()
g. public void reverse()
Answer:
//a.
public void addFirst(char e){
head = new Node(e, head);
if (isEmpty()){
tail = head;
8
`
//b.
public void addLast(char e){
if(head==null) addFirst(e);
else{
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = new Node(e, null);
//c.
public void add(int index, char e){
Node node = new Node ();
node.element = e;
node.next = null;
if (this.head == null){
if (index != 0){
return;
}else{
this.head = node;
9
`
if (head != null && index == 0){
node.next = this.head;
this.head = node;
return;
Node current = head;
Node prev = null;
int i=0;
while (i < index-1){
prev = current;
current = current.next;
if (current == null){
break;
i++;
node.next = current;
prev.next = node;
10
`
//d.
public Node removeFirst(){
Node temp = head;
head = head.next;
return temp;
//e.
public Node removeLast(){
Node currentNode = head;
Node prev = head;
if(head.next == null){
head = null;
return currentNode;
}else{
while (currentNode.next != null){
prev = currentNode;
currentNode = currentNode.next;
prev.next = null;
return currentNode;
11
`
//f.
public void printList(){
Node currentNode = head;
System.out.print("List: ");
while(currentNode != null){
currentNode.printNode();
currentNode = currentNode.next;
System.out.println(" ");
//g.
public void reverse(){
Node current = head;
Node prev = null;
while(current != null){
Node next = current.getNext();
current.setNext(prev);
prev = current;
12
`
current = next;
this.head = prev;
4. Write a test program called TestLinkedList that creates a list from
MyLinkedList class. Using the methods in (3) , do the following:
a. Add these elements to the linked list using addFirst() method according to
the order : a, b, c, d, e
b. Print all the elements in the list.
c. Delete the last value.
d. Print current list.
e. Add ‘f’ at the second position in the linked list.
f. Print current list.
g. Add ‘g’ at the end of the list.
h. Print current list.
i. Delete the first element in the current list.
j. Print current list.
k. Reverse the list.
l. Print the list.
Answer:
MyLinkedList list = new MyLinkedList();
//a. insert elements to the linked list
list.addFirst('a');
list.addFirst('b');
list.addFirst('c');
list.addFirst('d');
list.addFirst('e');
//b. print all elements in the list
list.printList();
System.out.println("");
//c. delete last value
list.removeLast();
//d. print current list
13
`
list.printList();
System.out.println("");
//e. add 'f' at second position
list.add(2, 'f');
//f. print current list
list.printList();
System.out.println("");
//g. add 'g' at the end
list.addLast('g');
//h. print current list
list.printList();
System.out.println("");
//i. delete first element
list.removeFirst();
//j. print current list
list.printList();
System.out.println("");
//k. reverse the list
list.reverse();
//l. print current list
list.printList();
5. Compile the program. Execute the program and record the results.
Upload the screenshot using the control box provided below:
Answer:
14
`
6. Read the instruction regarding submission carefully. Submit your answer using the link
provided at Oceania UMT. Please ensure your codes are submitted to the correct
group.
15