java实现链表集合并使用链表实现队列
Posted 今夜月色很美
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现链表集合并使用链表实现队列相关的知识,希望对你有一定的参考价值。
1、自定义链表集合
package com.study.datastructure.linked;
/**
* @author lyz
* @Title: SingleLinked
* @Description:
* @date 2021/3/4 9:36
*/
public class SingleLinked<E> {
private int max;
private int count = 0;
private Node<E> head;
private Node<E> last;
public SingleLinked(int max) {
this.max = max;
head = last = new Node<>(null);
}
/**
* 从链表尾部添加Node
* @param e
* @return
*/
public boolean add(E e){
if (isFull()){
System.out.println("链表已满");
return false;
}
Node<E> newNode = new Node<>(e);
newNode.next = null;
last = last.next = newNode;
count++;
return true;
}
/**
* 从链表头部添加Node
* @param e
* @return
*/
public boolean addHead(E e){
if (isFull()){
System.out.println("链表已满");
return false;
}
Node<E> newNode = new Node<>(e);
newNode.next = this.head.next;
this.head.next = newNode;
count++;
return true;
}
/**
* 从尾部删除Node
* @return
*/
public E removeLast(){
Node<E> cur = head.next;
if (cur == null){
System.out.println("队列是空的");
return null;
}
if (cur.next == null){
head.next = null;
return cur.item;
}
Node<E> tmp = null;
while (true){
if (cur.next.next == null){
tmp = cur.next;
cur.next = null;
break;
}
cur = cur.next;
}
return tmp.item;
}
/**
* 从头部删除Node
* @return
*/
public E removeHead(){
if (isEmpty()){
System.out.println("队列是空的");
return null;
}
Node<E> cur = this.head.next;
E item = cur.item;
Node<E> tmp = cur.next;
this.head.next = tmp;
count--;
return item;
}
/**
* 反转,返回一个新的链表
*/
public SingleLinked reverse(){
if (isEmpty()){
System.out.println("队列是空的");
return this;
}
if (count == 1){
System.out.println("队列只有一个元素,直接返回");
return this;
}
Node<E> cur = this.head.next;
SingleLinked newLinked = new SingleLinked(this.count);
while (cur != null){
newLinked.addHead(cur.item);
cur = cur.next;
}
return newLinked;
}
/**
* 遍历所有Node
*/
public void showAll(){
Node<E> cur = head.next;
while (true){
if (cur == null){
break;
}
System.out.println(cur.item);
cur = cur.next;
}
}
/**
* 链表有没有满
* @return
*/
private boolean isFull() {
return count == max;
}
/**
* 链表是否为空
* @return
*/
private boolean isEmpty() {
return count == 0;
}
static class Node<E>{
private E item;
private Node<E> next;
public Node(E item) {
this.item = item;
}
}
}
2、使用链表实现队列
package com.study.datastructure.queue;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author lyz
* @Title: LinkedQueueDemo
* @Description:
* @date 2021/3/2 16:00
*/
public class SingleLinkedQueue<E> {
private int capacity;
private AtomicInteger count = new AtomicInteger();
private Node<E> head;
private Node<E> last;
public SingleLinkedQueue(int size) {
capacity = size;
head = last = new Node(null);
}
public boolean offer(E e){
if (isFull()){
System.out.println("队列已满。。。");
return false;
}
Node<E> node = new Node<E>(e);
last = last.next = node;
count.incrementAndGet();
return true;
}
public E poll(){
if (isEmpty()){
System.out.println("队列是空的。。。");
return null;
}
Node temp = head;
Node first = temp.next;
temp.next = temp;
head = first;
E e = (E) first.item;
first.item = null;
count.decrementAndGet();
return e;
}
private boolean isFull() {
return count.get() == capacity;
}
private boolean isEmpty(){
return count.get() == 0;
}
static class Node<E>{
E item;
Node<E> next;
public Node(E item) {
this.item = item;
}
}
}
以上是关于java实现链表集合并使用链表实现队列的主要内容,如果未能解决你的问题,请参考以下文章
数据结构 ---[链表 ] [使用链表实现栈 以及 队列 (Java代码实现)]
[DataStructure]线性数据结构之稀疏数组链表栈和队列 Java 代码实现