If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

Java Program Help please

HI. I need to know what the code would be to print the Nodes. I simply can't figure it out.I'm not allowed to change anything or add anything. I just have to fill in the methods and make the ListSort work as it is. I can't figure out how to print each node so the out put looks like this:
-----------
12 2 10 5
----------
12 2 10 5

Eventually I have to have to program sort them but I can probably figure it out if I can just print them. ANy help? i can't find out the code to write in the printLIst and insertNodeAtBeginning that will get the nodes to print.

Code:

*
* implements various methods related to manipulating a singly-linked list
*/
public class LinkedList
{
private Node firstNode = null;
/*
* prints all the elements of the list in order on one line, separated by s
paces
* puts a line of dashes above and below the printed list
*/
public void printList()
{
System.out.println("----------");
Node currentNode = firstNode;
}
System.out.println("----------");
}
/*
* inserts a new node into the list at the beginning of the list
*/
public void insertNodeAtBeginning(Node newNode)
{
// quick error checking
if (newNode == null)
return;
// TODO: insert newNode into the beginning of the list
}
/*
* inserts the newNode into the list just before the first node that
* has a larger value than newNode; if the list is empty or if newNode
* has the smallest value, then newNode will be inserted at the beginning
* of the list; if newNode has the largest value in the list, then it
* should be inserted at the end of the list
*/
public void insertNodeInAscendingOrder(Node newNode)
{
// quick error checking
if (newNode == null)
return;
// TODO: insert newNode in the right place in the list
}
/*
* remove the first node in the list and return it. if there are no
* elements in the list, then return null
*/
public Node removeFirstNode()
{
return firstNode;// TODO: remove the first element in the list and return
it, or
// return null if the list is empty
}
/*
* use recursion to sort the given list in ascending order
*/
public static void recursiveListSort(LinkedList list)
{
// quick error checking
if (list == null)
return;
// TODO: use recursion to sort the list
}
}
Next file:
public class ListSort {
/**
* @param args
*/
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insertNodeAtBeginning(new Node(5));
list.insertNodeAtBeginning(new Node(10));
list.insertNodeAtBeginning(new Node(2));
list.insertNodeAtBeginning(new Node(12));
list.printList();
LinkedList.recursiveListSort(list);
list.printList();
}
}
Next File:
public class Node
{
private int value;
public Node next = null;
public Node(int newValue)
{
value = newValue;
}
public int getValue()
{
return value;
}
}