Jump to content

plz help me in understanding this code

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
sweeeeeeety

sweeeeeeety

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,,

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)


#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
First two notes:
  • Formatting is everything
  • Spell things correctly you sound like a 10 year old when you dont
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 

Watch these videos to understand nodes

the head and tail pointers point out its a double linked list - (deque I believe) which means you can add items to each side of the list and pull from both sides of the list

I believe the example my teacher gave me was lets say you have a stack of plates - basically with a deque you can add things to either side (the bottom or the top) and take a plate from either side.

Starting at the top:
  • you have two private "ListNodes" head and tail
  • you have a method to tell if the list is empty or not
  • then you have a method to actually make the list empty
  • you have a method to add to the list
    • you do a question to see if its the first add or not

  • the next method is add last to add to the other side of the list
    • does the same thing

  • addafter basically makes it so you can put a plate anywhere in the deque instead of the top of bottom of the stack
  • remove helps you remove it
  • append just appends two lists lol
  • removeall takes all the nodes off the list (start new)
  • printlist just shows you all the nodes you have
  • printlistrev just does the same thing but backwards
  • the long and ugly things now just adds things prints things and deletes things (shows you how it works)
  • then you actually have the listnode class which is usually put at the top


#3
sweeeeeeety

sweeeeeeety

    Newbie

  • Members
  • Pip
  • 2 posts
thank u soo much, u made it much more clearer..