Data Structures and Algorithms (V) queue - sequential storage structure

queue


Queue definition of : inserting operation is only allowed at one end and at the other end of the linear table delete operation.

For example, like Mobile, China Unicom, telecommunications and other customer service calls, customer service staff and customers are always a few in comparison, in the case of so customer service staff are busy, customers were asked to wait until a customer service staff and idle, waiting to make the first connect customers to phone, so there will be current customer customer service phone call queuing process. Customer service system is the application of such a queue data structure.

FIFO queue is a linear table that allows the insertion end is called the tail, allowed to delete called queue head end.
Here Insert Picture Description
First define the interface queue Queue, write ArrayQueue realize the benefits of this interface and define the interface of Queue, after longer need to write another queue, just let them implement this interface, you can override the abstract methods interface.
Then try to write Queue, Queue not so much abstract methods List, has sentenced empty, obtain a valid number of elements, into the team, the team, get first team to get the tail, several abstract methods to empty the queue.

public interface Queue<E> extends Iterable {
    //获取队列中元素的个数
    int getSize();

    //判断队列是否为空
    boolean isEmpty();

    //入队一个元素
    void enqueue(E e);

    //出队一个元素
    E dequeue();

    //获取队头
    E getFront();

    //获取队尾
    E getRear();

    //清空队列
    void clear();

}

Here extends Iterable to implement iterators, let ArrayQueue implement the interface, written before the linear form, we say that the interface only allows insertion at one end and at the other end of the linear table delete operation, but also the nature of the linear form, linear table object can be created in ArrayQueue, the direct use of the linear form.

import java.util.Iterator;
/*
  队列的顺序存储结构
*/
public class ArrayQueue<E> implements Queue<E> {
    //定义线性表对象list
    private ArrayList<E> list;
    //构造函数
    public ArrayQueue(){
        list=new ArrayList<>(); //创建list对象
    }
    //获取队列有效元素,用线性表的方法
    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void enqueue(E e) {
        list.addLast(e);
    }

    @Override
    public E dequeue() {
        return list.removeFirst();
    }

    @Override
    public E getFront() {
        return list.getFirst();
    }

    @Override
    public E getRear() {
        return list.getLast();
    }

    @Override
    public void clear() {
        list.clear();
    }

    @Override
    public Iterator<E> iterator() {
        return list.iterator();
    }
	//以数组的格式返回,方便测试
    @Override
    public String toString() {
        StringBuilder sb=new StringBuilder();
        sb.append(String.format("ArrayQueue: %d/%d\n",getSize(),list.getCapacity()));
        sb.append('[');
        if(isEmpty()){
            sb.append(']');
        }else{
            for(int i=0;i<list.getSize();i++){
                sb.append(list.get(i));
                if(i==list.getSize()-1){
                    sb.append(']');
                }else{
                    sb.append(',');
                }
            }
        }
        return sb.toString();
    }
}

Published 70 original articles · won praise 56 · views 1985

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/103569962