Queue-Train rearrangement problem

When doing the stack and queue part of the data structure experiment by myself, I spent a lot of time to do this problem. I originally wanted to copy the algorithm on the book directly, but I didn't try it out after a long trial. )), Thinking that the algorithm is wrong, he threw the book aside and wrote one by himself, and finally compared with the book, the result is slightly different. ≧ ﹏ ≦.
Existing n cars, given k-section buffer rails, are located between the derailment and the inbound rail. The order of the carriages has been disrupted, requiring n carriages to enter the buffer rail from entering the rail, and the order from derailing is 1 ~ n.
Example: 9 carriages in the car, buffer rail 3 into the
rail sequence is 3 6 9 2 4 7 1 8 5
derailment sequence is 1 2 3 4 5 6 7 8 9

Queue solving algorithm

The solution algorithm I gave is as follows. Compared with main.cpp for detailed information, there is a small process that the actual number of buffer rails that can be used should be k-1, because the entry into the track needs to be combined with a buffer rail.

  1. If all elements are derailed, output the result, otherwise perform 2
  2. Compare whether the head of the derailment queue and the derailed element are the same. If they are the same, the derailment queue of the element enters the derailment queue. The derailment element should be adjusted without performing 3. Otherwise, execute 2 after executing 3
  3. Loop check the non-empty buffer queue. If there is a buffer track head equal to the derailed element, the element exits the buffer track queue and enters the derailment queue. The derailed element should be adjusted.
  4. If the inbound queue is not empty
    1. Check the non-empty buffer queue circularly. If the entry element is greater than the tail element of the buffer queue, the element enters the buffer track. 3.2 is not executed, otherwise 3.2
    2. If the buffer track is left, push the element into the nearest empty queue, otherwise the train cannot be rearranged and the procedure is ended.
  5. Execute 1

Sample program

/*Queue.h*/
#include<iostream>
using namespace std;
template <class T>
class Queue
{
public:
    template <class S>
    class Node //火车车厢节点
    {
    public:
        S data;
        Node<S>* next;
        Node<S>():next(nullptr) {}
        Node<S>(T i):data(i),next(nullptr) {}
    };
    Node<T> * front;
    Node<T> * rear;//队头队尾

    Queue(){
        Node<T> *p=new Node<T>;
        p->next=nullptr;
        front=rear=p;
    }
    ~Queue(){
        Node<T> *p=nullptr;
        while(front!=nullptr){
            p=front->next;
            delete front;
            front=p;
        }
    }
    void inQueue(T i){
        Node<T> *p=new Node<T>;
        p->data=i;
        p->next=nullptr;
        rear->next=p;
        rear=p;
    }
    T getfront(){
        Node<T> *p=front->next;
        if(p==nullptr)throw "null";
        return p->data;
    }
    T getrear(){
        Node<T> *p=rear;
        if(p==nullptr)throw "null";
        return p->data;
    }
    T outQueue(){
        Node<T> *p=nullptr;
        T i;
        if(rear==front) throw "null,can't out";
        p=front->next;
        front->next=p->next;
        i=p->data;
        if(p->next==nullptr)rear=front;
        delete p;
        return i;
    }
    void printQueue()
    {
        for(Node<T> *p=front->next; p!=nullptr; p=p->next)
        {
            cout<<p->data<<" " ;
        }
        cout<<endl;
    }
    bool isEmpty()
    {
        return front==rear;
    }
};
/* main.cpp */
#include <iostream>
#include "Queue.h"
using namespace std;


int main()
{
   //输入车厢和火车轨数
    int n,k;
    cin>>n>>k;
   //初始化入轨,出轨,k节缓冲轨
    Queue<int> *dusk=new Queue<int>[k+1];//0,k入轨,出轨,1~k-1缓冲轨
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        dusk[0].inQueue(a);
    }
   
    while(nowcount<=n){
        //检查入轨是否满足
        flag=0;
        if(!dusk[0].isEmpty()&&nowcount==dusk[0].getfront()){
            dusk[k].inQueue(dusk[0].outQueue());
            cout<<nowcount<<"从0轨出轨"<<endl;
            nowcount++;
            flag=1;
        }

        //检查其余缓冲轨是否满足
        for(int i=1;i<k;i++){
            if(!dusk[i].isEmpty()){
                if(dusk[i].getfront()==nowcount){
                    dusk[k].inQueue(dusk[i].outQueue());
                    cout<<nowcount<<"从"<<i<<"轨出轨"<<endl;
                    nowcount++;
                }
            }
        }
        //添加至其余缓冲轨,前提入轨非空且入轨未直接出轨
        if(!dusk[0].isEmpty()&&!flag){
            flag=0;
            //优先添加至非空队列
            for(int i=1;i<k;i++){
                if(!dusk[i].isEmpty()){
                    //如果队尾小于入轨,入队
                    if(dusk[i].getrear()<dusk[0].getfront()){
                        dusk[i].inQueue(dusk[0].outQueue());
                        cout<<dusk[i].getrear()<<" 入"<<i<<"轨"<<endl;
                        flag=1;
                        break;
                    }
                }
            }
            //如果添加至非空队列失败,添置新缓冲轨
            if(!flag){
                if(num<k-1)//如果缓冲轨有空余
                    for(int i=1;i<k;i++){
                        if(dusk[i].isEmpty()){
                            dusk[i].inQueue(dusk[0].outQueue());
                            cout<<dusk[i].getrear()<<" 入"<<i<<"轨"<<endl;
                            num++;
                            break;
                        }
                    }
                else{
                    cout<<"重排失败,缓冲轨已用完"<<endl;
                }
            }
        }
    }
    /**************************************/
    cout<<"重排结束,排序结果为"<<endl;
    dusk[k].printQueue();

   return 0;
}

The result of the program is
Insert picture description here

Published 3 original articles · Like1 · Visits 62

Guess you like

Origin blog.csdn.net/weixin_43323747/article/details/103303257
Recommended