"Weiss", "chapter No. 03" Exercise 3.25: Array emulation queue

Source: Internet
Author: User

"Exercise 3.25"

Write a routine that implements the queue, using the

A. Linked list

B. Arrays

Answer:

At the beginning of this chapter has written a list of the queue routines, so in fact, as long as the B-small problem can be.

The array simulation queue and the two point small difference of the linked list are:

①, array space is limited, the queue needs to detect whether the array is full

②, array after a few operations, rear may be around back to front front, so many operations are to be implemented using modulo.

Test code:

1#include <iostream>2#include"queue.h"3 using namespacestd;4 using namespacequeue;5Templateclassqueue<int>;6 intMainvoid)7 {8simu_queue<int> Test (6);9     //Test InsertTenTest.enqueue (2); OneTest.enqueue (3); ATest.enqueue (5); -Test.enqueue (7); -Test.enqueue ( One); theTest.enqueue ( -); -Test.enqueue ( -); - test.traverse (); -cout <<Endl; +     //Test Delete - test.dequeue (); + test.dequeue (); A test.dequeue (); at test.traverse (); -cout <<Endl; -     //testing a loop around an array -Test.enqueue ( -); -Test.enqueue ( +); -Test.enqueue ( at); in test.traverse (); -cout <<Endl; to  +System"Pause"); -}
View Code

Implementation code:

1 //Exercise 3.25 New, simulating queues with arrays2Template <typename t>classSimu_queue3 {4  Public:5Simu_queue (): Head (nullptr), Front (0), Rear (0), Size (0){}6Simu_queue (unsignedint_maxsize): Front (0), Rear (0), MaxSize (_maxsize) {head =NewT[maxsize +1]; }7Simu_queue (Constsimu_queue&another)8     {9Front =Another.front;TenRear =another.rear; OneMaxSize =another.maxsize; AHead =NewT[maxsize +1]; -          for(Unsigned i =0; I < MaxSize +1; ++i) -Head[i] =Another.head[i]; the     } -~Simu_queue () -     {  - delete[] head; +Front = Rear = MaxSize =0; -Head =nullptr; +     } Asimu_queue&operator=(Constsimu_queue&another) at     { -         if( This! = &another) -         { - delete[] head; -Front =Another.front; -Rear =another.rear; inMaxSize =another.maxsize; -Head =NewT[maxsize +1]; to              for(Unsigned i =0; I < MaxSize +1; ++i) +Head[i] =Another.head[i]; -         } the     } *  Public: $     //returns the maximum element amountPanax NotoginsengUnsignedintSize ()Const{returnmaxsize;} -     //returns the current element amount theUnsignedintLength ()Const{returnFront <= rear? Rear-front:rear + maxsize +1-Front;} +     //determines whether the empty A     BOOLEmpty ()Const{returnFront = =Rear;} the     //Queue +     BOOLEnqueueConstT &Item) -     { $         if((Rear +1)% (MaxSize +1) !=front) $         { -Head[rear] =item; -Rear = (rear +1)% (MaxSize +1); the             return true; -         }Wuyi         return false; the     } -     //out Team Wu     BOOLdequeue () -     { About         if(Rear! =front) $         { -Front = (front +1)% (MaxSize +1); -             return true; -         } A         return false; +     } the     //Output Queue Elements -     voidTraverse ()Const $     { theUnsignedinttemp =Front; the          while(Temp! =rear) the         { thecout <<" "<< Head[temp] <<Flush; -Temp = (temp +1)% (MaxSize +1); in         } the     } the Private: Aboutt* head =nullptr; theUnsignedintFront; theUnsignedintRear; theUnsignedintmaxsize; +};

"Weiss", "chapter No. 03" Exercise 3.25: Array emulation queue

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.