Repository navigation
Expand file tree
/
Copy pathLList.java
More file actions
245 lines (227 loc) · 4.62 KB
/
Copy pathLList.java
File metadata and controls
245 lines (227 loc) · 4.62 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
//Tristhal Parasrm
//LList.java
//
//Represents a linked list and manipulates it
import java.util.*;
public class LList{
private LNode head;
private LNode tail;
public LList(){
head = null
}
///
public void push(final int n){//adds to the head
//rewires the next and last
LNode tmp = new LNode(n, head);
if (head==null){
tail=tmp;
}
head = tmp;
if(head.getLast()==null){
}
else{
head.setLast(tmp);
}
}
public LNode pop(){
//removes front making the one behind it the head
LNode tmp=head;
head=head.getNext();
head.setLast(null);
return tmp;
}
///
public void enqueue(final int n){//add to the tail
if(head==null){
push(n);
}
else{
LNode temp=new LNode(n,null);
temp.setLast(tail);//reworks pointers
tail.setNext(temp);
tail=temp;
}
}
public LNode dequeue(){//pop
return pop();
}
///
public void delete(LNode e){//contains cases for head and tail as e
//redefines head and tail if necessary
//otherwise it just reworks pointers
if(e.getNext()==null || e.getLast()==null){
if(e==tail){
if(e==head){
tail=null;
head=null;
}
else{
tail=e.getLast();
tail.setNext(null);
}
}
else if(e==head){
head=e.getNext();
head.setLast(null);
}
else{
e.getNext().setLast(null);
e.getLast().setNext(null);
}
}
else{
e.getNext().setLast(e.getLast());
e.getLast().setNext(e.getNext());
}
}
public void delete(int n){//searches for the node with n
//if it finds it it calls delete for that node
//otherwise it ends
LNode observed=tail;
if(observed.getVal()==n){
delete(observed);
}
else{
while (true){
observed=observed.getLast();
if(observed==head){
if(observed.getVal()==n){
delete(observed);
}
break;
}
else if(observed.getVal()==n){
delete(observed);
break;
}
}
}
}
public void deleteAt(int pos){//assumes a valid indicie is given
//finds the node at position pos starting from the tail going towards the head.
//pos 0 = tail
LNode observed=tail;
int i=0;
while (true){
if (i==pos){
break;
}
else{
observed=observed.getLast();
i++;
}
}
delete(observed);
}
public void sortedInsert(int num){//goes through untill the value
//of a lnode is >=num then it insertes it behind that
LNode e= new LNode(num,null);
LNode observed=tail;
int i=0;
while(true){
System.out.println(observed.getLast());
if(observed.getVal()>=e.getVal()){
if(observed==tail){
enqueue(e.getVal());
break;
}
else{
e.setNext(observed.getNext());
e.getNext().setLast(e);
e.setLast(observed);
observed.setNext(e);
break;
}
}
else if(observed==head){//largest value in the list case
LNode temp=head;
push(e.getVal());
temp.setLast(head);
break;
}
else{
observed=observed.getLast();
i++;
}
}
}
public void reverse(){//swaps last and next and head and tail
LNode observed= tail;
LNode lst;
LNode nxt;
while (true){
if(observed==head){
lst=observed.getNext();
nxt=observed.getLast();
observed.setNext(nxt);
observed.setLast(lst);
LNode tmp=head;
head=tail;
tail=tmp;
break;
}
lst=observed.getNext();
nxt=observed.getLast();
observed.setNext(nxt);
observed.setLast(lst);
observed=observed.getNext();
}
}
public void removeDuplicates(){//stores used values and removes
//nodes that are repeated
LNode observed=tail;
ArrayList<Integer> used=new ArrayList<Integer>();
while (true){
if (observed==head){
if(used.contains(observed.getVal())){
LNode tmp=observed;
observed=observed.getLast();
delete(tmp);
}
break;
}
else if(used.contains(observed.getVal())){
LNode tmp=observed;
observed=observed.getLast();
delete(tmp);
}
else{
used.add(observed.getVal());
observed=observed.getLast();
}
}
}
public LList clone(){//it starts from the head and
//adds reconstructs the list by inputting lnode values values to the tail
LList copy=new LList();
copy.enqueue(head.getVal());
LNode observed=head;
while (true){
if (observed==tail){
break;
}
observed=observed.getNext();
copy.enqueue(observed.getVal());
}
return copy;
}
public LNode returntail(){
return tail;
}
public LNode returnhead(){
return head;
}
public void setTail(LNode t){
tail=t;
}
public String toString(){
String ans = "";
LNode tmp = head;
while (tmp != null){
ans += tmp + ", ";
tmp = tmp.getNext();
}
ans = ans.substring(0, ans.length() - 2);
return "{" + ans + "}";
}
}