Results 1 to 2 of 2
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default What does LinkedList.Node head = linkedList.head(); does?

    In the following code, what does
    Java Code:
     LinkedList.Node head = linkedList.head();
    line does?

    Java Code:
    public class LinkedListTest {
     
     
        public static void main(String args[]) {
            //creating LinkedList with 5 elements including head
          LinkedList linkedList = new LinkedList();
          LinkedList.Node head = linkedList.head();
          linkedList.add( new LinkedList.Node("1"));
          linkedList.add( new LinkedList.Node("2"));
          linkedList.add( new LinkedList.Node("3"));
          linkedList.add( new LinkedList.Node("4"));
       
          //finding middle element of LinkedList in single pass
          LinkedList.Node current = head;
          int length = 0;
          LinkedList.Node middle = head;
       
          while(current.next() != null){
              length++;
              if(length%2 ==0){
                  middle = middle.next();
              }
              current = current.next();
          }
        
          if(length%2 == 1){
              middle = middle.next();
          }
    
          System.out.println("length of LinkedList: " + length);
          System.out.println("middle element of LinkedList : " + middle);
         
        }
     
    }
    
    // Class LinkedList
    
    class LinkedList{
        private Node head;
        private Node tail;
     
        public LinkedList(){
            this.head = new Node("head");
            tail = head;
        }
     
        public Node head(){
            return head;
        }
     
        public void add(Node node){
            tail.next = node;
            tail = node;
        }
        
        // NODE CLASS BEGINS
     
        public static class Node{
            private Node next;
            private String data;
    
            public Node(String data){
                this.data = data;
            }
         
            public String data() {
                return data;
            }
    
            public void setData(String data) {
                this.data = data;
            }
    
            public Node next() {
                return next;
            }
    
            public void setNext(Node next) {
                this.next = next;
            }
         
            public String toString(){
                return this.data;
            }
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: What does LinkedList.Node head = linkedList.head(); does?

    Quote Originally Posted by Jack_Tauson_Sr View Post
    In the following code, what does
    Java Code:
     LinkedList.Node head = linkedList.head();
    line does?
    You didn't write that class yourself did you? Node is a statically nested class in the LinkedList class; so to anything not in the outer class a node has to be refered to as LinkedList.Node. head() is a method in the LinkedList class. For more details, ask the author of that class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Head First Java Book question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 05-02-2011, 09:23 PM
  2. Java Project Head, Kochi
    By enroutehuma in forum Jobs Offered
    Replies: 1
    Last Post: 11-09-2009, 06:44 PM
  3. Monitor Disc Head Using Java
    By zorroforce in forum New To Java
    Replies: 4
    Last Post: 09-18-2009, 07:51 AM
  4. HEAD first by kathy sierra
    By j2vdk in forum New To Java
    Replies: 0
    Last Post: 08-30-2008, 01:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •