How to implement pointer concept in java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How to implement pointer concept in java

  1. #1
    Join Date
    Aug 2007
    Posts
    4

    How to implement pointer concept in java

    How to write a program for a linkedlist without using the collection framework

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    All datatypes (other than the simple/base/elemental types int, short, float, char, boolean, etc.) are "reference" types. When you use the instance's name, you are passing a reference to the memory location where the object is being stored. So, when you build your linked list, you will not be copying objects, but storing references to the next Node and the element being stored in the Node of interest. The location's name acts, almost, like a pointer.

    So ... what's holding you up? Just write a Node class which has the links you want (prior, next, parent, child, whatever) as some of the fields and the methods you need to manipulate your element.

  3. #3
    Join Date
    Aug 2007
    Posts
    4
    Can u please send me the program regarding this. I need it.. i tried to write one but failed..

    Thank U

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    why don't you post your code for the part where your attempt failed and we can work on it?

  5. #5
    Join Date
    Aug 2007
    Posts
    2
    I have written a sample. It should be good enough for you to get started. You should try to implement delete and iterator yourself.

    public class Node <T>{

    private T item;
    private Node<T> next;

    public Node (T item)
    {
    this.item = item;
    }
    public void set(T item)
    {
    this.item = item;
    }

    public T get()
    {
    return item;
    }

    public void setNext(Node<T> node)
    {
    next = node;
    }
    }


    public class LinkedList <T>{
    private Node<T> last;
    private Node<T> first;
    private int count;

    public LinkedList()
    {
    first = null;
    last = null;
    count = 0;
    }

    public LinkedList(T item)
    {
    first = new Node<T>(item);
    last = first;
    count++;
    }

    public void add(T item)
    {
    if(count == 0)
    {
    first = new Node<T>(item);
    last = first;
    }
    else
    {
    Node<T> tmpNode = new Node<T>(item);
    last.setNext(tmpNode);
    last = tmpNode;
    }
    count++;
    }

    public int size()
    {
    return count;
    }

    }



    public class main {

    /**
    * @param args
    */
    static public void main(String[] args) {
    int MAX = 10;
    LinkedList<Integer> list = new LinkedList<Integer>();

    for(int i = 0; i< MAX; i++)
    {
    list.add(i);
    }
    }

    }

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  2. how to implement telent in java
    By saif in forum Java
    Replies: 0
    Last Post: 02-18-2002, 06:39 PM
  3. Re: changing mouse pointer in java
    By Phil Weber in forum Architecture and Design
    Replies: 0
    Last Post: 04-19-2001, 07:36 PM
  4. how to implement VPN in java.
    By jacob in forum Java
    Replies: 0
    Last Post: 01-15-2001, 02:18 AM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center