-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMyQueue.java
More file actions
151 lines (130 loc) · 3.35 KB
/
MyQueue.java
File metadata and controls
151 lines (130 loc) · 3.35 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
package com.example;
import java.util.Iterator;
/**
* API
* <p>
* Queue() 创建空队列<br>
* void enqueue(T item) 添加一个元素<br>
* T dequeue() 删除最早添加的元素<br>
* boolean isEmpty() 队列是否为空<br>
* int size() 队列中的元素数量<br>
* </p>
* Created by siyehua on 2016/12/3.
*/
public class MyQueue<T> implements Iterable<T> {
@Override
public Iterator<T> iterator() {
return new MyQueueAdapter();
}
private Node first;
private Node last;
private int number;
public boolean isEmpty() {
return number == 0;
}
public int size() {
return number;
}
public void enQueue(T item) {
Node oldFirst = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldFirst.next = last;
number++;
}
public T deQueue() {
T tmp = first.item;
first = first.next;
number--;
if (isEmpty()) {
last = null;
}
return tmp;
}
/**
* push item in front of the index.<br>
* the index start with <b>0</b>.
*
* @param index 0 ~ size()
* @param item item
*/
public void enQueue(int index, T item) {
if (index < 0 || index > size()) throw new RuntimeException("index must be 0 ~ size()");
if (index == size()) {
enQueue(item);
return;
}
Node node = new Node();
node.item = item;
number++;
Node tmp = first;
if (index == 0) {
node.next = first;
first = node;
return;
}
for (int i = 0; i < index - 1; i++) {
tmp = tmp.next;
}
node.next = tmp.next;
tmp.next = node;
}
/**
* delete the item
* the index start with <b>0</b>.
*
* @param k index: 0 ~ size()-1
* @return item
*/
public T delete(int k) {
if (k > size() - 1 || k < 0) throw new RuntimeException("k must be 0 ~ size()-1.");
number--;
T tmp;
Node node = first;
if (k == 0) {
tmp = first.item;
first = first.next;
return tmp;
}
for (int i = 0; i < k - 1; i++) {
node = node.next;
}
tmp = node.next.item;
if (k == size()) node.next = null;
else node.next = node.next.next;
return tmp;
}
private class Node {
T item;
Node next;
}
private class MyQueueAdapter implements Iterator<T> {
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
T tmp = current.item;
current = current.next;
return tmp;
}
@Override
public void remove() {
}
}
public static void main(String[] args) {
MyQueue<String> myQueue = new MyQueue<>();
for (int i = 0; i < 10; i++) {
myQueue.enQueue(i + 1 + "");
}
myQueue.enQueue(11,"new data");
// System.out.println("delete: " + myQueue.delete(10));
for (String str : myQueue) {
System.out.println(str);
}
}
}