0

Possible Duplicate:
How do I implement a Linked List in Java?

We know there is no pointers in java. Then what is the best way to build the link list in java?

Community
  • 1
  • 1
mumair
  • 2,768
  • 30
  • 39
  • already answered [here](http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java) – Grijesh Chauhan Dec 18 '12 at 07:03
  • 2
    Using [java.util.LinkedList](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html) ? – Charlie Dec 18 '12 at 07:03
  • 3
    In java all complex variables are [reference types](http://en.wikipedia.org/wiki/Reference_type), which means they behave similar to pointers – Karthik T Dec 18 '12 at 07:04
  • http://www.roseindia.net/java/jdk6/linkedlistexample.shtml – Grijesh Chauhan Dec 18 '12 at 07:04
  • http://javadude.com/articles/passbyvalue.htm *In short: Java has pointers and is strictly pass-by-value.* – The111 Dec 18 '12 at 07:06
  • 1
    Not quite true, on the everything in Java is pointers except for primitive datatypes like int. – AndersG Dec 18 '12 at 07:09
  • Java _has_ a LinkedList class, you don't need to build one. While you're at it, you may as well re-implement _all_ of Java's collection classes. That seems a bizarre use of one's time. – paxdiablo Feb 13 '13 at 06:07

3 Answers3

5

The best way is to not build it. Java already has a LinkedList class amongst its rather large selection of collection classes.

You would be better off using what the language/library already provides.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

You have an object that essentially contains two variables, no methods (bare minimum; however, you could have methods if you wanted). Something like:

class Link
{
   int data;
   Link next;
}

Then you create a new Link like any other object. Set the data to the data you want a node to hold. Then set the Link node to the node that it will be "pointing" to (or null if it doesn't point to another one).

Note: you can also have a previous node (which points to the previous node) if need be.

adchilds
  • 963
  • 2
  • 9
  • 22
1

try having this code.

public class Main {
public static void main(String[] args) {
LinkedList theList = new LinkedList();
LinkedListIterator theItr;

theItr = theList.zeroth();
printList(theList);

for (int i = 0; i < 10; i++) {
  theList.insert(new Integer(i), theItr);
  printList(theList);
  theItr.advance();
}
System.out.println("Size was: " + listSize(theList));

}

public static int listSize(LinkedList theList) {
LinkedListIterator itr;
int size = 0;
for (itr = theList.first(); itr.isValid(); itr.advance())
  size++;
return size;
  }

 public static void printList(LinkedList theList) {
if (theList.isEmpty())
   System.out.print("Empty list");
 else {
   LinkedListIterator itr = theList.first();
   for (; itr.isValid(); itr.advance())
    System.out.print(itr.retrieve() + " ");
  }
  System.out.println();
}
}

class LinkedList {
 public LinkedList() {
header = new ListNode(null);
 }

public boolean isEmpty() {
return header.next == null;
}

public void makeEmpty() {
header.next = null;
}

 public LinkedListIterator zeroth() {
  return new LinkedListIterator(header);
 }

 public LinkedListIterator first() {
 return new LinkedListIterator(header.next);
 }

  public void insert(Object x, LinkedListIterator p) {
  if (p != null && p.current != null)
  p.current.next = new ListNode(x, p.current.next);
}

 public LinkedListIterator find(Object x) {
ListNode itr = header.next;

while (itr != null && !itr.element.equals(x))
  itr = itr.next;

return new LinkedListIterator(itr);
 }

  public LinkedListIterator findPrevious(Object x) {
  ListNode itr = header;

 while (itr.next != null && !itr.next.element.equals(x))
  itr = itr.next;

return new LinkedListIterator(itr);
  }

public void remove(Object x) {
LinkedListIterator p = findPrevious(x);

if (p.current.next != null)
  p.current.next = p.current.next.next; // Bypass deleted node
}

 private ListNode header;

}

class LinkedListIterator {
LinkedListIterator(ListNode theNode) {
current = theNode;
  }

  public boolean isValid() {
 return current != null;
  }

  public Object retrieve() {
return isValid() ? current.element : null;
  }

public void advance() {
  if (isValid())
  current = current.next;
  }

  ListNode current;
}

class ListNode {
  public ListNode(Object theElement) {
this(theElement, null);
  }

  public ListNode(Object theElement, ListNode n) {
   element = theElement;
    next = n;
  }

  public Object element;

  public ListNode next;
}
ggcodes
  • 2,859
  • 6
  • 21
  • 34