8
8
* This implementation uses a singly linked list to store the elements.
9
9
* Further details can be found on this link
10
10
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
11
- *
12
11
* @author Muhammad Junaid Khalid
13
12
* @param int the type of elements in this list
14
13
*/
@@ -24,24 +23,20 @@ public SortedLinkedList() {
24
23
25
24
/**
26
25
* Inserts a new element into the sorted linked list.
27
- *
28
26
* @param value the value to be inserted
29
27
*/
30
28
public void insert (int value ) {
31
29
Node newNode = new Node (value );
32
30
if (head == null ) {
33
31
this .head = newNode ;
34
32
this .tail = newNode ;
35
- }
36
- else if (value < head .value ) {
33
+ } else if (value < head .value ) {
37
34
newNode .next = this .head ;
38
35
this .head = newNode ;
39
- }
40
- else if (value > tail .value ) {
36
+ } else if (value > tail .value ) {
41
37
this .tail .next = newNode ;
42
38
this .tail = newNode ;
43
- }
44
- else {
39
+ } else {
45
40
Node temp = head ;
46
41
while (temp .next != null && temp .next .value < value ) {
47
42
temp = temp .next ;
@@ -63,24 +58,21 @@ public void display() {
63
58
64
59
/**
65
60
* Deletes the first occurrence of the specified element in the sorted linked list.
66
- *
67
61
* @param value the value to be deleted
68
62
* @return true if the element is found and deleted, false otherwise
69
63
*/
70
- public boolean delete (int value ){
64
+ public boolean delete (int value ) {
71
65
if (this .head == null ) {
72
66
return false ;
73
- }
74
- else if (this .head .value == value ) {
67
+ } else if (this .head .value == value ) {
75
68
if (this .head .next == null ) {
76
69
this .head = null ;
77
70
this .tail = null ;
78
71
} else {
79
72
this .head = this .head .next ;
80
73
}
81
74
return true ;
82
- }
83
- else {
75
+ } else {
84
76
Node temp = this .head ;
85
77
while (temp .next != null ) {
86
78
if (temp .next .value == value ) {
@@ -93,13 +85,11 @@ else if (this.head.value == value) {
93
85
temp = temp .next ;
94
86
}
95
87
return false ;
96
-
97
88
}
98
89
}
99
90
100
91
/**
101
92
* Searches for the specified element in the sorted linked list.
102
- *
103
93
* @param value the value to be searched
104
94
* @return true if the element is found, false otherwise
105
95
*/
@@ -116,7 +106,6 @@ public boolean search(int value) {
116
106
117
107
/**
118
108
* Checks if the sorted linked list is empty.
119
- *
120
109
* @return true if the list is empty, false otherwise
121
110
*/
122
111
public boolean isEmpty () {
@@ -125,7 +114,6 @@ public boolean isEmpty() {
125
114
126
115
/**
127
116
* Returns the minimum value in the sorted linked list.
128
- *
129
117
* @return the minimum value
130
118
*/
131
119
public int minValue () {
@@ -134,7 +122,6 @@ public int minValue() {
134
122
135
123
/**
136
124
* Returns the maximum value in the sorted linked list.
137
- *
138
125
* @return the maximum value
139
126
*/
140
127
public int maxValue () {
@@ -143,21 +130,19 @@ public int maxValue() {
143
130
144
131
/**
145
132
* Returns a string representation of the sorted linked list.
146
- *
147
133
* @return a string representation of the sorted linked list
148
134
*/
149
135
@ Override
150
136
public String toString () {
151
137
if (this .head != null ) {
152
- ArrayList <String > elements = new ArrayList <>();
138
+ ArrayList <String > elements = new ArrayList <>();
153
139
Node temp = this .head ;
154
140
while (temp != null ) {
155
141
elements .add (String .valueOf (temp .value ));
156
142
temp = temp .next ;
157
143
}
158
144
return String .join (", " , elements );
159
- }
160
- else {
145
+ } else {
161
146
return "" ;
162
147
}
163
148
}
0 commit comments