0% found this document useful (0 votes)
99 views

University of Kwazulu Natal: School of Mathematics, Statistics & Computer Science

This document provides instructions for a practical assignment on implementing various data structures in Java, including lists, linked lists, and doubly linked lists. Students are asked to: 1) Add set operations like addAll, removeAll, and retainAll to the MyList interface and implement them in MyAbstractList. 2) Implement methods for contains, get, indexOf, lastIndexOf, set, and ensureUniqueElements in MyLinkedList. 3) Define a DoublyLinkedList node class and implement a MyTwoWayLinkedList class with listIterator and additional list methods. 4) Extend MyLinkedList to implement a circular linked list called MyCircularLinkedList with addFirst, add
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

University of Kwazulu Natal: School of Mathematics, Statistics & Computer Science

This document provides instructions for a practical assignment on implementing various data structures in Java, including lists, linked lists, and doubly linked lists. Students are asked to: 1) Add set operations like addAll, removeAll, and retainAll to the MyList interface and implement them in MyAbstractList. 2) Implement methods for contains, get, indexOf, lastIndexOf, set, and ensureUniqueElements in MyLinkedList. 3) Define a DoublyLinkedList node class and implement a MyTwoWayLinkedList class with listIterator and additional list methods. 4) Extend MyLinkedList to implement a circular linked list called MyCircularLinkedList with addFirst, add
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SCHOOL OF MATHEMATICS, STATISTICS &

COMPUTER SCIENCE

UNIVERSITY OF KWAZULU NATAL

WESTIVILLE CAMPUS

Practical III

COURSE CODE: COMP201 Data Structures

Examiner: Mr E. Jembere
Duration: Open

Instructions

1. Go over this Practical paper in preparation for the Tutorial/Practical session on Tuesday,
22 August 2017.
2. Use the resource files in the file named Prac_III_Resources on the course page on Moodle.
The resource files include the following files; MyList.java, MyAbstractList.java,
MyArrayList.java and MyLinkedList.java.
3. For all your implementations involving a singly linked list, define your own Node
class(name SLLNode) with the following two constructors:
a. a no-argument constructor,
b. a two-argument constructor that takes a value and a reference to the next node
as arguments.

1. **(Add set operations in MyList) Define the following methods in MyList and implement them
in MyAbstractList:
/** Adds the elements in otherList to this list.
* Returns true if this list changed as a result of the call */
public boolean addAll(MyList<E> otherList);

/** Removes all the elements in otherList from this list


* Returns true if this list changed as a result of the call */
public boolean removeAll(MyList<E> otherList);

1|Page
/** Retains the elements in this list that are also in otherList
* Returns true if this list changed as a result of the call */
public boolean retainAll(MyList<E> otherList);

Write a test program that creates two MyArrayLists, list1 and list2, with the initial values
{"Tom", "George", "Peter", "Jean", "Jane"} and {"Tom", "George", "Michael", "Michelle",
"Daniel"}, then perform the following operations:

a. Invokes list1.addAll(list2), and displays list1 and list2.


b. Recreates list1 and list2 with the same initial values, invokes list1.removeAll(list2),
and displays list1 and list2.
c. Recreates list1 and list2 with the same initial values, invokes list1.retainAll(list2),
and displays list1 and list2.

2. *(Implement MyLinkedList) The implementations of the methods contains(E e), get(int


index), indexOf(E e), lastIndexOf(E e), and set(int index, E e) for MyLinkedList are omitted
in the prescribed text.
a. Implement the above mentioned methods.
b. Also implement a method that ensures that the list has no duplicate elements using
the following method signature,
/** removes all duplicate elements from this list. */
public void ensureUniqueElements()

3. **(Implement a doubly linked list) Define a class for a Doubly-Linked list node with the
following properties:

i. Three (3) class attributes value, next and previous to hold the value of the node, a
reference to the next node and a reference to the previous node respectively,
ii. a no-argument constructor, and
iii. a three-argument constructor DLLNode(E e, DLLNode nxt, DLLNode prev) ,
where nxt is a reference to the next node and prev is reference to the previous node.

a. Implement a new class named MyTwoWayLinkedList that uses doubly-linked list


nodes to store elements in a list. Define MyTwoWayLinkedList to extend the
java.util.AbstractSequentialList class. You need to implement the methods listIterator()
and listIterator(int index). Both return an instance of java.util.ListIterator<E>. The former
sets the cursor to the head of the list and the latter to the element at the specified index.

Your doubly-linked list is also expected to implement the methods; add(), addFirst(E e),
addLast(E e), addLast(E e), add( int index, E e), removeFirst(), removeLast(), remove(int
index)

b. Write a test program to test the methods you implemented in Question (3.a)

2|Page
4. *Extend the implementation of MyLinkedList to implement a Circular-linked list. Name the
class MyCircularlinkedList. Your implementation should include the implementation of the
following methods
a. addFirst(E e),
b. addLast(E e),
c. removeFirst(),
d. removeLast().

----------------------------------------------------------------------------------------------------------------

WISHING YOU ALL THE BEST

3|Page

You might also like