三、队列和栈的实现
1、队列实现MyQueue(先进先出):
- 链表实现队列:
public static class Node<V> {
public V value;
public Node<V> next;
public Node(V v) {
value = v;
next = null;
}
}
public static class MyQueue<V> {
private Node<V> head;
private Node<V> tail;
private int size;
public MyQueue() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void offer(V value) {
Node<V> node = new Node<V>(value);
if (tail == null) {
head = node;
tail = node;
} else {
tail.next = node;
tail = node;
}
size ++;
}
public V poll() {
V ans = null;
if (head != null) {
ans = head.value;
head = head.next;
size --;
}
if (head == null) {
tail = null;
}
return ans;
}
public V peek() {
V ans = null;
if (head != null) {
ans = head.value;
}
return ans;
}
}
- 数组实现队列:
public static class MyQueue {
private int[] arr;
private int pushi;
private int polli;
private int size;
private final int limit;
public MyQueue(int limit) {
arr = new int[limit];
pushi = 0;
polli = 0;
size = 0;
this.limit = limit;
}
public void push(int value) {
if (size == limit) {
throw new RuntimeException("队列满了,不能再添加了!");
}
arr[pushi] = value;
size++;
pushi = nextIndex(pushi);
}
public int pop() {
if (size == 0) {
throw new RuntimeException("队列为空,无数据!");
}
int value = arr[polli];
size--;
nextIndex(polli);
return value;
}
public boolean isEmpty() {
return size == 0;
}
private int nextIndex(int index) {
return index < limit -1 ? index+1 : 0;
}
}
2、栈MyStack的实现(先进后出):
- 链表:
public static class MyStack<V> {
private Node<V> head;
private int size;
public MyStack() {
head = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void push(V value) {
Node<V> node = new Node<>(value);
if (head == null) {
head = node;
}else {
node.next = head;
head = node;
}
size ++;
}
public V pop() {
V ans = null;
if (head != null) {
ans = head.value;
head = head.next;
size--;
}
return ans;
}
public V peek() {
V ans = null;
if (head != null) {
ans = head.value;
}
return ans;
}
}
- 数组:
public static class MyStack {
private int[] arr;
private int pushi;
private int polli;
private int size;
public final int limitStack;
public MyStack(int limit) {
arr = new int[limit];
pushi = 0;
polli = 0;
size = 0;
limitStack = limit;
}
public void push(int value) {
if (size < limitStack) {
arr[pushi] = value;
pushi =(pushi+1)%limitStack;
size ++;
} else {
throw new RuntimeException("栈满了!");
}
}
public int poll() {
if (size == 0) {
throw new RuntimeException("当前栈为空!");
}else {
int ans = arr[polli];
polli =(polli+1)%limitStack;
size--;
return ans;
}
}
public boolean isEmpty() {
return size==0;
}
}
双端链表实现双端队列Deque:
public static class Node<V> {
public V value;
public Node<V> last;
public Node<V> next;
public Node(V value) {
this.value = value;
last = null;
next = null;
}
}
public static class MyDeque<V> {
private Node<V> head;
private Node<V> tail;
private int size;
public MyDeque() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void pushHead(V value) {
Node<V> node = new Node<>(value);
if (head == null) {
head = node;
tail = node;
} else {
head.last = node;
node.next = head;
head = node;
}
size ++;
}
public void pushTail(V value) {
Node<V> node = new Node<>(value);
if (head == null) {
head = node;
tail = node;
} else {
tail.next = node;
node.last = tail;
tail = node;
}
size ++;
}
public V pollHead() {
V ans = null;
if (head == null) {
return null;
}
size --;
ans = head.value;
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
head.last = null;
}
return ans;
}
public V pollTail() {
V ans = null;
if (head == null) {
return ans;
}
size --;
ans = tail.value;
if (head == tail) {
head = null;
tail = null;
} else {
tail = tail.last;
tail.next = null;
}
return ans;
}
public V peekHead() {
V ans = null;
if (head != null) {
ans = head.value;
}
return ans;
}
public V peekTail() {
V ans = null;
if (head != null) {
ans = tail.value;
}
return ans;
}
}