链表实现队列LinkedQueue


/**
 * 使用链表实现队列
 * @param <E>
 */
public class LinkedQueue<E> implements Queue<E> {

    public static void main(String[] a) {

        LinkedQueue<Integer> list = new LinkedQueue<>();


        for (int i = 0; i < 5; i++) {
            list.enqueue(i);
            System.out.println(list);
        }

        list.dequeue();
        System.out.println(list);



    }
    //队首对象,队尾对象
    Node head, tail;
    int size;


    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    //入队
    @Override
    public void enqueue(E o) {
        //队尾为null,整个链表为null
        if (tail == null) {
            tail = new Node(o);
            head = tail;
        } else {
            tail.next = new Node(o);
            tail = tail.next;

        }
        size++;

    }

    //出队(移除队首)
    @Override
    public E dequeue() {
        if (isEmpty())
            throw new IllegalArgumentException("list is null");

        Node ret = head;
        head = head.next;
        ret.next = null;
        if (head == null) {
            tail = null;
        }
        size--;
        return ret.e;
    }

    //获取队首
    @Override
    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("list is null");
        return head.e;
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append("LinkedQueue ");
        ret.append("[");
        Node pre = head;
        for (int i = 0; i < size; i++) {

            ret.append("<---"+pre.e);
            pre = pre.next;

        }
        ret.append("]");
        return ret.toString();
    }

    class Node {
        E e;
        Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this.e = e;
            this.next = null;
        }

        public Node() {
            this.e = null;
            this.next = null;
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }
}

是的,队列算法可以用链表实现,这种实现方式被称为链式队列Linked Queue)。 链式队列使用链表来存储元素,不需要预先指定队列的容量,因此可以动态地增加或减少元素。链式队列有两个指针,一个指向队头,一个指向队尾,插入和删除元素只需要更新这两个指针即可。 下面是使用Java实现链式队列的算法代码: ```java public class LinkedQueue { private Node front; // 队头指针 private Node rear; // 队尾指针 // 节点类 private class Node { private int data; private Node next; public Node(int data) { this.data = data; } } // 构造方法,初始化队列 public LinkedQueue() { front = null; rear = null; } // 插入元素,从队尾插入 public void insert(int value) { Node newNode = new Node(value); if (rear == null) { front = newNode; rear = newNode; } else { rear.next = newNode; rear = newNode; } } // 删除元素,从队头删除 public int remove() { if (front == null) { throw new NoSuchElementException(); } int value = front.data; front = front.next; if (front == null) { rear = null; } return value; } // 判断队列是否为空 public boolean isEmpty() { return (front == null); } // 返回队列大小 public int size() { int count = 0; Node current = front; while (current != null) { count++; current = current.next; } return count; } } ``` 此代码实现了一个简单的链式队列,包括插入、删除、判断队列是否为空和返回队列大小等基本操作。需要注意的是,链式队列插入和删除元素只需要更新队尾指针和队头指针即可,不需要像数组实现那样移动元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值