WIA1002/WIB1002 Data Structures Lab 5: Linked List


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (5 votes)

Question 1
1) Write the generic Node class consisting 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.
2) Write a class called MyLinkedList. The class should have the following :
a. Default constructor
b. Nodes for head and tail
3) Implement the following methods from tutorial in this class:
a. public void addFirst(E e)
b. public void addLast(E e)
c. public void add(int index, E e)
d. public E removeFirst()
e. public E removeLast()
f. public E remove(int index)
4) Expand the MyLinkedList by implementing the following methods:
Methods Description
public void
add(E e)
Return nothing, but adds an element to the list
public boolean
contains(E e)
Return true if list contains the element e
public E
get(int index)
Return element at the specified index
public E
Return the value of the first item
public E
Return the value of the last item
public int
indexOf(E e)
Return the index of the head matching element in this list. Return -1
of no match
public int
Return the index of the last matching element in this list. Return -1
of no match
public E
set(int index,
Replace the element at the specified position in this list with the
specified element
Page 1
WIA1002/WIB1002 Lab 5 Sem. 2,
E e)
public void
Clear the list
public void
Print all the elements in the list
public void
Print all elements in reverse order
5) Write a test program called TestLinkedList that creates a list from MyLinkedList class. Using the
methods in (3) and (4), do the following:
a. Append the following : a, b, c, d, e
b. Print all the elements in the list.
c. Reverse all the elements in the list.
d. Retrieve the number of elements in the list.
e. Retrieve the first and last value.
f. Delete the middle value.
g. Retrieve the index location for the second and third value.
h. Checks if the list has the value ‘c’.
i. Replace the items individually with the following: h,e,l,l,o.
Question 2
A method called getMiddleValue() returns the value of the middle element of a linked list. The method
signature is given as follows :
public E getMiddleValue()
Write the codes for the getMiddleValue().
Page 2