-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathEx_1_3_41.java
More file actions
145 lines (115 loc) · 2.98 KB
/
Ex_1_3_41.java
File metadata and controls
145 lines (115 loc) · 2.98 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
package Ch_1_3;
import edu.princeton.cs.algs4.StdOut;
import java.util.Iterator;
import tools.PrintUtil;
/**
* Created by HuGuodong on 2019/7/28.
*/
public class Ex_1_3_41 {
public static class _Queue<Item> implements Iterable<Item> {
public _Queue(_Queue<Item> another) {
if (!another.isEmpty()) {
for (Item item : another) {
// this.enqueue(item);
try {
this.enqueue((Item) item.getClass().getDeclaredMethod("clone", null).invoke(item));
} catch (Exception e) {
e.printStackTrace();
} finally {
this.enqueue(item);
}
}
}
}
public _Queue() {
}
private Node first; // link to the least added node
private Node last; // link to hte most added node
private int N;
private class Node {
Item item;
Node next;
}
public void enqueue(Item item) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) {
first = last;
} else {
oldlast.next = last;
}
N++;
}
public Item dequeue() {
Item item = first.item;
first = first.next;
if (isEmpty()) {
last = null;
}
N--;
return item;
}
public int size() {
return N;
}
public boolean isEmpty() {
return N == 0;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
}
}
public static class Student implements Cloneable {
String name;
public Student(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
Student other = new Student(this.name);
return other;
}
}
public static void main(String[] args) {
_Queue<Student> q = new _Queue<>();
Student st_1 = new Student("st_1");
Student st_2 = new Student("st_2");
Student st_3 = new Student("st_3");
Student st_4 = new Student("st_4");
q.enqueue(st_1);
q.enqueue(st_2);
q.enqueue(st_3);
q.enqueue(st_4);
_Queue<Student> r = new _Queue<>(q);
Student st_5 = new Student("st_5");
r.enqueue(st_5);
q.dequeue();
st_4.name = "modify"; // modify st_4, but this operation does not effect element in the new queue.
PrintUtil.show(q);
PrintUtil.show(r);
// Student{name='st_2'} Student{name='st_3'} Student{name='modify'}
// Student{name='st_1'} Student{name='st_2'} Student{name='st_3'} Student{name='st_4'} Student{name='st_5'}
}
}