/Users/adelaide/Public/Netbeans - All JSR Projects/Linked List 2/src/linked/list/pkg2/List.java
 1 
 2 package linked.list.pkg2;
 3 
 4 /**
 5  *
 6  * @author John Rayworth
 7  */
 8 public class List {
 9     
10     private Node head;                                              //So only one attribute in this class; the head, which is the only thing there is direct access to.
11     
12     public List(){                                                        //One attribute only means only one attribute to be assigned in the constructor.
13         head = null;
14     }
15     
16     public Node getHead(){                                       //The one and only internal attribute can be returned with this get method.
17         return head;
18     }
19     
20     public boolean isEmpty(){                                   //This method will be required a couple of times to prevent null pointer exceptions that would be thrown if checking for an empty list was not first done.
21         return (head == null);
22     }
23     
24     
25  //ADD TO  THE  LIST:
26     
27     public void addToList(Object data){                   //So with this implementaion of a list - as is normal - we can only add to one end of it.
28         Node newNode = new Node(data);                 //The new node is made...
29         newNode.setNextPointer(head);                     //It links up to the list by pointing to the head...
30         head = newNode;                                            //And since it itself is the last one it, it becomes the head.
31  
32     }
33     
34     
35     
36 //REMOVE  LAS  ADDED  IN
36
37     public Object removeLastIn(){
38         
39         if(isEmpty()){                                                 //Needed because in the case of an empty list, a null pointer expception would be thrown if only doing the code below.
40             return null;
41         }
42         
43         Node lastAddedIn = head;                             //This will be the node which is "chopped off" of the top.
44         head = lastAddedIn.getNextPointer();           //This makes the head the second last Node added.    
45         return lastAddedIn.getData();                        //The return statement has to be last, so it is here that we return the data of what was the last Node in.
46     }
47     
48     
49     
50  //REMOVE  FIRST  ADDED  IN
51     
52     public Object removeFirstAddedIn(){
53         
54         //Case when an empty list
55         if(isEmpty()){                                                //The same idea as above in removeLastIn().
56             return null;
57         }
58         
59         Node current = head;                                     //The current is the Node which will "traverse" down the list to the first one added. It needs to be declared here to be used in the code for 
                                                                                    //if there is only one node.
60         
61 //CASE  WHEN  A  LIST  OF  ONLY  ONE
62         
63         if(current.getNextPointer() == null){             //So this means there must be only one node. 
64             head = null;                                                 //Given that we will remove the one and only node, the head becomes null.
65             return current.getData();                             //The return statement has to be last, so here we return the data.
66         }
67         
68  //ALL  OTHER  CASES  WHERE  MORE  THAN  ONE  NODE
69         
70         Node previous = null;                                     //Before moving to the loop to traverse down the list, we will need another node to keep track of the "previous" node passed on the way down.
71        
72         while(current.getNextPointer() != null){        //This loop keeps on going until the pointer is null, meaning that that node is "the end of the line".
73             previous = current;                                      //And when we get to "the end of the line", we will need access to the two last nodes, so this line always keeps track of the second last one reached.
74             current = current.getNextPointer();            //This is actually what makes the "current" keep on going down and down the list.
75         }
76         previous.setNextPointer(null);                       //We will need to set the pointer of the new last node to be null, there-by "cutting off" the first that had been added...
77         
78         return current.getData();                                //...but even though the last one has been "cut off" by this line, we still have access to it via current (since we kept track of both previous and current each step.)
79     }
80     
81 }
82 
/Users/adelaide/Public/Netbeans - All JSR Projects/Linked List 2/src/linked/list/pkg2/Node.java
 1 
 2 package linked.list.pkg2;
 3 
 4 /**
 5  *
 6  * @author John Rayworth
 7  */
 8 public class Node {                                                                //Pretty much a normal template class:
 9     
10     private Object data;                                                           //Two attributes, though, yes, it's curious that one of them is an instance of the class itself...
11     private Node nextPointer;                                                 //... but that's handy because the "pointers", as objects, can be used in the List class as the addresses they actually are.
12     
13     public Node(Object data){                                                //The constructor.
14         this.data = data;
15         nextPointer = null;
16     }
17     
18     public void setData(Object data){                                     //A couple of "set" methods.
19         this.data = data;
20     }
21     
22     public void setNextPointer(Node nextPointer){
23         this.nextPointer = nextPointer;
24     }
25     
26     public Object getData(){                                                    //And a couple of "get" methods.
27         return data;
28     }
29     
30     public Node getNextPointer(){
31         return nextPointer;
32     }
33     
34 }
35