forked from shinezejian/javaStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
150 lines (134 loc) · 3.76 KB
/
Copy pathLinkedQueue.java
File metadata and controls
150 lines (134 loc) · 3.76 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
package com.zejian.structures.Queue;
import com.zejian.structures.LinkedList.singleLinked.Node;
import java.io.Serializable;
import java.util.*;
/**
* Created by zejian on 2016/11/28.
* Blog : http://blog.csdn.net/javazejian/article/details/53375004 [原文地址,请尊重原创]
* 链式队列的实现
*/
public class LinkedQueue<T> implements Queue<T> ,Serializable{
private static final long serialVersionUID = 1406881264853111039L;
/**
* 指向队头和队尾的结点
* front==null&&rear==null时,队列为空
*/
private Node<T> front,rear;
private int size;
/**
* 用于控制最大容量,默认128,offer方法使用
*/
private int maxSize=128;
public LinkedQueue(){
//初始化队列
this.front=this.rear=null;
}
@Override
public int size() {
return size;
}
public void setMaxSize(int maxSize){
this.maxSize=maxSize;
}
@Override
public boolean isEmpty() {
return front==null&&rear==null;
}
/**
* data 入队,添加成功返回true,否则返回false,可扩容
* @param data
* @return
*/
@Override
public boolean add(T data) {
Node<T> q=new Node<>(data,null);
if (this.front==null) {//空队列插入
this.front = q;
} else {//非空队列,尾部插入
this.rear.next=q;
}
this.rear=q;
size++;
return true;
}
/**
* offer 方法可插入一个元素,这与add 方法不同,
* 该方法只能通过抛出未经检查的异常使添加元素失败。
* 而不是出现异常的情况,例如在容量固定(有界)的队列中
* NullPointerException:data==null时抛出
* IllegalArgumentException:队满,使用该方法可以使Queue的容量固定
* @param data
* @return
*/
@Override
public boolean offer(T data) {
if (data==null)
throw new NullPointerException("The data can\'t be null");
if (size>=maxSize)
throw new IllegalArgumentException("The capacity of LinkedQueue has reached its maxSize:128");
Node<T> q=new Node<>(data,null);
if (this.front==null) {//空队列插入
this.front = q;
} else {//非空队列,尾部插入
this.rear.next=q;
}
this.rear=q;
size++;
return false;
}
/**
* 返回队头元素,不执行删除操作,若队列为空,返回null
* @return
*/
@Override
public T peek() {
return this.isEmpty()? null:this.front.data;
}
/**
* 返回队头元素,不执行删除操作,若队列为空,抛出异常:NoSuchElementException
* @return
*/
@Override
public T element() {
if(isEmpty()){
throw new NoSuchElementException("The LinkedQueue is empty");
}
return this.front.data;
}
/**
* 出队,执行删除操作,返回队头元素,若队列为空,返回null
* @return
*/
@Override
public T poll() {
if (this.isEmpty())
return null;
T x=this.front.data;
this.front=this.front.next;
if (this.front==null)
this.rear=null;
size--;
return x;
}
/**
* 出队,执行删除操作,若队列为空,抛出异常:NoSuchElementException
* @return
*/
@Override
public T remove() {
if (isEmpty()){
throw new NoSuchElementException("The LinkedQueue is empty");
}
T x=this.front.data;
this.front=this.front.next;
if (this.front==null)
this.rear=null;
size--;
return x;
}
@Override
public void clearQueue() {
this.front= this.rear=null;
size=0;
}
}