Data Structures and Algorithms (IX) chains queue - a queue of Storage Structure

Chain queue


Storage Structure queue, in fact, single-chain linear form, but it can only be the end of it into the head out of it, we call it queue chain.

Since the chain queues and a single list, as observed queue LinkedQueue chain and singly-linked list LinkedList UML class diagram, you can see the relationship between the two is polymerized, also said LinkedQueue equivalent LinkedList member variables, and before we say the chain the stack is the same
Here Insert Picture Description
being the case, the object is to create a list LinkedList realized, call the list of methods can, of course, there LinkedQueue implement Queue Interface

package DS02.动态链表;

import DS01.动态数组.Queue;

import java.util.Iterator;

public class LinkedQueue<E> implements Queue<E> {
    //声明并创建单链表对象
    private LinkedList<E> list;
    public LinkedQueue() {
        list = new LinkedList<>();
    }


    @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();
    }
}

Comparative circular queues and queues chain

For comparison circular queues and queues chain, can be considered in two ways:

  • From the time, in fact, their basic operations are time constants, the time complexity is O (1), but the application is to achieve good circular queue space, is not released during use, and a queue chain for each application and release node also there will also be spending some time, if a team into the team frequently, then there are two subtle differences.
  • For the space, a circular queue must have a fixed length, so there is a storage element and the number of wasted space problem, but this problem does not exist the queue chain, although it requires a pointer field, some overhead in the space but it is also acceptable, so the chain of queue space more flexible.

In general, in the case where the maximum queue length may be determined it recommended circular queue, if not the queue length estimate, use the queue chain.

Published 70 original articles · won praise 56 · views 1981

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/103616754
Recommended