Circular Singly-Linked List
You are to code a circular singly-linked list with a head reference. A linked list is a collection of nodes, each having a data item and a reference pointing to the next node. Since it must be circular, the next reference for the last node in this list will point to the head node. As a special case, this means that in a one node list, the head node will point to itself.

Do not use a phantom node to represent the start or end of your list. A phantom or sentinel node is a node that does not store data held by the list and is used solely to indicate the start or end of a linked list. If your list contains n elements, then it should contain exactly n nodes.

It will use the default constructor (the one with no parameter) which is automatically provided by Java. Since instance variables are automatically assigned default values, it is not necessary to explicitly set them, so do not write your own constructor.

As an additional note, your circular implementation doesn't have a tail reference, but it is still possible to efficiently add and remove from the head as well as add to the back in 2(1) time. However, it is still not possible to remove from the back in O(1) time unless the linked list is doubly-linked.

The linked list consists of nodes. A class LinkedListNode is provided to you. LinkedListNode has setter and getter methods to access and mutate the structure of the nodes.

You will implement three add() methods. One will add to the front, one will add to the back, and one will add to anywhere in the list. See the javadocs for more details.

Removing, just like adding, can be done from the front, the back, or anywhere in your linked list. In addition, you will also be coding a method to remove the last instance of a piece of data. When removing from the front, the first node should be removed in such a way that the last node points to the new front of the list (mind the efficiency!). When removing from the back, the last node should be removed and have the new last node point to the head. When removing from the middle, the node before the removed node should point to the next node of the removed node. See the javadocs for more details.

Garbage Collection
Java will automatically mark objects for Garbage Collection based on whether there is any means of accessing the object. In other words, if we want to remove a node from the list, we must remove all references to that node. What the next reference of that node points to doesn't particularly matter since the node will be Garbage Collected eventually.

Differences between Java API and This Assignment
Some of the methods in this assignment are called different things or don't exist in Java's LinkedList class. Additionally, Java's built in LinkedList is a Doubly-Linked List, so the efficiency of some operations will differ. This won't matter until you tackle coding questions on the first exam, but it's something to be aware of. The list below shows all methods with a different name and their Java API equivalent if it exists. The format is assignment method name Java API name.

addAtIndex (int index, I data) add (int index, T data)
addToFront (T data)
addFirst (T data)
addToBack (T data)
add (T data) or addLast (T data)
removeAtIndex (int index) remove (int index)
removeFromFront () poll() or pollFirst ()
T removeLastOccurrence (T data) boolean removeLastOccurrence (Object data)

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

* Your implementation of a circular singly linked list.
* @author YOUR NAME HERE
* @version 1.0
public class SinglyLinkedList<T> {
    // Do not add new instance variables or modify existing ones.
    private LinkedListNode<T> head;
    private int size;

    * Adds the element to the index specified.
    * Adding to indices 0 and {@code size} should be O(1), all other cases are
    * O(n).
    * @param index the requested index for the new element
    * @param data the data for the new element
    * @throws IndexOutOfBoundsException if index is negative or
    * index > size
    * @throws IllegalArgumentException if data is null
    public void addAtIndex(int index, T data) {
       if(index < 0 || index > size){
            throw new IndexOutOfBoundsException("Cannot insert out of bound index into data structure.");

       if(data == null){
            throw new IllegalArgumentException("Cannot insert null data into data structure.");

       if(index == 0){
       }else {
            int i = 0;
            LinkedListNode<T> tmp = head;
            LinkedListNode<T> prev = null;
            while (i<index){
                prev = tmp;
                tmp = tmp.getNext();
            LinkedListNode<T> node = new LinkedListNode<>(data);...

By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats