重庆大学数据结构英文课件Queue

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Data Structure
来自百度文库
04 Queue
Queues
FIFO: First in, First Out Restricted form of list: Insert at one end, remove from the other. Notation:
ðp ðp ðp ðp
Insert: Enqueue Delete: Dequeue First element: Front Last element: Rear
Data Structure
04 Queue
Queue ADT
// Reinitialize the queue. The user is responsible for // reclaiming the storage used by the queue elements. virtual void clear() = 0; // Place an element at the rear of the queue. // it: The element being enqueued. virtual void enqueue(const E&) = 0; // Remove and return element at the front of the queue. // Return: The element at the front of the queue. virtual E dequeue() = 0; // Return: A copy of the front element. virtual const E& frontValue() const = 0; // Return: The number of elements in the queue. virtual int length() const = 0; };
Data Structure
04 Queue
Queue Implementation (2)
Data Structure
04 Queue
Queue Implementation (2)
ðp
How can we recognize when the circular queue is empty or full?
ðp
A refinement of the lazy approach is the circular queue
head = tail = 0; enQueue enQueue enQueue enQueue enQueue (q, (q, (q, (q, (q, ‘a’); ‘b’); ‘c’); ‘d’); ‘e’); 1
ðn
ðn
Another solution is to make the array be of size n+ 1, and only allow n elements to be stored.
Data Structure
04 Queue
Circular Queue Demo
ðp
A refinement of the lazy approach is the circular queue
Data Structure
04 Queue
Array_based Queue
ðp
The array-based queue is somewhat tricky to implement effectively. Assume that there are n elements in the queue. ðn If we choose the rear element of the queue to be in position 0, enqueue operations will shift the n elements currently in the queue one position in the array. ðn If instead we chose the rear element of the queue to be in position n-1, a dequeue operation must shift all of the elements down by one position to retain the property that the remaining n1 queue elements reside in the first n-1positions of the array. ðn A far more efficient implementation can be obtained by relaxing the require-ment that all elements of the queue must be in the first n positions of the array.
2 c b d e
3
enQueue (q, ‘f’); ??? head
Data Structure
0
a
4
5
04 Queuetail
Circular Queue Demo
ðp
A refinement of the lazy approach is the circular queue
Empty: head == tail; Full: tail+1 == head ??? 1 General Equations: head = (head+1)%N; tail = (tail+1)%N; head
ðn
For both empty queue and full queues ÿ the value for rear is one less than the value for front0 One obvious solution is to keep an explicit count of the number of elements in the queue, or a Boolean variable that indicates whether the queue is empty or not.
Data Structure
04 Queue
Queue Implementation (1)
Data Structure
04 Queue
Queue Implementation (1)
ðp
This implementation raises a new problem.
ðn
When elements are removed from the queue, the front index increases. Over time, the entire queue will drift toward the higher-numbered positions in the array. Once an element is inserted into the highest-numbered position in the array, the queue has run out of space. This happens despite the fact that there might be free positions at the low end of the array where elements have previously been removed from the queue.
head = tail = 0; enQueue (q, ‘a’); 1
2
3
tail head
Data Structure
0 5
4
04 Queue
Circular Queue Demo
ðp
A refinement of the lazy approach is the circular queue
Data Structure
Chapter 4 Queues
College of Computer Science, CQU
Outline
ðp ðp ðp ðp
Queue ADT Circular Queue Linked Queue Comparison of Array-Based and Linked Queues
Data Structure
04 Queue
2
Queues
ðp
Like the stack, the queue is a list-like structure that provides restricted access to its elements.
ðp
Queue elements may only be inserted at the back (called an enqueue operation) and removed from the front (called a dequeue operation).
tail head = tail = 0; enQueue (q, ‘a’); enQueue (q, ‘b’); enQueue (q, ‘c’); 1
2
b
3
0 head
Data Structure
a 4 5
04 Queue
Circular Queue Demo
ðp
A refinement of the lazy approach is the circular queue
Data Structure
04 Queue
Queue Implementation (2)
ðp
The drifting queue problem can be solved by pretending that the array is circular and so allow the queue to continue directly from the highest-numbered position in the array to the lowest-numbered position.
head = tail = 0; enQueue enQueue enQueue enQueue (q, (q, (q, (q, ‘a’); ‘b’); ‘c’); ‘d’); 1
2 c b
tail 3
0 head
Data Structure
a 4 5
04 Queue
Circular Queue Demo
head = tail = 0; enQueue (q, ‘a’); enQueue (q, ‘b’); 1
2
tail
3
0 head
Data Structure
a 4 5
04 Queue
Circular Queue Demo
ðp
A refinement of the lazy approach is the circular queue
Data Structure
04 Queue
Example: Queue of Char
insert insert insert
c b a b a
delete
c b
a
Data Structure
04 Queue
Queue ADT
// Abstract queue class template <typename E> class Queue { private: void operator =(const Queue&) {} // Protect assignment Queue(const Queue&) {} // Protect copy constructor public: Queue() {} // Default virtual ÜQueue() {} // Base destructor
2 c b d
3
0 head
a 4 5
tail
Data Structure
04 Queue
Circular Queue Demo
ðp
A refinement of the lazy approach is the circular queue
head = tail = 0; enQueue enQueue enQueue enQueue enQueue (q, (q, (q, (q, (q, ‘a’); ‘b’); ‘c’); ‘d’); ‘e’); 1
相关文档
最新文档