Data Construction 数据结构 队列

//========================the practice of the queue =======================//

/*************************************************
Copyright:all rights reversed
Author: Steven
Date:2010-08-25
Description: Conditions      : give you the number 1-N  in sequence.
             Operation rules : move out the top  number in sequence
                               move the one afer the top one to the end of the sequence.
             task            : give the sequence of the number moved out
**************************************************/

#include<iostream>
#include<queue>
using namespace std;
int main()
{
    int num_count =0; // the quanity of the number
    queue<int> num_queue; // number queue
    cin>>num_count;
    for(int index=0;index<num_count;index++)
    {
        num_queue.push(index+1);
    }
    while(!num_queue.empty())
    {
        cout<<num_queue.front()<<endl;
        num_queue.pop();
        // judge whether the queue is empty
        if(num_queue.empty())
            break;
        else
        {
            num_queue.push(num_queue.front());
            num_queue.pop();
        }
       
    }
    system("pause");
    return 0;
}

/*
  optimize code 
  the sentence while contains if .this makes code is not easy to understand.
  so the sentence while handle the condition that at list two number.
*/

 while(num_queue.size()>=2)
    {
        cout<<num_queue.front()<<endl;
        num_queue.pop();
        num_queue.push(num_queue.front());
        num_queue.pop();
       
       
    }
    cout<<"the last number is: "<<num_queue.front()<<endl;
    num_queue.pop();

你可能感兴趣的:(Data Construction 数据结构 队列)