0

I've created a sorted linked list class, and the only part I'm struggling with is implementing the toArray() method properly.

    public class SortedLinkedList<T extends Comparable<T>> implements ListInterface<T>
{

  // container class

  private class LinkedNode<T>
  {
    public T data;
    public LinkedNode<T> next;

    public LinkedNode(T data, LinkedNode<T> next)
    {
      this.data = data;
      this.next = next;
    }
  }

  // private variables

  private LinkedNode<T> head, tail;
  private int size;
  private String name;

  // constructor

  public SortedLinkedList(String name)
  {
    head = null;
    tail = null;
    size = 0;
    this.name = name;
  }

  // core functions

  public void Add(T data)
  {
    size++;

    // creation of new node to be added

    LinkedNode<T> newNode = new LinkedNode<T>(data, null);

    // check for empty list; adds node at head if so

    if (head == null)
    {
      head = newNode;
      return;
    } 

    if (head.data.compareTo(data) < 0)
    {
      head = newNode;
      return;
    }

    // insertion in middle

    LinkedNode<T> current = head;
    LinkedNode<T> prev = null;

    while (current != null)
    {
      if (current.data.compareTo(data) > 0)
      {
        prev.next = newNode;
        newNode.next = current;
        return;
      }
      prev = current;
      current = current.next;
      }

    // insertion at end

    prev.next = newNode;

    return;
  }

  public void Remove(T data)
  {
    if (head == null)
    {
      return;
    }

    LinkedNode<T> current = head.next;

    while (current != null && current.next != null)
    {
      if (current.data.compareTo(current.next.data) == 0)
      {
        current.next = current.next.next;
      } else {
      current = current.next;
      }
    }
    size--;
  }

  public int size()
  {
    return size;
  }

  @SuppressWarnings("unchecked")
  public T[] toArray()
  {
    T[] result = (T[])(new Comparable[size()]);

    int counter = 0;

    for ( T item : )
    {
      result[counter++] = item;
    }
    return result;
  }

The problem I'm having is what I should include after "T item : " in my for/each line. I had no problem with implementing a similar toArray() method in a set class recently, as that was "for each item in the set", but for some reason I'm blanking on what to place there for the linked list.

suicuned
  • 37
  • 5

3 Answers3

0

Don't use for loop with linked-lists. Try something like below:

LinkedNode cursor = head;
int index = 0;
while (cursor != null ){
  result[index] = cursor.data;
  cursor = cursor.next;
  index++;
}
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52
mstfyldz
  • 482
  • 1
  • 6
  • 12
0

Try this.

  @SuppressWarnings("unchecked")
  public T[] toArray()
  {
    T[] result = (T[])(new Comparable[size()]);

    int counter = 0;

    for ( LinkedNode<T> cursor = head; cursor != null; cursor = cursor.next )
    {
      result[counter++] = cursor.data;
    }
    return result;
  }
lauthu
  • 306
  • 3
  • 11
0

Actually, the answer to your question is this:

@SuppressWarnings("unchecked")
public T[] toArray() {
    T[] result = (T[])(new Comparable[size()]);

    int counter = 0;

    for (T item : this) {
        result[counter++] = item;
    }
    return result;
}

This is correct, but to do this you have to implement Iterable<T> interface:

public class SortedLinkedList<T extends Comparable<T>> implements ListInterface<T>, Iterable<T> {
    @Override
    public Iterator<T> iterator() {
        return null;
    }

    // ...
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35