Ok, so I need some guidance. Am trying to implement a linked list in java. I can't get it. I have been on here - dreaminncode, wikipedia, and some other colleges sites - I still don't/can't get it. I did look at the previous threads on this but they didn't quite cover it for me. Any advice would be greatly appreciated.
Here's the assignment:
Design your own List class to hold a series of integers in ListNode classes. The list class must implement the following methods:
· No Argument Constructor – this will initialize the list as having no elements
· Copy Constructor – this will copy the elements from another List
· appendItem – create a new node and add it to the end
· insertItem – insert a new node into a list such that it is after a smaller integer and before a larger one
· deleteItem – delete all instances of the item from the list
· print – print the list in order to the console
· sort (extra credit) – sort the items in the list and put them in ascending order
Here's all of my code:
public class List
{
private ListNode head;
//constructor
public List()
{
head = null;
}
//to append the list
public void appendItem(int item)
{
ListNode newNode = new ListNode(item);
if (head == null)
{
head = newNode;
}
else
{
// find last item in list
ListNode nextNode = head;
while (nextNode.getNext() != null)
{
nextNode = nextNode.getNext();
}
nextNode.setNext(newNode);
}
}
public void insertItem(int item)
{
ListNode temp = new ListNode(item);
ListNode current = head;
for(int i = 1; i < i && current.getNext() != null; i++)
{
current = current.getNext();
}
temp.setNext(current.getNext());
current.setNext(temp);
}
public boolean deleteItem(int index)
{
ListNode current = head;
for(int i = 1; i < index; i++)
{
if(current.getNext() == null)
return false;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
return true;
}
public void printItem()
{
ListNode nextNode = head;
while (nextNode.getNext() != null)
{
System.out.print(nextNode.getNext());
}
}
public ListNode getHead()
{
return head;
}
public void setHead(ListNode listNode)
{
head = listNode;
}
}
public class ListNode
{
private int newItem;
private ListNode nextNode;
public ListNode()
{
newItem = 0;
nextNode = null;
}
public ListNode(int item)
{
newItem = item;
nextNode = null;
}
public void setNext(ListNode next)
{
nextNode = next;
}
public ListNode getNext()
{
return nextNode;
}
public void setItem(int item)
{
newItem = item;
}
public int getItem()
{
return newItem;
}
}
public class LinkedListDriver
{
public static void main(String[] args)
{
List list = new List();
list.appendItem(1);
list.appendItem(2);
list.appendItem(3);
list.appendItem(42);
list.appendItem(5);
list.appendItem(2);
list.insertItem(0);
list.insertItem(5);
list.insertItem(4);
list.insertItem(192);
list.appendItem(9);
list.insertItem(3);
list.appendItem(3);
System.out.println("Print after insert and appends");
list.printItem();
//list.deleteItem(9);
//list.deleteItem(1);
//list.deleteItem(3);
System.out.println("Print after deletes");
list.printItem();
list.appendItem(44);
list.appendItem(22);
list.appendItem(1);
System.out.println("Print prior to sort");
list.printItem();
//list.sort();
System.out.println("Print after sort");
list.printItem();
}
}
The error I am getting in Eclipse keeps crashing the school's computer - here's a very small snipet:
ListNode@19821fListNode@19821fListNode@19821fListNode@19821fListNode@19821fListNode@19821fListNode@19821f