用单链表实现队列和栈
需求:
用单链表结构实现队列结构
用单链表结构实现栈结构
队列:先进先出

栈:像弹夹一样,先压进去的后出。后压进去的先出。
队列的offer方法演示草图:

poll弹出,如果链表一个数也没有了,head为null,此时应该将tail也置为null。如下图所示,如果tail不置为null的话,5就是脏数据了。而且此时再添加节点也不对了。

单链表实现栈,只有一个变量,草图:

代码实现:
package com.cy.class04;
/**
* 用单链表实现队列和栈
* 时间复杂度O(1),因为不管是加入、弹出,都是有限的变量
*/
public class Code02_LinkedListToQueueAndStack {
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> cur = new Node<>(value);
if (tail == null) {
head = cur;
tail = cur;
} else {
tail.next = cur;
tail = cur;
}
size++;
}
/**
* 弹出
* 先进先出
*/
public V poll() {
V ans = null;
if (head != null) {
ans = head.value;
head = head.next;
size--;
}
//链表一个数也没有了,head为null,请tail和head保持一致
if (head == null) {
tail = null;
}
return ans;
}
/**
* 此时要弹出的节点是什么,但是不真的弹出
*/
public V peek() {
V ans = null;
if (head != null) {
ans = head.value;
}
return ans;
}
}
/**
* 单链表实现栈
*/
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> cur = new Node<>(value);
if (head == null) {
head = cur;
} else {
cur.next = head;
head = cur;
}
size++;
}
public V pop() {
V ans = null;
if (head != null) {
ans = head.value;
head = head.next;
size--;
}
return ans;
}
public V peek() {
return head == null ? head.value : null;
}
}
}
--
浙公网安备 33010602011771号