数据结构-队列-链队列(Java语言)

详细的代码可见github:

https://github.com/AbitGo/myClassWork/tree/master/workspace_ds

队列一般分为顺序队列以及链队列,本文主要讲述链队列。

所需要实现的接口功能。

package com.company.ch3.queue;
 
public interface IQueue {
    public void clear();
    public boolean isEmpty();
    public int length();
    public Object peek();
    public void offer(Object x);
    public Object poll();
}

链队列主要实现代码:

package com.company.ch3.queue;

import com.company.ch2.LinkTable.Node;

public class LinkSqeue implements IQueue {
    private Node front;
    private Node rear;

    public LinkSqeue() {
        this.front = this.rear = null;
    }

    @Override
    public void clear() {
        this.front = this.rear = null;
    }

    @Override
    public boolean isEmpty() {
        if (this.front == null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int length() {
        Node p = this.front;
        int len = 0;
        while (p != null) {
            len++;
            p = p.next;
        }
        return len;
    }

    //取队内首元素
    @Override
    public Object peek() {
        if (this.isEmpty() == true) {
            System.out.println("队列为空");
            return null;
        } else {
            return this.front.data;
        }
    }

    //入队
    @Override
    public void offer(Object x) {
        Node p = new Node(x);
        if(this.isEmpty()==true){
            this.front = this.rear =p;
        }else{
            this.rear.next = p;
            this.rear = p;
        }
    }

    //出队
    @Override
    public Object poll() {
        if(this.isEmpty()==true){
            System.out.println("队列为空");
            return null;
        }else{
            Object p = this.front.data;
            this.front = this.front.next;
            if(p==rear){
                this.rear = null;
                this.front = null;
            }
            return p;
        }
    }

    public void display(){
        Node p = this.front;
        while (p!=null){
            System.out.print(" "+p.data);
            p = p.next;
        }
        System.out.println();
    }
}

测试类:

package com.company.ch3.queue;

public class LinkSqeueTest {
    public static void main(String[] args){
        LinkSqeue sqStack = new LinkSqeue();
        System.out.println("----------插入操作:开始----------");
        sqStack.offer(1);
        sqStack.offer(2);
        sqStack.offer(3);
        System.out.println("----------插入操作:结束----------");
        sqStack.display();

        System.out.println("----------查看队列操作:开始----------");
        System.out.println(sqStack.peek());
        System.out.println("弹出队头元素:"+sqStack.poll());
        System.out.println(sqStack.peek());
        System.out.println("弹出队头元素:"+sqStack.poll());
        System.out.println("----------查看队列操作:结束----------");

        System.out.println("----------查看队列长度操作:开始----------");
        System.out.println("长度为:"+sqStack.length());
        System.out.println("----------查看队列长度操作:结束----------");

        System.out.println("----------清除队列操作:开始----------");
        sqStack.clear();
        System.out.println("----------清除队列操作:结束----------");
    }
}

测试结果:

----------插入操作:开始----------
----------插入操作:结束----------
 1 2 3
----------查看队列操作:开始----------
1
弹出队头元素:1
2
弹出队头元素:2
----------查看队列操作:结束----------
----------查看队列长度操作:开始----------
长度为:1
----------查看队列长度操作:结束----------
----------清除队列操作:开始----------
----------清除队列操作:结束----------
发布了84 篇原创文章 · 获赞 39 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Abit_Go/article/details/104137930