Data Construction stack 数据结构 栈的练习

//============================the possible sequence of the stack=============================//
/*

Copyright  all rights reversed
Author   steven
Date: 2011-06-23
Description :
              Conditions: 1. Give you  the sequence of  original datas.
                          2. Give you  the sequence of  destination datas
                          3. These datas  may pass though a stack.
              task: judge the  destination datas whether is a possible sequence passing through a stack.      
           

*/
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
void main()
{
    int input_num =0;                        //original data input temp
    int input_count=0;                       // the length of the sequence
    queue<int> original_que;                 // original data queue
    queue<int> dest_que;
    stack<int> dest_stack;                   // dest data stack
    cin>>input_count;                        // get the lenght
    cout<<"input original data"<<endl;
    for(int index =0;index<input_count;index++)
    {
        cin>>input_num;
        original_que.push(input_num);
    }
    cout<<"dest sequence "<<endl;
    for(int index =0;index<input_count;index++)
    {
        cin>>input_num;                    // input the destination data
        dest_que.push(input_num);         
       // original_que.push(index+1);         //init the original data
    }

    while(!original_que.empty())//just make all the original data in a stack
    {
        if(original_que.front()==dest_que.front())
        {
            original_que.pop();
            dest_que.pop();
        }
        else if(!dest_stack.empty() && (dest_stack.top()==dest_que.front()))
        {
            dest_stack.pop();
            dest_que.pop();
        }
        else
        {
            dest_stack.push(original_que.front());
            original_que.pop();
        }
    }
    while(!dest_que.empty()) // match the desk_que and the dest_stack
    {
        if(dest_que.front()==dest_stack.top())
        {
            dest_stack.pop();
            dest_que.pop();
        }
        else
        {
            break;
        }
    }

    if(dest_stack.empty())
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    system("pause");
}

你可能感兴趣的:(数据结构,System,input,construction)