I need to insert a new int into a list of its. BUT - It needs to be after a number smaller than it, and before a number larger than it, but the list is not to be sorted. It's driving me bonkers. I have 3 classes - all work right except for these assignments in my List class:
if (current.getNex t() < temp && current > temp)
while (current.getNex t() == index)
I am very good at rookie errors - please help if you can...
if (current.getNex t() < temp && current > temp)
while (current.getNex t() == index)
I am very good at rookie errors - please help if you can...
Code:
public class List extends ListNode
{
public ListNode head;
public int listCount;
//constructor
public List()
{
head = null;
listCount=0;
}
//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);
}
}
//to insert item at a certain point
public void insertItem(int item, int index)
{
ListNode temp = new ListNode(item);
ListNode current = head;
while (current.getNext()!= null )
for(int i = 1; i < index && current.getNext() != null; i++)
{
if (current.getNext() < temp && current > temp)
{
current = current.getNext();
}
}
temp.setNext(current.getNext());
current.setNext(temp);
listCount++;
}
// to delete a specific item
public boolean deleteItem(int index)
{
ListNode current = head;
if (head == null)
{
return false;
}
while (current.getNext() == index)
{
if(current.getNext() == null)
return false;
current = current.getNext();
}
current.setNext(current.getNext());
return true;
}
// to print the list
public void printItem()
{
ListNode nextNode = head;
while (nextNode!= null)
{
System.out.print(nextNode.getNext());
}
}
public ListNode getHead()
{
return head;
}
}
Code:
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;
}
}
Code:
public class LinkedListDriver extends List
{
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();
}
}
Comment