基于链表实现队列

带尾节点的链表

我们之前实现的链表,只有链表头节点head。链表对中间元素的操作时间复杂度为O(n),因此要对链表的结构进行优化。需要多维护一个尾节点的变量(tail),对链表头元素的add 和remove操作都是O(1),链表尾元素add操作为O(1),而对链表尾元素remove操作时间复杂度为O(n)。结合队列FIFO的特点,决定在尾节点tail处添加元素,而在链表头head处删除元素,这样实现的队列添加删除操作的时间复杂度都为O(1)。

话不多说,上代码

在这里插入图片描述

/*
* 基于优化的链表(带tail尾结点)实现队列
* */
public class LinkedListQueue<E> implements Queue<E> {

    /*
    *定义一个内部类Node
    * */
    private class Node<E>{
        private E e;
        private Node next;
        public Node(E e, Node next){
            this.e=e;
            this.next=next;
        }
        public Node(E e){
            this(e,null);
        }
        public  Node(){
            this(null,null);
        }
        @Override
//        重写方法必须修饰权限必须比被覆盖的方法权限大
        public  String toString(){
            return e.toString();
        }
    }
    private Node head;
    private Node tail;
    private int size;
    public LinkedListQueue(){
        tail=null;
        head=null;
        size=0;

    }


    @Override
    public void enqueue(E element) {
        if(tail==null){
            tail=new Node(element,null);
            head=tail;
        }else{
            tail.next=new Node(element,null);
            tail=tail.next;
        }
        size++;
    }

    @Override
    public E dequeue() {
        if(isEmpty()){
            throw new IllegalArgumentException("队列为空,删除失败");
        }
        Node retNode=head;
        head=head.next;
//        将删除的节点从链表中删除
        retNode.next=null;
        if(head==null){
            tail=null;
        }
        size--;
        return (E) retNode.e;
    }

    @Override
    public E getFront() {
        if(isEmpty()){
            throw new IllegalArgumentException("队列为空");
        }
        return (E)tail.e;
    }

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

    @Override
    public boolean isEmpty() {
        return size==0;
    }
    @Override
    public String toString(){
        StringBuilder buffer=new StringBuilder();
        buffer.append("Queue: front ");
        Node cur=head;
        while(cur!=null){
            buffer.append(cur.e+"->");
            cur=cur.next;
        }
        buffer.append("Null tail");
        return buffer.toString();
    }

    public static void main(String[] args) {
        LinkedListQueue<Integer> linkedListQueue=new LinkedListQueue<>();
        for (int i = 0; i < 5; i++) {
            linkedListQueue.enqueue(i);
            System.out.println(linkedListQueue);
        }
        linkedListQueue.dequeue();
        System.out.println(linkedListQueue);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,你可以使用链表实现队列。下面是一个基于链表生成队列的示例代码: 首先,你需要定义一个节点类,用于表示链表的节点: ```java class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } ``` 然后,你可以创建一个队列类,包含队列的相关操作: ```java class Queue { private Node front; private Node rear; public Queue() { this.front = null; this.rear = null; } public boolean isEmpty() { return front == null; } public void enqueue(int data) { Node newNode = new Node(data); if (isEmpty()) { front = newNode; rear = newNode; } else { rear.next = newNode; rear = newNode; } System.out.println(data + " enqueued to the queue"); } public int dequeue() { if (isEmpty()) { throw new NoSuchElementException("Queue is empty"); } int data = front.data; front = front.next; if (front == null) { rear = null; } return data; } public int peek() { if (isEmpty()) { throw new NoSuchElementException("Queue is empty"); } return front.data; } } ``` 接下来,你可以使用该队列类来进行队列操作: ```java public class Main { public static void main(String[] args) { Queue queue = new Queue(); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); System.out.println("Front element: " + queue.peek()); System.out.println(queue.dequeue() + " dequeued from the queue"); System.out.println(queue.dequeue() + " dequeued from the queue"); System.out.println("Front element: " + queue.peek()); } } ``` 运行上述代码,你将得到以下输出: ``` 10 enqueued to the queue 20 enqueued to the queue 30 enqueued to the queue Front element: 10 10 dequeued from the queue 20 dequeued from the queue Front element: 30 ``` 这样,你就使用链表实现了一个基本的队列数据结构。注意,该示例使用了简化的整数数据类型,你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值