Application of queue: Dual-end queue

Source: Internet
Author: User

Dual-end queue(Deque: double ended queue) is a queue with both ends ending. Each end of the queue can insert and remove data items. Compared with normal queues, the queuing and queuing operations of double-end queues can be performed at both ends.

Dual-end queue:


Left: right

Here we use the most common sequence structure to store dual-end queues. To save space, we connect them first and end to form a cyclic queue. It is also required that left points to the first element on the left end, and right points to the next position on the right end. If the team is empty, the result is left = right. The team is full (left-1 + MAX) % MAX = right or (right-left + MAX) % MAX = MAX. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + z + rPuM + 4vdq/tlt6wujuw.vcd4kpha + wOC2qNLlus3A4Mq1z9Y8L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java;"> # include # Include Using namespace std; // The maximum storage space of the queue is MAX const int MAX = 10; typedef int ElemType; // double-end queue class Deque/double ended queue {private: int size; // number of elements in the queue ElemType * base; int left, right; // 0 indicates left, and 1 indicates right rightpublic: // constructor Deque (); // Structure ~ Deque () {delete [] base;} // obtain the int getSize () {return size;} // determine bool empty () {return left = right ;} // or size = 0 // determine bool full () {return (left-1 + MAX) % MAX = right ;} // obtain the Header element bool topAt (ElemType &, int) at the specified end; // insert (join) bool pushAt (ElemType, int) at the specified end ); // Delete (out of the queue) bool popAt (int) on the specified end; // print the queue void print (int) from the specified end; // empty the queue void clear ();}; deque: Deque () {base = new ElemType [MAX]; left = right = 0; size = 0;} bool Deque:: TopAt (ElemType & data, int end) {if (empty () return false; if (end = 0) data = base [left]; elsedata = base [(right-1 + MAX) % MAX]; return true;} bool Deque: pushAt (ElemType data, int end) {if (full ()) return false; if (end = 0) {left = (left-1 + MAX) % MAX; base [left] = data;} else {base [right] = data; right = (right + 1) % MAX;} return true;} bool Deque: popAt (int end) {if (empty () return false; if (end = 0) Left = (left + 1) % MAX; elseright = (right-1 + MAX) % MAX; return true;} void Deque: print (int end) {if (empty () {cout <"empty queue, cannot traverse! "<Endl; return;} if (end = 0) {int I = left; while (I! = Right) {cout <setw (4) <base [I]; I = (I + 1) % MAX ;}} else {int I = right; while (I! = Left) {I = (I-1 + MAX) % MAX; cout <setw (4) <base [I] ;}} cout <endl ;} void Deque: clear () {left = right = 0; size = 0 ;}Main functions and related methods:

Void check (int & end) // check the end number {while (cin> end &&! (End = 0 | end = 1) {cout <"incorrect end number, re-enter:" ;}} void input (Deque & deque) // Input Function {int end; cout <"Enter the queue at the specified end, 0 left end, 1 right end:"; check (end); ElemType data; cout <"input data, input 0 ends" <endl; while (cin> data & data) {deque. pushAt (data, end) ;}} void traverse (Deque & deque) // traverse from the specified end {int end; cout <"traverse from the specified end :"; check (end); deque. print (end);} int main () {cout <"******* double-end queue drill ******" <endl; Deque deque; cout <"new double-end queue" <endl; cou T <"is the queue empty:"; deque. empty ()? Cout <"Yes! "<Endl: cout <" No! "<Endl; input (deque); traverse (deque); ElemType data; int end; cout <"Get the Header element of a specified end:"; check (end); deque. topAt (data, end )? Cout <data <endl: cout <"the team is empty! "<Endl; cout <" Delete the Header element of a specified end: "; check (end); deque. popAt (end )? Cout <"deleted successfully" <endl: cout <"Empty team! "<Endl; traverse (deque); cout <" empty queue: "; deque. clear (); deque. empty ()? Cout <"Yes! "<Endl: cout <" No! "<Endl; system (" pause "); return 0 ;}

Run:



If it is helpful, try again!

Column Directory: Data Structure and algorithm directory



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.