初级--04---链表反转----链表实现栈队列双端队列
Posted 高高for 循环
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初级--04---链表反转----链表实现栈队列双端队列相关的知识,希望对你有一定的参考价值。
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
链表反转
Node 节点
public static class Node
public int value;
public Node next;
public Node(int data)
value = data;
单链表反转
public static Node reverseLinkedList(Node head)
Node pre = null;
Node next = null;
while (head != null)
next = head.next;
head.next = pre;
pre = head;
head = next;
return pre;
双链表反转
public static DoubleNode reverseDoubleList(DoubleNode head)
DoubleNode pre = null;
DoubleNode next = null;
while (head != null)
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;
return pre;
单链表实现-----队列
线性表–07—队列
代码
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<V>(value);
if (tail == null)
head = cur;
tail = cur;
else
tail.next = cur;
tail = cur;
size++;
// C/C++的同学需要做节点析构的工作
public V poll()
V ans = null;
if (head != null)
ans = head.value;
head = head.next;
size--;
if (head == null)
tail = null;
return ans;
// C/C++的同学需要做节点析构的工作
public V peek()
V ans = null;
if (head != null)
ans = head.value;
return ans;
对数器
public static void testQueue()
MyQueue<Integer> myQueue = new MyQueue<>();
Queue<Integer> test = new LinkedList<>();
int testTime = 5000000;
int maxValue = 200000000;
System.out.println("测试开始!");
for (int i = 0; i < testTime; i++)
if (myQueue.isEmpty() != test.isEmpty())
System.out.println("Oops!");
if (myQueue.size() != test.size())
System.out.println("Oops!");
double decide = Math.random();
if (decide < 0.33)
int num = (int) (Math.random() * maxValue);
myQueue.offer(num);
test.offer(num);
else if (decide < 0.66)
if (!myQueue.isEmpty())
int num1 = myQueue.poll();
int num2 = test.poll();
if (num1 != num2)
System.out.println("Oops!");
else
if (!myQueue.isEmpty())
int num1 = myQueue.peek();
int num2 = test.peek();
if (num1 != num2)
System.out.println("Oops!");
if (myQueue.size() != test.size())
System.out.println("Oops!");
while (!myQueue.isEmpty())
int num1 = myQueue.poll();
int num2 = test.poll();
if (num1 != num2)
System.out.println("Oops!");
System.out.println("测试结束!");
单链表实现-----栈
线性表–05—栈
代码
public static class Node<V>
public V value;
public Node<V> next;
public Node(V v)
value = v;
next = null;
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;
对数器
public static void testStack()
MyStack<Integer> myStack = new MyStack<>();
Stack<Integer> test = new Stack<>();
int testTime = 5000000;
int maxValue = 200000000;
System.out.println("测试开始!");
for (int i = 0; i < testTime; i++)
if (myStack.isEmpty() != test.isEmpty())
System.out.println("Oops!");
if (myStack.size() != test.size())
System.out.println("Oops!");
double decide = Math.random();
if (decide < 0.33)
int num = (int) (Math.random() * maxValue);
myStack.push(num);
test.push(num);
else if (decide < 0.66)
if (!myStack.isEmpty())
int num1 = myStack.pop();
int num2 = test.pop();
if (num1 != num2)
System.out.println("Oops!");
else
if (!myStack.isEmpty())
int num1 = myStack.peek();
int num2 = test.peek();
if (num1 != num2)
System.out.println("Oops!");
if (myStack.size() != test.size())
System.out.println("Oops!");
while (!myStack.isEmpty())
int num1 = myStack.pop();
int num2 = test.pop();
if (num1 != num2)
System.out.println("Oops!");
System.out.println("测试结束!");
双端队列
代码
public static class Node<V>
public V value;
public Node<V> last;
public Node<V> next;
public Node(V v)
value = v;
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> cur = new Node<>(value);
if (head == null)
head = cur;
tail = cur;
else
cur.next = head;
head.last = cur;
head = cur;
size++;
public void pushTail(V value)
Node<V> cur = new Node<>(value);
if (head == null)
head = cur;
tail = cur;
else
tail.next = cur;
cur.last = tail;
tail = cur;
size++;
public V pollHead()
V ans = null;
if (head == null)
return ans;
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 (tail != null)
ans = tail.value;
return ans;
对数器
public static void 以上是关于初级--04---链表反转----链表实现栈队列双端队列的主要内容,如果未能解决你的问题,请参考以下文章
算法初级面试题03——队列实现栈栈实现队列转圈打印矩阵旋转矩阵反转链表之字打印矩阵排序矩阵中找数
Java算法 -- 单链表的反转单链表实现栈和队列以及双端队列K 个一组翻转链表
Java算法 -- 单链表的反转单链表实现栈和队列以及双端队列K 个一组翻转链表
Java算法 -- 单链表的反转单链表实现栈和队列以及双端队列K 个一组翻转链表