一:双向链表实现
双向链表实现可以从头部进,头部出,以及尾部进,尾部出的结构
public static class Node<T> {
public T value;
public Node<T> last;
public Node<T> next;
public Node(T value) {
this.value = value;
}
}
public static class DoubleEndsQueue<T> {
public Node<T> head;
public Node<T> tail;
public void addFromHead(T value) {
Node<T> cur = new Node<T>(value);
if (head == null){
head = cur;
tail = cur;
}else {
cur.next = head;
head.last = cur;
head = cur;
}
}
public void addFromBottom(T value) {
Node<T> cur = new Node<T>(value);
if (head == null){
head = cur;
tail = cur;
}else {
cur.last = tail;
tail.next = cur;
tail = cur;
}
}
public T popFromHead(T value) {
if (head == null){
return null;
}
Node<T> cur = head;
if (head == tail){
head = null;
tail = null;
}else {
head = head.next;
cur.next = null;
head.last = null;
}
return cur.value;
}
public T popFromBottom(T value) {
if (head == null){
return null;
}
Node<T> cur = tail;
if (head == tail){
head = null;
tail = null;
}else {
tail = tail.last;
cur.last = null;
tail.next = null;
}
return cur.value;
}
public boolean isEmpty() {
return head == null;
}
}
1.栈
要求只能从头部进、头部出
public static class MyStack<T> {
private DoubleEndsQueue<T> queue;
public MyStack(){
queue = new DoubleEndsQueue<T>();
}
public void push(T value){
queue.addFromHead(value);
}
public T pop(){
return queue.popFromHead();
}
public boolean isEmpty(){
return queue.isEmpty();
}
}
测试
MyStack<Integer> myStack = new MyStack<>();
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
while (!myStack.isEmpty()){
System.out.println(myStack.pop());
}
2.队列
要求只能从头部进、尾部出
public static class MyQueue<T> {
private DoubleEndsQueue<T> queue;
public MyQueue(){
queue = new DoubleEndsQueue<T>();
}
public void push(T value){
queue.addFromHead(value);
}
public T pop(){
return queue.popFromBottom();
}
public boolean isEmpty() {
return queue.isEmpty();
}
}
测试
MyQueue<Integer> myQueue = new MyQueue<>();
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
myQueue.push(4);
while (!myQueue.isEmpty()){
System.out.println(myQueue.pop());
}
二:数组实现
1.栈
使用数组配合一个指针来完成
public class StackArray {
public static class MyStack{
private int[] arr;
private int cur;
private int size;
private final int limit;
public MyStack(int limit) {
arr = new int[limit];
cur = 0;
this.limit = limit;
}
public void push(int value){
if (cur == limit){
System.out.println("栈已满!!");
}
arr[cur] = value;
cur++;
}
public int pop(){
if (cur == 0){
System.out.println("栈已空!!");
}
cur--;
return arr[cur];
}
public boolean isEmpty(){
return cur == 0;
}
}
}
2.队列
设计一个循环队列
public class RingArray {
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){
System.out.println("队列已满!");
}
size++;
arr[pushi] = value;
pushi = nextIndex(pushi);
}
public int pop(){
if (size == 0){
System.out.println("队列已空!");
}
size--;
int x = arr[polli];
polli = nextIndex(polli);
return x;
}
public boolean isEmpty(){
return size == 0;
}
private int nextIndex(int i) {
return i < limit - 1 ? i + 1 : 0;
}
}
}