-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL1Q2.java
154 lines (146 loc) · 3.24 KB
/
L1Q2.java
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
class Livro{
String code;
Livro next;
Livro prev;
Livro(String code){
this.code = code;
this.next = null;
this.prev = null;
}
}
class Lista{
Livro head;
Livro sent = new Livro("-1");
Livro mark;
boolean first;
Lista(){
this.head = null;
this.sent.next=head;
this.sent.prev=null;
this.sent.code="-1";
this.first = true;
this.mark = head;
}
void printLista(Arquivo arq){
Livro aux = head;
while (aux!=null){
arq.println("[" + aux.code + "]");
aux = aux.next;
}
}
boolean isEmpty(){
if (head!=null) return false;
return true;
}
String pass(String code){
String str = code.substring(1, code.length()-1);
return str;
}
void add(String code){
code = pass(code);
Livro novo = new Livro(code);
if (head==null){ //lista vazia
head = novo;
novo.prev = sent;
novo.next = null;
mark = head;
} else { //lista não vazia
Livro curr = head;
while (curr.next!=null && code.compareToIgnoreCase(curr.code)>0){
curr = curr.next;
}
if (curr==head){ //lista com somente head
if (code.compareToIgnoreCase(head.code)>0){ //inserir antes do head
curr.next = novo;
novo.prev = curr;
novo.next = null;
} else { //inserir depois do head
curr.prev = novo;
head = novo;
head.next = curr;
mark = head;
}
} else if (curr.next==null){ //chegou no final da lista
if (code.compareToIgnoreCase(curr.code)>0){ //inserir no final da lista
curr.next = novo;
novo.prev = curr;
novo.next = null;
} else { //inserir logo antes do final da lista
curr.prev.next=novo;
novo.next=curr;
novo.prev = curr.prev;
curr.prev=novo;
}
} else if (curr.next!=null && curr.prev!=null){ //inserir no meio
curr.prev.next=novo;
novo.next=curr;
novo.prev = curr.prev;
curr.prev=novo;
}
}
}
void remove(int n){
if (!isEmpty()){
if (this.first){ //primeira remoção
while (n>0){
if (head!=null && head.next!=null) head.next.prev=head.prev;
if (!isEmpty()) head = head.next;
n--;
}
mark = head;
this.first = false;
} else { //a partir da segunda
while (n>0){
if (!isEmpty()){
if (mark.next!=null && mark!=head){ //remoção no meio
mark.prev.next = mark.next;
mark.next.prev = mark.prev;
mark = mark.next;
} else { //mark.next==null || mark==head
if (mark==head && mark.next!=null){
head.next.prev=head.prev;
head = head.next;
mark = head;
} else { //mark.next=null
if (mark==head){
mark.prev = sent;
sent.next = head = mark = null;
} else {
mark.prev.next=null;
mark = head;
}
}
}
n--;
}
}
}
} else {
return;//isEmpty
}
}//remover
}
public class L1Q2 {
public static void main(String[] args) {
Arquivo arq = new Arquivo("L1Q2.in", "L1Q2.out");
Lista l = new Lista();
while (!arq.isEndOfFile()){
char in = arq.readString().charAt(0);
if (in!= '0'){
switch (in){
case 'A':
l.add(arq.readString());
break;
case 'B':
l.remove((arq.readInt()));
break;
}
} else {
l.printLista(arq);
if (!arq.isEndOfFile()) arq.println();
l = new Lista();
}
}
arq.close();
}
}