What line of code do I change to avoid duplication in a linked list?

54 views Asked by At

I'm trying to add items to a linked list, however, if a node's data repeats instead of returning the new node it should return the original node with the same data. I have a test file along with the methods I'm testing. the trouble comes from my addOnce() method returning the node parsed through instead of the first occurrence. trying to avoid

"Error: The initial item was not returned for the " + result" line of code

here's the test block of code, no editing is required

import java.util.Iterator;
public class OrderedListTest {
    static private class Courses implements Comparable<Courses>{
        String rubric;
        int number;
        int occurance;
        public Courses(String rub, int num, int occ) {
            rubric = rub;
            number = num;
            occurance = occ;
        }
        public int compareTo(Courses other) {
            if (rubric.compareTo(other.rubric) < 0)
            return -1;
            else if (rubric.compareTo(other.rubric) > 0)
            return 1;
            else
            return number - other.number;
        }
        public String toString() {
            return rubric + " " + number;
        }
    }
    public static void main(String[] args) {
        Courses listOfCourses[] = {
            new Courses("COSC", 2436, 1),
            new Courses("ITSE", 2409, 1),
            new Courses("COSC", 1436, 1),
            new Courses("ITSY", 1300, 1),
            new Courses("ITSY", 1300, 2),
            new Courses("COSC", 1436, 2),
            new Courses("COSC", 2436, 2),
            new Courses("ITSE", 2417, 1),
            new Courses("ITNW", 2309, 1),
            new Courses("CPMT", 1403, 1),
            new Courses("CPMT", 1403, 2)};
        OrderedAddOnce<Courses> orderedList = new OrderedAddOnce<Courses>();
        Courses result;
        for (int i = 0; i < listOfCourses.length; i++){
            result = orderedList.addOnce(listOfCourses[i]);
            if (result == null)
                System.out.println("Error: findOrAdd returned null for " +
                listOfCourses[i]);
            else {
                if (result.occurance != 1)
                    System.out.println("Error: The initial item was not returned for " + result);
                if (result.compareTo(listOfCourses[i]) != 0)
                    System.out.println("Error: " + listOfCourses[i] + " was passed to findOrAdd but " + result + " was returned");
            }
        }
        Iterator<Courses> classIter = orderedList.iterator();
        while(classIter.hasNext()) {
        System.out.println(classIter.next());
        }
    // There should be 7 courses listed in order
    }
}

Here's the code in need of debugging, specifically in the addOnce() method

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
*
* @author User
*/

// Interface for COSC 2436 Labs 3 and 4
/**
* @param <E> The class of the items in the ordered list
*/
interface AddOnce <E extends Comparable<? super E>> {
/**
* This method searches the list for a previously added
* object, if an object is found that is equal (according to the
* compareTo method) to the current object, the object that was
* already in the list is returned, if not the new object is
* added, in order, to the list.
*
* @param an object to search for and add if not already present
*
* @return either item or an equal object that is already on the
* list
*/
public E addOnce(E item);
}

//generic linked list
public class OrderedAddOnce<E extends Comparable<? super E>> implements Iterable<E>, AddOnce<E> {
    private Node<E> firstNode;
    public OrderedAddOnce() {
        this.firstNode = null;
    }
    @Override
    public E addOnce(E item) {
    Node<E> current;
    if (firstNode == null || item.compareTo(firstNode.data) <= 0) {
        Node<E> newNode = new Node<>(item);
        newNode.next = firstNode;
        firstNode = newNode;
        return firstNode.data;
    }
    current = firstNode;
    while (current.next != null && item.compareTo(current.next.data) > 0) {
        current = current.next;
    }
    Node<E> newNode = new Node<>(item);
    newNode.next = current.next;
    current.next = newNode;

    return newNode.data;
    }
    @Override
    public Iterator iterator() {
        return new AddOnceIterator();
    }
    private class AddOnceIterator implements Iterator{
        private Node<E> currentNode = firstNode;

        @Override
        public boolean hasNext() {
            return currentNode != null;
        }

        @Override
        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            E data = currentNode.data;
            currentNode = currentNode.next;
            return data;
        }
    }
    private class Node<E> {
        public E data;
        public Node<E> next;

        public Node(E intialData){
            this.data = intialData;
            this.next = null;
        }

    }
}

I've tried swapping the return statement values from the newNode.data, current.data, item. but it returns the same error messages regardless

Error: The initial item was not returned for ITSY 1300
Error: The initial item was not returned for COSC 1436
Error: The initial item was not returned for COSC 2436
Error: The initial item was not returned for CPMT 1403
COSC 1436
COSC 1436
COSC 2436
COSC 2436
CPMT 1403
CPMT 1403
ITNW 2309
ITSE 2409
ITSE 2417
ITSY 1300
ITSY 1300

I'm expecting the list without duplicates

0

There are 0 answers