Source of StringLinkedList.java


  1: public class StringLinkedList
  2: {
  3:         private ListNode head;
  4: 
  5:         public StringLinkedList()
  6:         {
  7:                 head = null;
  8:         }
  9: 
 10:         /**
 11:          Returns the number of nodes in the list
 12:         */
 13:         public int length()
 14:         {
 15:                 int count = 0;
 16:                 ListNode position = head;
 17:                 while(position != null)
 18:                 {
 19:                         count++;
 20:                         position = position.link;
 21:                 }
 22:                 return count;
 23:         }
 24: 
 25:         /**
 26:          Adds a node at the start of the list.  The added node has
 27:          addData as its data.  The added node will be the first
 28:          node in the list.
 29:         */
 30:         public void addANodeToStart(String addData)
 31:         {
 32:                 head = new ListNode(addData, head);
 33:         }
 34: 
 35:         public void deleteHeadNode()
 36:         {
 37:                 if(head != null)
 38:                         head = head.link;
 39:                 else
 40:                 {
 41:                         System.out.println("Deleting from and empty list.");
 42:                         System.exit(0);
 43:                 }
 44:         }
 45: 
 46:         public boolean onList(String target)
 47:         {
 48:                 return(Find(target) != null);
 49:         }
 50: 
 51:         /**
 52:          Finds the first node containing the target data
 53:          and returns a reference to that node.  If target is not
 54:          in the list, null is returned.
 55:         */
 56:         private ListNode Find(String target)
 57:         {
 58:                 ListNode position;
 59:                 position = head;
 60:                 String dataAtPosition;
 61:                 while(position != null)
 62:                 {
 63:                         dataAtPosition = position.data;
 64:                         if(dataAtPosition.equals(target))
 65:                                 return position;
 66:                         position = position.link;
 67:                 }
 68:                 //target was not found
 69:                 return null;
 70:         }
 71: 
 72:         public void showList()
 73:         {
 74:                 ListNode position;
 75:                 position = head;
 76:                 while(position != null)
 77:                 {
 78:                         System.out.println(position.data);
 79:                         position = position.link;
 80:                 }
 81:         }
 82: 
 83:         private class ListNode
 84:         {
 85:                 private String data;
 86:                 private ListNode link;
 87: 
 88:                 public ListNode()
 89:                 {
 90:                         link = null;
 91:                         data = null;
 92:                 }
 93: 
 94:                 public ListNode(String newData, ListNode linkValue)
 95:                 {
 96:                         data = newData;
 97:                         link = linkValue;
 98:                 }
 99:         }
100: }