예제 #1
0
        public void addTo(Node n)
        {
            /*
                if the head is null then the list is empty and thus the added element becomes head - the only element in the list
                If the head is not null, the list is not empty and therefore new element will be added at the end of the list.
                The element will be added at the end of the list.
                The end of the list is reached once the next element in the list is null
            */
            if (head == null) {
                head = n;

                // the List is not empty
            } else {
                //set Current node to the first node in the list. aka the head node.
                current = head;

                //triverse the list untll the end (next element is null is reached)
                while(current.getLink() != null)
                {
                    //set current node to the node linkd to it
                    current = current.getLink();
                }

                //adding new node at the end of the list by setting the
                current.setLink (n);

            }
        }
예제 #2
0
 //printing the linked list
 public void printlist()
 {
     //starting at the beginnign of the list
     current = head;
     //if the head is empty then the whole list is empty
     if(head == null)
     {
         Console.WriteLine ("List is Empty");
     }
     //if not empty we traverse the whole list top to bottom
      else {
         while(current.getLink() != null)
         {
             Console.WriteLine (current.getValue());
             current = current.getLink ();
         }
         //printing the last element of the list
         Console.WriteLine (current.getValue());
     }
 }
예제 #3
0
 public void setLink(Node n)
 {
     link = n;
 }