1+ /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
2+ * E-Mail : info@careermonk.com
3+ * Creation Date : 2015-01-10 06:15:46
4+ * Last modification : 2006-05-31
5+ by : Narasimha Karumanchi
6+ * File Name : LinkedList.java
7+ * Book Title : Data Structures And Algorithms Made In Java
8+ * Warranty : This software is provided "as is" without any
9+ * warranty; without even the implied warranty of
10+ * merchantability or fitness for a particular purpose.
11+ *
12+ */
13+
14+ package chapter3linkedlists ;
15+
16+ public class LinkedList {
17+ // This class has a default constructor:
18+ public LinkedList () {
19+ length = 0 ;
20+ }
21+
22+ // This is the only field of the class. It holds the head of the list
23+ ListNode head ;
24+
25+ // Length of the linked list
26+ private int length = 0 ;
27+
28+ // Return the first node in the list
29+ public synchronized ListNode getHead () {
30+ return head ;
31+ }
32+
33+ // Insert a node at the beginning of the list
34+ public synchronized void insertAtBegin (ListNode node ) {
35+ node .setNext (head );
36+ head = node ;
37+ length ++;
38+ }
39+
40+ // Insert a node at the end of the list
41+ public synchronized void insertAtEnd (ListNode node ) {
42+ if (head == null )
43+ head = node ;
44+ else {
45+ ListNode p , q ;
46+ for (p = head ; (q = p .getNext ()) != null ; p = q );
47+ p .setNext (node );
48+ }
49+ length ++;
50+ }
51+
52+ // Add a new value to the list at a given position.
53+ // All values at that position to the end move over to make room.
54+ public void insert (int data , int position ) {
55+ // fix the position
56+ if (position < 0 ) {
57+ position = 0 ;
58+ }
59+ if (position > length ) {
60+ position = length ;
61+ }
62+
63+ // if the list is empty, make it be the only element
64+ if (head == null ) {
65+ head = new ListNode (data );
66+ }
67+ // if adding at the front of the list...
68+ else if (position == 0 ) {
69+ ListNode temp = new ListNode (data );
70+ temp .next = head ;
71+ head = temp ;
72+ }
73+ // else find the correct position and insert
74+ else {
75+ ListNode temp = head ;
76+ for (int i =1 ; i <position ; i +=1 ) {
77+ temp = temp .getNext ();
78+ }
79+ ListNode newNode = new ListNode (data );
80+ newNode .next = temp .next ;
81+ temp .setNext (newNode );
82+ }
83+ // the list is now one value longer
84+ length += 1 ;
85+ }
86+
87+ // Remove and return the node at the head of the list
88+ public synchronized ListNode removeFromBegin () {
89+ ListNode node = head ;
90+ if (node != null ) {
91+ head = node .getNext ();
92+ node .setNext (null );
93+ }
94+ return node ;
95+ }
96+
97+ // Remove and return the node at the end of the list
98+ public synchronized ListNode removeFromEnd () {
99+ if (head == null )
100+ return null ;
101+ ListNode p = head , q = null , next = head .getNext ();
102+ if (next == null ) {
103+ head = null ;
104+ return p ;
105+ }
106+ while ((next = p .getNext ()) != null ) {
107+ q = p ;
108+ p = next ;
109+ }
110+ q .setNext (null );
111+ return p ;
112+ }
113+
114+ // Remove a node matching the specified node from the list.
115+ // Use equals() instead of == to test for a matched node.
116+ public synchronized void removeMatched (ListNode node ) {
117+ if (head == null )
118+ return ;
119+ if (node .equals (head )) {
120+ head = head .getNext ();
121+ return ;
122+ }
123+ ListNode p = head , q = null ;
124+ while ((q = p .getNext ()) != null ) {
125+ if (node .equals (q )) {
126+ p .setNext (q .getNext ());
127+ return ;
128+ }
129+ p = q ;
130+ }
131+ }
132+
133+ // Remove the value at a given position.
134+ // If the position is less than 0, remove the value at position 0.
135+ // If the position is greater than 0, remove the value at the last position.
136+ public void remove (int position ) {
137+ // fix position
138+ if (position < 0 ) {
139+ position = 0 ;
140+ }
141+
142+ if (position >= length ) {
143+ position = length -1 ;
144+ }
145+
146+ // if nothing in the list, do nothing
147+ if (head == null )
148+ return ;
149+
150+ // if removing the head element...
151+ if (position == 0 ) {
152+ head = head .getNext ();
153+ }
154+
155+ // else advance to the correct position and remove
156+ else {
157+ ListNode temp = head ;
158+ for (int i =1 ; i <position ; i +=1 ) {
159+ temp = temp .getNext ();
160+ }
161+ temp .setNext (temp .getNext ().getNext ());
162+ }
163+ // reduce the length of the list
164+ length -= 1 ;
165+ }
166+
167+ // Return a string representation of this collection, in the form ["str1","str2",...].
168+ public String toString () {
169+ String result = "[" ;
170+ if (head == null ) {
171+ return result +"]" ;
172+ }
173+ result = result + head .getData ();
174+ ListNode temp = head .getNext ();;
175+ while (temp != null ) {
176+ result = result + "," + temp .getData ();
177+ temp = temp .getNext ();
178+ }
179+ return result + "]" ;
180+ }
181+
182+ // Return the current length of the list.
183+ public int length () {
184+ return length ;
185+ }
186+
187+ // Find the position of the first value that is equal to a given value.
188+ // The equals method us used to determine equality.
189+ public int getPosition (int data ) {
190+ // go looking for the data
191+ ListNode temp = head ;
192+ int pos = 0 ;
193+ while (temp != null ) {
194+ if (temp .getData () == data ) {
195+ // return the position if found
196+ return pos ;
197+ }
198+ pos += 1 ;
199+ temp = temp .getNext ();
200+ }
201+ // else return -1
202+ return Integer .MIN_VALUE ;
203+ }
204+ }
0 commit comments