数据结构——队列
2018-02-12 19:58
169 查看
队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表。
(1)允许删除的一端称为队头(Front)。
(2)允许插入的一端称为队尾(Rear)。
(3)当队列中没有元素时称为空队列。
(4)队列亦称作先进先出(First In First Out)的线性表,简称为FIFO表。
在Java编程中,Queue的实现都是用LinkedList
顺序队列
链式队列
(1)允许删除的一端称为队头(Front)。
(2)允许插入的一端称为队尾(Rear)。
(3)当队列中没有元素时称为空队列。
(4)队列亦称作先进先出(First In First Out)的线性表,简称为FIFO表。
在Java编程中,Queue的实现都是用LinkedList
顺序队列
package com.demo.quee; public class SeqQuee { private int top; private int rear; private int capacity = 10; private int[] array; private int size; public SeqQuee(int capacity){ top = rear = 0; array = new int[capacity]; } public SeqQuee(){ array = new int[this.capacity]; } public int size(){ return size; } public boolean isEmpty(){ return this.top == -1; } public void push(int data){ if(array.length == size){ addCapacity(size*2); } array[++rear] = data; size++; } public int pop(){ if(isEmpty()){ return -1; } size--; array[top] = (Integer) null; return array[top--]; } public void addCapacity(int capacity){ if(capacity < size){ return; } int[] old = array; array = new int[capacity]; for(int i=0; i<size; i++){ array[i] = old[i]; } } }
链式队列
package com.demo.quee; import com.demo.node.Node; public class LinkedQuee { private Node top; private Node rear; private int size; public LinkedQuee(){ this.top = null; } public int size(){ return this.size; } public void push(int data) throws Exception{ if(this.top == null){ this.top = new Node(data); }else{ Node p = new Node(data,this.top); rear.next = p; rear = p; } size++; } public int pop(){ Node p = top.next; int data = p.data; top.next = p.next; p = null; size--; return data; } }
相关文章推荐
- 数据结构——栈与队列
- 数据结构实验之队列一:排队买饭
- 基础数据结构之栈和队列
- SDUT_2449_数据结构实验之栈与队列十:走迷宫
- 数据结构基础——循环队列的C++实现
- 数据结构之队列
- 数据结构教程第四版*(李春葆)P81~82//队列的顺序存储结构以及实现
- 数据结构(队列):队列的链式存储结构
- 数据结构之队列(数组队列)
- 第六周--数据结构之自建算法库之迷宫问题(用队列)
- 数据结构—队列
- 数据结构之队列――顺序存储结构(php代码实现――方法一)
- 数据结构——队列
- 【2135】数据结构实验之队列一:排队买饭 sdutOJ
- 数据结构——队列的使用(二)
- 数据结构实验之队列一:排队买饭
- 数据结构——队列篇
- Linked List实现队列的数据存储结构
- 数据结构实验之队列一:排队买饭
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果