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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值