This was an exaple used at my skool, but I do not undestand exactly what the code is doin. The program is about linked lists. each class is supposed to do something, for example makeEmpty: akes an existing list empty and returns type void. Here is the code:
public class LinkedList2
{
private ListNode head;
private ListNode tail;
public boolean isEmpty()
{
return (head == null);
}
public void makeEmpty()
{
head = null;
}//makes the whole list empty
public void add(Object x)
{
ListNode newNode = new ListNode(x);
if(head == null)
{
newNode.next = null;
head = newNode;
tail = newNode;
}
else
{
newNode.next = head;
head = newNode;
}
}//adds element to the list
public void addLast(Object x)
{
ListNode newNode = new ListNode(x);
newNode.next = null;
if(head == null)
{
head = newNode;
tail = newNode;
}
else
{
tail.next = newNode;
tail = newNode;
}
}// adds object at the last
public void addAfter(Object y, Object x)
{
ListNode newNode = new ListNode(x);
ListNode node = head;
while(node != null)
{
if(node.getData() == y)
{
newNode.next = node.next;
node.next = newNode;
}
node = node.getNext();
}
}// adds element after the specified object
public void remove(Object x)
{
ListNode currentNode = head;
ListNode previousNode = head;
while (currentNode != null)
{
if(head.getData() == x)
head = head.next;
else if (currentNode.getData() == x)
previousNode.next = currentNode.next;
previousNode = currentNode;
currentNode = currentNode.next;
}
}// removes only the first occurence of the object in the list
public void append(LinkedList list2)
{
tail.next = list2.head;
}//appends two lists.
public void removeAll(Object x)
{
ListNode currentNode = head;
while(currentNode != null)
{
remove(x);
currentNode = currentNode.next;
}
}// removes all the occurences of the object in the list
public void printList()
{
ListNode node = head;
while(node != null)
{
System.out.println(node.getData() + ", ");
node = node.getNext();
}
}// prints the list
public void printListRev()
{
ListNode currentNode, nextNode, loopNode;
currentNode = head;
nextNode = head.next;
loopNode = null;
while(nextNode != null)
{
currentNode.next = loopNode;
loopNode = currentNode;
currentNode = nextNode;
nextNode = nextNode.next;
}
head = currentNode;
head.next = loopNode;
ListNode node = head;
while(node != null)
{
System.out.println(node.getData() + ", ");
node = node.getNext();
}
}
public static void main(String [] args)
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list.add(12);
list.add(14);
list.add(14);
list.add(13);
list.add(15);
list2.add(32);
list2.add(45);
list2.add(56);
System.out.println("The list is empty : " + list.isEmpty());
System.out.println("Original List :");
list.printList();
System.out.println("List after calling addLast():");
list.addLast(16);
list.printList();
System.out.println("List after calling addAfter():");
list.addAfter(13,9);
list.printList();
System.out.println("List after calling remove():");
list.remove(15);
list.printList();
System.out.println("List after calling removeAll():");
list.removeAll(14);
//list.append(list2);
list.printList();
list.append(list2);
System.out.println("The List after calling append() : ");
list.printList();
System.out.println("List after printListRev() : ");
list.printListRev();
list.makeEmpty();
list.printList();
}// main function tests data
}// class LinkedList
public class ListNode
{
public Object element;
public ListNode next;
public ListNode(Object theElement)
{
element = theElement;
}//ListNode constructor
public ListNode getNext()
{
return next;
}//getNext()
public Object getData()
{
return element;
}//getData()
}// class ListNode
I appreciate all your help, Thank you...
Edited by WingedPanther, 29 April 2009 - 07:43 AM.
add code tags (the # button)


Sign In
Create Account

Back to top










