Java Reference
In-Depth Information
Display 15.8
A Generic Linked List Class (part 3 of 3)
91 }
92 public void outputList( )
93 {
94 Node<T> position = head;
95 while (position != null )
96 {
97 System.out.println(position.data);
98 position = position.link;
99 }
100 }
Type T must have a well-defined toString
methods for this to work.
101 public boolean isEmpty( )
102 {
103
return (head == null );
104 }
105 public void clear( )
106 {
107 head = null ;
108 }
109 /*
110 For two lists to be equal they must contain the same data items in
111 the same order. The equals method of T is used to compare data
items .
112 */
113 public boolean equals(Object otherObject)
114 {
115 if (otherObject == null )
116 return false ;
117 else if (getClass( ) != otherObject.getClass( ))
118 return false ;
119 else
120 {
121 LinkedList3<T> otherList = (LinkedList3<T>)otherObject;
122 if (size( )!= otherList.size( ))
123 return false ;
124 Node<T> position = head;
125 Node<T> otherPosition = otherList.head;
126 while (position != null )
127 {
128 if (!(position.data.equals(otherPosition.data)))
129 return false ;
130 position = position.link;
131 otherPosition = otherPosition.link;
132 }
133
return true ; //no mismatch was not found
134 }
135 }
136 }
 
Search WWH ::




Custom Search