【Data Structures】 6. Queue—Another limited data structure and FIFO
来源:互联网 发布:网络音乐在线听歌曲 编辑:程序博客网 时间:2024/06/03 11:21
A queue is a container of objects that are inserted and removed based on FIFO (First-in First-out) principle.
In the queue, there are two major operations, enqueue and dequeue.
1. Enqueue means inserting a new item to the back of the queue.
2. Dequeue means removing the first item from the queue.
Just like Stack, Queue is also a data structure that is built on top of other data structures (array, ArrayList, or LinkedList). No matter what underlying structure it uses, we want to make sure that a queue implements the same following functionalities.
public interface QueueInterface<AnyType> { void enqueue(AnyType item); // O(1) AnyType dequeue(); // O(1) AnyType peekFront(); // O(1) boolean isEmpty(); // O(1)}
Array-based implementation
We need some fields:
1. An "array elements" which has a fixed length.
2. Variable "back" that refers to the back of the queue.
3. Variable "front" that refers to the front of the queue.
4. Additionally, some others such as "nitems" variable to keep track of current number of items.
Every time a new item is added (enqueued), the "back" index should be increased.
And, when the front item is removed (dequeued), the "front" index should also be increased.
Circular Queue
When we need to do is to have "front" and "back" variables wrap around the array. The result is the circular queue.
Skeleton: ArrayQueue class
public class ArrayQueue<AnyType> implements QueueInterface<AnyType> { private static final int DEFAULT_CAPACITY = 6; private Object[] elements; private int front; // front index private int back; // back index private int nItems; // current number of items public ArrayQueue() { elements = new Object[DEFAULT_CAPACITY]; front = 0; back = -1; nItems = 0; } // TODO Implements all of the core methods here}
Enqueue
// Inserts a new item into the back of the queue@Overridepublic void enqueue(AnyType item) { if (nItems == elements.length) { throw new RuntimeException("Queue is full"); } back++; int index = back % elements.length; elements[index] = item; nItems++;}
Dequeue
// Returns and removes the item at the front@Overridepublic AnyType dequeue() { if (isEmpty()) { throw new NoSuchElementException(); } int index = front % elements.length; AnyType result = (AnyType)elements[index]; elements[index] = null; // remove it front++; nItems--; // one less in the queue return result;}
Peek Front
// Returns the first item in the queue without removing it@Overridepublic AnyType peekFront() { if (isEmpty()) { throw new NoSuchElementException(); } return (AnyType)elements[front % elements.length];}
isEmpty
@Overridepublic boolean isEmpty() { return nItems == 0;}
LinkedListQueue
LinkedList<Integer> theQueue = new LinkedList<Integer>();// enqueue into the queuetheQueue.addLast(5);// dequeue from the queuetheQueue.removeFirst();
Also there are other methods in LinkedList and ArrayDequeue such as offer(E e), pollFirst(), and pollLast(), etc.
- 【Data Structures】 6. Queue—Another limited data structure and FIFO
- 【Data Structures】 5. Stack—A limited data structure and LIFO
- 队列(Queue)——先进先出(FIFO)的数据结构(Data Structures)
- Stack & Queue--Data Structure
- Priority Queue--Data Structure
- Data Structures and Algorithms
- Data Structures and Algorithms
- Objects and Data Structures
- •data structures using c: 1.1: structure: data and program-(2013/11/20)
- algorithm and data structure
- algorithm and data structure
- Data Structure and Algorithm
- JNI Types and Data Structures
- JNI Types and Data Structures
- Data Structures (Weiss) Chapter 6: Binomial Queue
- android data structure And algorithm
- Algorithm and Data Structure Review
- [Data Structure and Algorithm]--Heap
- entity framework 事务
- perfect素数3274
- linux—套接字
- 【Python】初识Python的学习笔记
- 微信小应用第一天(简介)
- 【Data Structures】 6. Queue—Another limited data structure and FIFO
- Objective-C Runtime笔记(官方Doc翻译+原创)
- web服务器集群
- 面试问题
- 移动端布局
- win10安装python3.x+scrapy
- java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制)
- hdu1.3.2 Rank
- java trick--intergerCache