4 Replies - 1315 Views - Last Post: 07 December 2009 - 06:30 PM Rate Topic: -----

#1 tscott  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 370
  • Joined: 30-January 09

Understanding Nodes

Post icon  Posted 07 December 2009 - 05:11 PM

Anyone have any good practice problems or sites that break down nodes well? I'm in my 2nd java class I tried googling for examples and got nothing but advanced shit anyhow I need to know how to transverse and remove them as well as having a general understanding of how they work.

I mean I know that if you wanted to add a node after another u'd need to know the address of the node and that there is basically 2 parts to a know the value and the .next part but thats about all I understand right now and the book is terrible

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding Nodes

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Understanding Nodes

Posted 07 December 2009 - 05:51 PM

A Node is fairly simple- it holds an element and it points to the next Node, which is useful when traversing the LinkedList. Think about it like this- there are a group of people in line. Each person points to the next person in line. If there isn't another person in line, then the end person points to nobody. Like so:
A --> B --> C --> D -->



So to traverse, start at the head of the line, person A. Does A point to another person? Yes, go to that person, labeled B. Does B point to another person? Yes, go to C. And so on.

When working with adding or removing elements from the middle of the list, it is just a matter of re-arranging the pointers. For example, if you want to remove person B from the List, you would want A to point to C. So in terms of Nodes, you would modify A.next = A.next.next. Does this make sense?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding Nodes

Posted 07 December 2009 - 05:52 PM

Nodes always have a forward link which is a pointer to the next node
this forward link might be null if you are a the last node in the list

so to add a node
you start by the first node and go up to the next node using the forward link and to the next one.... and so on until the forward link is null
when to find that last node with the null forward link you set this forward link to point to the node you want to add
and you set to null the forward link of the node you add

to remove node N
you set the forward link of node N-1 to the forward link of node N (which might be null if the node N was the last node in the list

hope it is clearer
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Understanding Nodes

Posted 07 December 2009 - 05:55 PM

Linked Lists/Nodes
Was This Post Helpful? 0
  • +
  • -

#5 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Understanding Nodes

Posted 07 December 2009 - 06:30 PM

Everything in Java(except primitives) are actually pointers under the hood.

Pointers do exactly what it sounds like they should do, they 'point' to some data.
As long as you understand the concept of pointers, Nodes are a simple concept.

A node contains a pointer and an object(or multiple objects), the pointer points to another Node.(or if the last Node probably NULL)
So imagine you start at the beginning and want to get to a specific Node. You'd first go to where the pointer in the current Node
points(another node), look at the Ojbect in that Node or move to where the pointer of that Node points.

Here's a singly linked Node like structure. Since all Objects in Java are pointers, the field, pointer, of the Node
class is actually a pointer to another Node.
public class Node{
	 private Node pointer;
	 private Object data;
	 public Node(Node nextNode, Object data) {
		  pointer = nextNode;
		  this.data = data;
	 }

	 public Node getNextNode() {
		  return pointer;
	 }
	 public Object getData() {
		  return data;
	 }
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1