0

Trying to implement single-linked-list in below program, i am really not able to undertsand how to add a node in an Linked list (for start, m trying it on empty linked list).

To put it plain simple,i tried to setData and setNext but getSizeofList() return 0 everytime....its really looking like a rocket science to me now!!

Question : Can some-one tell me how to implement it....or rather, add a node to existing linked list....

What i have tried so far and why they dint worked out: i referenced multiple programs but they were too complex for me to understand(rocket science), so wrote below program from what i understood from algorithms....but even in algo's, they just show methods on how to implement and this is where i failed, as, i dont understand,what data-type and value is to be passed for adding a node...


please not that m not a java guy, so please go easy, this question comes in as an attempt to learn

package Data_S;

public class Linked_List {

    private int data;
    private Linked_List next_ptr;
    private Linked_List headNode = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Linked_List ll = new Linked_List();
        //ll.setnext(25);
        ll.insert_node(24);
        ll.traverse();
        ll.getSizeofList();
    }


    //size of list
    public void getSizeofList()
    {
        int l = 0;
        Linked_List curr = headNode;
        while(curr != null)
        {
            l++;
            curr = curr.getnext();
        }
        System.out.print("Size of list is = "+l);
    }

    //insert node
    public void insert_node(/*Linked_List node, */int data)
    {
        if(headNode == null)
        {
            System.out.println("in insert"); // checking
            this.setnext(headNode);
            this.setData(data);
            System.out.print("value = "+this.getData());
        }
    }

   //set data for this node
    public void setData(int data)
    {
        this.data = data;
    }

    //return the data
    public int getData()
    {
        return this.data;
    }

    //set next pointer
    public void setnext(Linked_List next_ptr)
    {
        this.next_ptr = next_ptr;
    }

    //get next pointer
    public Linked_List getnext()
    {
        return this.next_ptr;
    }


}
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
  • It's good that you're trying to learn, but there are hundreds of descriptions on linked lists in books and on web sites. What exactly don't you understand about how the insertion works? – Joni Feb 07 '14 at 16:18
  • @Joni : i am not able to picture out how it is allocated in memory....like `arrays`, they are simple as they don't have `next` and `fwd` pointer only a continuous block.....to answer your question, i fail to understand that how one block of data can have `pointers` kind of thing in java so that it can be attached.....i just want to understand, how this so called `previous/next` pointers of java are there in memory and how they work!! – NoobEditor Feb 07 '14 at 16:24
  • 1
    @NoobEditor: The data is allocated in separate places unrelated to the other data. For each node you allocate enough space for 3 pointers which point to 3 abstract points in memory that contain the previous node, the next node, and the current node's data. – Stas Jaro Feb 07 '14 at 16:27
  • You don't necessarily need to have a previous link btw. – Stas Jaro Feb 07 '14 at 16:28
  • @stas : `previous` wud be needed for double and circular LL...and enuf `3 pointers` thing even i understood...what i fail to do is implement that in code.... *and this is what my problem is*!! :\ – NoobEditor Feb 07 '14 at 16:35

2 Answers2

1

You have to make a distinction between the single chains (Node) of a linked list, and the entire container (LinkedList).

public class LinkedList {
    Node head;
    int size; // Maybe

    public void insertAtEnd(int data) {
        Node previous = null;
        for (Node current = head; current != null; current = current.next) {
            previous = current;
        }
        Node baby = new Node(data);
        if (previous == null) {
            head = baby;
        } else {
            previous.next = baby;
        }
        ++size;
    }

    public void insertInSortedList(int data) {
        Node previous = null;
        Node current = null;
        for (current = head; current != null && data < current.data;
                current = current.next) {
            previous = current;
        }
        Node baby = new Node(data);
        baby.next = current;
        if (previous == null) {
            head = baby;
        } else {
            previous.next = baby;
        }
        ++size;
    }
}

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

One may sometimes see encapsulation as:

public class LinkedList {
    private static class Node {
    }
    ...
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • +1 : wow....`you have to make a distinction between the single chains` cleared almost every doubt....thanks a lot,answer accepted!!! :) – NoobEditor Feb 07 '14 at 17:35
0

You never set headnode. In insertnode you just use setnext which does not set headnode. You are mixing the top class and the node implementation together.

Here is an example of how to implement a linked list in java for further reference: How do I create a Linked List Data Structure in Java?

Community
  • 1
  • 1
Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
  • i saw that reference link and the accepted answer....but follow up comments in that answer put me to doubt!! :\ – NoobEditor Feb 07 '14 at 16:33