"Data Structure" C + + code stacks and queues

Source: Internet
Author: User

In the linear table, FIFO is called the queue, advanced out of the call stack. Queues are commonly used in BFS, and when the recursive hierarchy of functions is too high, the recursive process needs to be manually implemented, and a "manual stack" needs to be written.

Sometimes we have a lot of data going in and out of the queue, but there are not many elements in it at the same time, so we need to write "loop queue". Its code is not difficult, but the subscript increment statement is worth considering.

k= (k +1)%MAXN;

This sentence uses the modulo operation%, which is a very waste of time. If you can avoid the use of%, you can greatly improve the speed of code operation. I did a test, the following five kinds of sentences to run the 5x10^8 times, on my machine with codeblocks10.05 each run 5 times, take the average, the time is as follows:

1I= (i==maxn-1)?0:(i+1);//spents 1.582s2 if(i==maxn-1) i=0;Else++i;//spents 1.588s3++i;if(I==MAXN) i=0;//spents 1.605s4++i; I= (I==MAXN)?0I//spents 2.040s5I= (i+1)%MAXN;//spents 4.538s

Taking into account the error of the codeblocks itself, I simply remove this statement, the rest of the code in the Codeblocks run, still 5 times averaged, time is:0.015s.

Therefore, the calculation error can be found, the first two statements the fastest, the third is also good, the fourth is slower, the last one used 3 times times. Therefore, my code uses the first line of the wording, we recommend that you try to use the first three lines of the wording.

The following code is given:

1 //Suppose the type of information stored is int2 3 //Stack4 classStack5 {6     Static Const intMAXN =10000;7     ints[maxn],l;8  Public:9Stack (): L (0) {}Ten     void inch(intx) {s[l++]=x;} One     int  out() {returns[--L]; } A     intSize () {returnL;} - }; -  the //Queue - classQueue - { -     Static Const intMAXN =10000; +     intq[maxn],i,j; -  Public: +Queue (): I (0), J (0) {} A     void inch(intx) {q[j++]=x;} at     int  out() {returnq[i++]; } -     intSize () {returnJ-i;} - }; -  - //Loop Queue - classCyclequeue in { -     Static Const intMAXN =10000; to     intq[maxn],i,j; +     voidAddint&k) {k= (k==maxn-1)?0:(K +1); } -  Public: theCyclequeue (): I (0), J (0) {} *     void inch(intx) {q[j]=x; Add (j);} $     int  out() {intX=q[i]; Add (i);returnx;}Panax Notoginseng     intSize () {return(j>i)? (j-i):(j+maxn-i); } -};

"Data Structure" C + + code stacks and queues

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.