Source of StringLinkedListSelfcontained.java


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