-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathEx_1_3_47.java
210 lines (175 loc) · 4 KB
/
Ex_1_3_47.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
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
package Ch_1_3;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
import org.junit.Test;
import tools.PrintUtil;
/**
* Created by HuGuodong on 2019/8/2.
*/
public class Ex_1_3_47 {
@Test
public void testQ() {
_Queue<Integer> q1 = new _Queue<>();
_Queue<Integer> q2 = new _Queue<>();
for (int i = 0; i < 5; i++) {
q1.enqueue(i);
}
for (int i = 10; i < 20; i++) {
q2.enqueue(i);
}
q1.concat(q2);
PrintUtil.show(q1);
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while (!q1.isEmpty()) {
StdOut.printf("%d->", q1.dequeue());
}
StdOut.println();
// 0->1->2->3->4->10->11->12->13->14->15->16->17->18->19->
}
public static class _Queue<Item> implements Iterable<Item> {
Node last;
int N;
public void enqueue(Item item) {
Node n = new Node(item);
if (isEmpty()) {
last = n;
last.next = n;
} else {
Node old = last;
last = n;
last.next = old.next; // link first node
old.next = last; // link last node
}
N++;
}
public Item dequeue() {
Item item = last.next.item;
if (last.next == last) {// there is only one node in the link list
last = null;
} else {
last.next = last.next.next;
}
// only one element in the list
N--;
return item;
}
public void concat(_Queue<Item> q) {
if (!q.isEmpty()) {
Node first = last.next;
last.next = q.last.next;
last = q.last;
last.next = first;
N += q.N;
}
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
private class Node {
public Node(Item item) {
this.item = item;
}
Item item;
Node next;
}
public class CircularListIterator implements Iterator<Item> {
private Node cur = N == 0 ? null : last.next; // first node
private int k;
@Override
public boolean hasNext() {
if (cur == null) {
return false;
} else {
return k < N;
}
}
@Override
public Item next() {
Item item = cur.item;
cur = cur.next;
k++;
return item;
}
}
@Override
public Iterator<Item> iterator() {
return new CircularListIterator();
}
}
public static class _Stack<Item> {
private Node last;
private int N;
/**
* e.g.
*/
public void push(Item item) {
Node n = new Node(item);
if (last == null) {
last = n;
} else {
n.next = last.next;
}
last.next = n;
N++;
}
public Item pop() {
Node n = last.next;
last.next = last.next.next;
N--;
return n.item;
}
public void catenation(_Stack<Item> s) {
if (!s.isEmpty() && !this.isEmpty()) {
Node first = last.next;
this.last.next = s.last.next;
this.last = s.last;
this.last.next = first;
N += s.N;
} else if (this.isEmpty() && !s.isEmpty()) {
this.last = s.last;
this.last.next = s.last.next;
this.N = s.N;
}
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public void print() {
if (!isEmpty()) {
Node x = last.next;
while (x != last) {
StdOut.printf("%s->", x.item.toString());
x = x.next;
}
StdOut.printf("%s->\n", x.item.toString());
}
}
private class Node {
public Node(Item item) {
this.item = item;
}
Item item;
Node next;
}
}
public static void main(String[] args) {
_Stack<Integer> s = new _Stack<>();
for (int i = 0; i < 10; i++) {
s.push(i);
}
_Stack<Integer> ss = new _Stack<>();
for (int i = 10; i < 20; i++) {
ss.push(i);
}
s.catenation(ss);
s.print();
}
}