简单数据结构实现——队列
2013-02-07 15:31
726 查看
队列 (queue) ,基本数据结构之一,属于FIFO(先进先出)数据结构。队列只允许在前端 (HEAD) 进行入队(插入)操作,只允许在后端 (TAIL) 进行出队(删除)操作。
单队列可以使用链表来实现,实现非常简单,队列的长度也没有限制,但是插入和读取的时间代价较高。
循环队列可以使用数组来实现,因此操作起来很快,但是长度一般固定。但是可以通过设计一个扩大容量的方法来使其长度可变。
在后面给出的代码是基于数组的循环队列实现。
另外,有一种所谓的优先队列,实际上是依靠堆的结构来实现的,在以后练习到堆的时候会再提及。
简述循环队列实现原理:利用两个下标来确定队列范围:head 与 tail. 队列的默认构造器将使构造长度为DEFAULT_CAPACITY=10的队列,带整数参数的构造器将构造相应长度的队列。
队列另外有两个属性size和capacity,size表明队列中的元素个数,capacity表明当前队列的容量(可容纳元素个数)。
每当有元素入队时,size加一,每当有元素退队时,size减一。
因此可以简单通过size==0和size==capacity来判断isEmpty和isFull.
入队操作:当队列不为满的时候,在下标为tail的位置插入元素,自增size,调整tail的值: if(++tail==queue.length){tail=0;}
退队操作:当队列不空的时候,记录头元素的值,自减size,调整head的值: if(++head==queue.length){head=0;},返回头元素的值。
扩容方法 enlarge():当调用该方法时,将capacity*=2,容量增加一倍,然后创建新的数组,再将旧数组中的值复制到新数组中,在新数组中的位置将从0位置开始向后排列,具体实现看代码。
java代码:
单队列可以使用链表来实现,实现非常简单,队列的长度也没有限制,但是插入和读取的时间代价较高。
循环队列可以使用数组来实现,因此操作起来很快,但是长度一般固定。但是可以通过设计一个扩大容量的方法来使其长度可变。
在后面给出的代码是基于数组的循环队列实现。
另外,有一种所谓的优先队列,实际上是依靠堆的结构来实现的,在以后练习到堆的时候会再提及。
简述循环队列实现原理:利用两个下标来确定队列范围:head 与 tail. 队列的默认构造器将使构造长度为DEFAULT_CAPACITY=10的队列,带整数参数的构造器将构造相应长度的队列。
队列另外有两个属性size和capacity,size表明队列中的元素个数,capacity表明当前队列的容量(可容纳元素个数)。
每当有元素入队时,size加一,每当有元素退队时,size减一。
因此可以简单通过size==0和size==capacity来判断isEmpty和isFull.
入队操作:当队列不为满的时候,在下标为tail的位置插入元素,自增size,调整tail的值: if(++tail==queue.length){tail=0;}
退队操作:当队列不空的时候,记录头元素的值,自减size,调整head的值: if(++head==queue.length){head=0;},返回头元素的值。
扩容方法 enlarge():当调用该方法时,将capacity*=2,容量增加一倍,然后创建新的数组,再将旧数组中的值复制到新数组中,在新数组中的位置将从0位置开始向后排列,具体实现看代码。
java代码:
public class MyQueue{ public int size=0,head=0,tail=0; private final int DEFAULT_CAPACITY=10; private int capacity; private int[] queue; public MyQueue(){ queue=new int[DEFAULT_CAPACITY]; capacity=DEFAULT_CAPACITY; } public MyQueue(int capacity){ queue=new int[capacity]; this.capacity=capacity; } public void enqueue(int element){ if(!isFull()){ queue[tail]=element; size++; if(++tail==queue.length){ tail=0; } } else{ System.out.println("Queue is full, use enlarge() method to enlarge."); } } public int dequeue(){ if(!isEmpty()){ int x=queue[head]; size--; if(++head==queue.length){ head=0; } return x; } else{ System.out.println("Queue is empty, cannot dequeue!"); return Integer.MIN_VALUE; } } public int size(){ return size; } public boolean isEmpty(){ return size==0; } public boolean isFull(){ return size==capacity; } public void enlarge(){ capacity*=2; int[] newQueue=new int[capacity]; int count=0,index=head; while(++count<=size){ newQueue[count]=queue[index]; count++; if(++index==queue.length){ index=0; } } queue=newQueue; head=0; tail=size; } public String toString(){ String str=""; if(isEmpty()) return "This is an empty stack"; else{ str+="["; int index=head,count=0; while(++count<=size()){ str+=" "+queue[index]; if(++index==queue.length){ index=0; } } str+=" ]"; } return str; } public int getCapacity(){ return capacity; } }
相关文章推荐
- (三)数据结构之线性表的简单实现:队列
- 数据结构之静态队列的java简单实现
- 数据结构7:队列的简单实现
- 【数据结构与算法】(四) c 语言静态队列的简单实现
- 数据结构(Java语言)——BinarySearchTree简单实现
- 数据结构之C/C++实现循环队列
- 用C语言模块实现队列数据结构
- 数据结构(Java语言)——HashTable(分离链接法)简单实现
- 数据结构之数组实现基础队列结构
- 利用内存chunk充当数据buffer的泛型的双向队列的简单实现
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- java通过LinkedList实现堆栈和队列数据结构
- 【数据结构】顺序队列(链表实现)
- 栈与队列-顺序队列与链队列类模板的实现(数据结构基础 第3周)
- 【数据结构】顺序队列 如何实现栈增长
- 数据结构之动态栈的java简单实现
- 简单数据结构实现——二叉查找树
- 数据结构之C/C++实现链式队列
- 优先队列的实现 Java数据结构与算法
- 数据实验结构(二)——栈和队列的设计与实现