How Linkedlist can be implemeented in java with out using collection framework?

Showing Answers 1 - 5 of 5 Answers

Sumana

  • Feb 15th, 2006
 

Java(tm) has no user-visible pointers, so how would you do a linked list?
Each element needs to point to the next one, doesn't it?

Well, fortunately there is a way, illustrated using this brief example:

        // source file link.java        public class link {                public int value;              // value of element                public link next;              // reference to next                        // constructor                public link(int n, link ln)                {                        value = n;                        next = ln;                }                public static void main(String args[])                {                        // initialize list head                        link head = null;                        // add some entries to list                        for (int i = 1; i <= 10; i++)                                head = new link(i, head);                        // dump out entries                        link p = head;                        while (p != null) {                                System.out.println(p.value);                                p = p.next;                        }                }        }

Java does not have pointers, but instead uses implicit references. A line like:

        link next;

does not actually declare an object instance of class link, but rather a reference to an object instance. The line:

        head = new link(i, head);

creates a new element for the list, sets its value, and then sets the object reference in "next" to point at the old list head (this approach means that the last element inserted will be at the head of the list). Saying:

        p = p.next;

simply picks up the "next" reference from the current node and makes it the current element being worked on.

When we're done using this list, we can simply leave the list with all its elements lying around -- garbage collection will eventually reclaim it.

If what you really want to do in a Java application is manage a list of numbers, there are higher-level techniques in the language and library (such as the Vector class) for doing so more cleanly than the approach illustrated here. This example shows some of what can be done underneath to implement the higher-level schemes.

  Was this answer useful?  Yes

// Linked List implementation without using Collection framework

public class Node {
 int data;
 Node next;

 public Node(int data, Node next)
 {
  this.data = data;
  this.next = next;
 }

 public static void main(String[] args)
 {
  Node first = new Node( 1, null);
  Node second = new Node( 2, null);
  Node third = new Node( 3, null);
  Node fourth = new Node( 4, null);
  Node fifth = new Node( 5, null);

  first.next = second; second.next = third;
  third.next=fourth; fourth.next = fifth;

  System.out.print(" Linked List : ");
  for(Node ptr = first; ptr!=null; ptr=ptr.next)
  {
    System.out.print("  -->  " + ptr.data);
  }
  System.out.println("\n\n --- End of Linked List ---");
 }
}

// Written by Seetha Ram Janapala -- sitaram81581@rediffmail.com

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions