Repository navigation
Expand file tree
/
Copy pathSLL.java
More file actions
245 lines (214 loc) · 7.05 KB
/
Copy pathSLL.java
File metadata and controls
245 lines (214 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//Дадена е еднострана поврзана листа чии што јазли содржат по еден String. Дополнително, даден е и уште еден природен број L. Од дадената листа потребно е да се најде последниот јазел којшто содржи String со должина L и да се премести на почеток на листата.
//
// Влез: Во првиот ред од влезот е даден бројот на елементи во листата - N, па во следните N редови се дадени самите елементи (String-ови) од листата. На крај, во последниот ред е даден бројот L.
//
// Излез: На излез треба да се испечати листата пред и после промената.
//
// /
//
// You are given a single linked list with String nodes. Additionally, you are given one more integer L. You need to find the last node that contains a String with exactly L characters, and move it to the front of the list.
//
// Input: In the first line from the input the number of elements in the list is given - N, and then in the next N lines the elements themselves. In the last line, the integer L is given.
//
// Output: The list before and after the transformation
//
//
// For example:
//
// Input Result
// 3
// Sky
// Blue
// Cloud
// 5
// Sky->Blue->Cloud
// Cloud->Sky->Blue
import java.util.Scanner;
class SLLNode<E> {
protected E element;
protected SLLNode<E> succ;
public SLLNode(E elem, SLLNode<E> succ) {
this.element = elem;
this.succ = succ;
}
}
class SLL<E> {
private SLLNode<E> first;
public SLL() {
// Construct an empty SLL
this.first = null;
}
public void deleteList() {
first = null;
}
public int size() {
int listSize = 0;
SLLNode<E> tmp = first;
while(tmp != null) {
listSize++;
tmp = tmp.succ;
}
return listSize;
}
@Override
public String toString() {
String ret = new String();
;
if (first != null) {
SLLNode<E> tmp = first;
ret += tmp.element;
while (tmp.succ != null) {
tmp = tmp.succ;
ret += "->" + tmp.element;
}
} else
ret = "Prazna lista!!!";
return ret;
}
public void insertFirst(E o) {
SLLNode<E> ins = new SLLNode<E>(o, null);
ins.succ = first;
//SLLNode<E> ins = new SLLNode<E>(o, first);
first = ins;
}
public void insertAfter(E o, SLLNode<E> node) {
if (node != null) {
SLLNode<E> ins = new SLLNode<E>(o, node.succ);
node.succ = ins;
} else {
System.out.println("Dadenot jazol e null");
}
}
public void insertBefore(E o, SLLNode<E> before) {
if (first != null) {
SLLNode<E> tmp = first;
if(first==before){
this.insertFirst(o);
return;
}
//ako first!=before
while (tmp.succ != before && tmp.succ!=null)
tmp = tmp.succ;
if (tmp.succ == before) {
tmp.succ = new SLLNode<E>(o, before);;
} else {
System.out.println("Elementot ne postoi vo listata");
}
} else {
System.out.println("Listata e prazna");
}
}
public void insertLast(E o) {
if (first != null) {
SLLNode<E> tmp = first;
while (tmp.succ != null)
tmp = tmp.succ;
tmp.succ = new SLLNode<E>(o, null);
} else {
insertFirst(o);
}
}
public E deleteFirst() {
if (first != null) {
SLLNode<E> tmp = first;
first = first.succ;
return tmp.element;
} else {
System.out.println("Listata e prazna");
return null;
}
}
public E delete(SLLNode<E> node) {
if (first != null) {
SLLNode<E> tmp = first;
if(first == node) {
return this.deleteFirst();
}
while (tmp.succ != node && tmp.succ.succ != null)
tmp = tmp.succ;
if (tmp.succ == node) {
tmp.succ = tmp.succ.succ;
return node.element;
} else {
System.out.println("Elementot ne postoi vo listata");
return null;
}
} else {
System.out.println("Listata e prazna");
return null;
}
}
public SLLNode<E> getFirst() {
return first;
}
public SLLNode<E> find(E o) {
if (first != null) {
SLLNode<E> tmp = first;
while (!tmp.element.equals(o) && tmp.succ != null)
tmp = tmp.succ;
if (tmp.element.equals(o)) {
return tmp;
} else {
System.out.println("Elementot ne postoi vo listata");
}
} else {
System.out.println("Listata e prazna");
}
return null;
}
public void merge (SLL<E> in){
if (first != null) {
SLLNode<E> tmp = first;
while(tmp.succ != null)
tmp = tmp.succ;
tmp.succ = in.getFirst();
}
else{
first = in.getFirst();
}
}
public void mirror() {
if (first != null) {
//m=nextsucc, p=tmp,q=next
SLLNode<E> tmp = first;
SLLNode<E> newsucc = null;
SLLNode<E> next;
while(tmp != null){
next = tmp.succ;
tmp.succ = newsucc;
newsucc = tmp;
tmp = next;
}
first = newsucc;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
SLL<String> list = new SLL<>();
for (int i = 0; i < N; i++) {
list.insertLast(sc.next());
}
int L = sc.nextInt();
// Print the list before the transformation
System.out.println(list);
// Step 1: Find the last node with string length L
SLLNode<String> current = list.getFirst();
SLLNode<String> lastMatch = null;
while (current != null) {
if (current.element.length() == L) {
lastMatch = current;
}
current = current.succ;
}
// Step 2: If such a node was found and it's not already the first, move it to front
if (lastMatch != null && lastMatch != list.getFirst()) {
list.delete(lastMatch);
list.insertFirst(lastMatch.element);
}
// Print the list after the transformation
System.out.println(list);
}
}