contents n n 6 1 The abstract data
contents n n 6. 1 The abstract data type 6. 2 formula-based representation 6. 3 linked representation 6. 4 applications 山东大学计算机科学与技术学院 数据结构 第 6章 队列 2
Bird’s eye view n A queue is a special kind of linear list, i. e. , insertion and deletions take place from different end, which is a first-in-first-out (FIFO) list. Although queue classes may be easily derived from the linear list class, for the run time efficiency, we only discuss it as a new linear list class. 山东大学计算机科学与技术学院 数据结构 第 6章 队列
Dynamically change A B C D E ↑ ↑ front rear front ↑ rear ↑ ↑ front rear delete l队列是一个先进先出( 山东大学计算机科学与技术学院 front rear insert ↑ C D E first-in-first-out, FIFO)的线性表。 数据结构 第 6章 队列 5
Abstract data type abstract. Data. Type Queue { n instance ordered list of elements,one end is called front,the another is the rear; operations; Create (): 创建一个空的队列; Is. Empty (): 如果队列为空,则返回true,否则返回false; Is. Full ( ) : 如果队列满,则返回true;否则返回false; First (): 返回队列的第一个元素; Last ( ) : 返回队列的最后一个元素; Add (x): 向队列中添加元素x; // enqueue Delete (x): 删除队首元素,并送入x ; // dequeue } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 6
6. 2 Formula-based representation queue 0 1 2 e 1 e 2 e 3 Max. Size-1 …… front n n n en rear formula: (using one dimension array) location(i) = i – 1 第一个元素: queue[0], 第二个元素 : queue[1] …… front总是为 0 , rear始终是最后一个元素的位置 山东大学计算机科学与技术学院 数据结构 第 6章 队列 7
公式化描述 queue 0 1 2 e 1 e 2 e 3 Max. Size-1 …… front n n n en rear 队列的长度: rear + 1 空队列 : rear=-1 Add(x): rear=rear+1; queue[rear]=x; O(1) Delete(x): x=queue[0]; queue[0. . rear-1]← queue[1. . rear]; rear=rear-1; n Q(n) 山东大学计算机科学与技术学院 数据结构 第 6章 队列 8
Front point is moving n 公式: location(i) = location(1) + i – 1 n n n front = location(1), rear = location(last element) 空队列 : rear < front Delete(x): x=queue(front) ; front++; Q(1) Add(x): when rear < Maxsize – 1: rear=rear+1; queue[rear]=x; Q(1) when rear = Maxsize-1 and front > 0 ? 山东大学计算机科学与技术学院 数据结构 第 6章 队列 9
Shifting a queue when rear meet end • when rear = Maxsize-1 and front > 0 队列的移位之前 n 队列的移位之后 平移队列 n n n Add(x)? queue[0. . rear-front+1]← queue[front. . rear]; rear=rear+1; queue[rear]=x; 时间复杂性 : Q(n) 山东大学计算机科学与技术学院 数据结构 第 6章 队列 10
Circular queue (循环存放) queue 0 1 2 a front n Max. Size-1 b c d rear the worst case add and delete time become O(1) when following formula is used location(i) = (location(1) + i – 1) % Maxsize 山东大学计算机科学与技术学院 数据结构 第 6章 队列 11
Circular queue n n Question: when front=rear the queue is full or empty? Solution: consider the queue is full when front=(rear+1) % Max. Size and empty if front == rear 山东大学计算机科学与技术学院 数据结构 第 6章 队列 14
template<class T> // 一维数组实现 class Queue { public: Queue(int Max. Queue. Size = 10); ~Queue() {delete [] queue; } bool Is. Empty() const {return front = = rear; } bool Is. Full() const {return (((rear + 1) % Max. Size = = front) ? 1 : 0); } T First() const; //返回队首元素 T Last() const; // 返回队尾元素 Queue<T>& Add(const T& x); Queue<T>& Delete(T& x); private: int front; //与第一个元素在反时针方向上相差一个位置 int rear; // 指向最后一个元素 int Max. Size; // 队列数组的大小 T *queue; // 数组 }; 山东大学计算机科学与技术学院 数据结构 第 6章 队列 15
Constructor function template<class T> Queue<T>: : Queue(int Max. Queue. Size) {// 创建一个容量为Max. Queue. Size的空队列 Max. Size = Max. Queue. Size + 1; queue = new T[Max. Size]; front = rear = 0; } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 16
‘First’ and ‘Last’ template<class T> T Queue<T>: : First() const {// 返回队列的第一个元素 if (Is. Empty()) throw Out. Of. Bounds(); return queue[(front + 1) % Max. Size]; } template<class T> T Queue<T>: : Last() const {// 返回队列的最后一个元素 if (Is. Empty()) throw Out. Of. Bounds(); return queue[rear]; } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 17
操作 ‘Add’ template<class T> Queue<T>& Queue<T>: : Add(const T& x) {// 把x 添加到队列的尾部 if (Is. Full()) throw No. Mem(); rear = (rear + 1) % Max. Size; queue[rear] = x; return *this; } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 18
操作 ‘Delete’ template<class T> Queue<T>& Queue<T>: : Delete(T& x) {// 删除第一个元素,并将其送入x if (Is. Empty()) throw Out. Of. Bounds(); front = (front + 1) % Max. Size; x = queue[front]; return *this; } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 19
6. 4 linked representation n A Queue is represented as a chain …… 0 l. Using front and rear to track two ends of queue 1)表头为 front ,表尾为 rear 2)表头为 rear ,表尾为 front 山东大学计算机科学与技术学院 数据结构 第 6章 队列 20
Using presentation front-to-rear implementation n n Define Linked. Queue as base class Front = rear = 0 at beginning Front = 0 iff the queue is empty. queue is full iff there is no memory storage 山东大学计算机科学与技术学院 数据结构 第 6章 队列 24
template<class T> class Linked. Queue { //基类 public: Linked. Queue() {front = rear = 0; } ~Linked. Queue(); bool Is. Empty() const {return ((front) ? false : true); } bool Is. Full() const; T First() const; // 返回第一个元素 T Last() const; // 返回最后一个元素 Linked. Queue<T>& Add(const T& x); Linked. Queue<T>& Delete(T& x); private: Node<T> *front; Node<T> *rear; }; 山东大学计算机科学与技术学院 数据结构 第 6章 队列 25
template<class T> Linked. Queue<T>: : ~Linked. Queue( ) {Node<T> *next; while (front) { next = front->link; delete front; front = next; } } template<class T> bool Linked. Queue<T>: : Is. Full( ) const // 是否有可利用空间 {Node<T> *p; try {p = new Node<T>; delete p; return false; } catch (No. Mem) {return true; } } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 26
template<class T> T Linked. Queue<T>: : First() const { // 返回队列的第一个元素 if (Is. Empty()) throw Out. Of. Bounds(); return front->data; } template<class T> T Linked. Queue<T>: : Last() const {// 返回队列的最后一个元素 if (Is. Empty()) throw Out. Of. Bounds(); return rear->data; } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 27
template<class T> Linked. Queue<T>& Linked. Queue<T>: : Delete(T& x) {// 删除第一个元素,并将其放入x If (Is. Empty( )) throw Out. Of. Bounds(); x = front->data; //保存第一个节点中的元素 Node<T> *p = front; front = front->link; // 删除第一个节点 delete p; return *this; } 山东大学计算机科学与技术学院 数据结构 第 6章 队列 29
Track selection n n When a car to be moved to a holding track: Move car c to a holding track that contains only cars with a smaller label; if there are more than two such track, select the one with largest label at its left end; otherwise select an empty track (if one remains) 山东大学计算机科学与技术学院 数据结构 第 6章 队列 32
- Slides: 34