将队列实例的前端复制到另一个队列实例的后面

时间:2014-10-31 03:19:21

标签: c++ queue

我一直在为我的c ++数据结构类做一个任务,我真的被卡住了。 我有大部分程序完成但我正在努力删除乘客功能

该功能的目的是删除[BOOKED]的前面并在[WAITING]的前面并将其分配到[BOOKED]的后面 我似乎无法弄清楚如何将预订的前面分配到等待的后方

非常感谢任何帮助或提示

include <iostream>
include <string>
//#include "cqueue.h"
using namespace std;
//1. cqueue.h

const int MAX = 4; //To do: determine appropriate number

struct Passenger {
    char name[80]; //the data will be an array of Passenger structures,
};

class CQueue {
private:
    int front;  //Index "before" the first element of array that holds a value of the queue

    int rear;   //Index of last element of array that holds a value of the queue

    int temp;

     Passenger passengers[MAX]; //Holds values being placed on the queue.

public:
    CQueue(); // Initializes front and rear. Optionally, you may also want to initialize the values of the data array to some default value.

    bool IsEmpty(void);  //Returns False (zero) if there is at least one item of the queue in array, True (non-zero) if not.

    bool IsFull(void);  //Returns True (non-zero) if all elements of the array contain items of the queue, False (zero) if not.

    void Enqueue(Passenger); /*Assigns the parameter (same data type as array) to the index following the existing rear element of the array and
                               changes the rear variable. */

    Passenger Front(); //[]

    void Dequeue(void); //Changes the front variable.
    void tempdequeue();

    void showfront();

    void frontback();

    char x(char[80]);

    void assign(string b, Passenger);

    };




CQueue:: CQueue ()
{
    front = MAX - 1;
    rear = MAX-1;
}
bool CQueue::IsEmpty(void)
{ //returns true if empty
    return (front == rear);
}

bool CQueue::IsFull(void)

{
    //returns true if the queue is full. flase otherwise
//   

    return(rear + 1) % MAX == front;

}


//2. test.cpp


enum choice { BOOKED, WAITING };
const int LINES = 2;
int showMenu(void);
void addPassenger(CQueue*);
void deletePassenger(CQueue*);
void showPassengers(CQueue*);



int main ()
{
    CQueue qPassengers[LINES];
    int x;
    do{
        x = showMenu();
        switch (x)
        {
            case 1: addPassenger(qPassengers);
                break;
            case 2: deletePassenger(qPassengers);
                break;
            case 3: showPassengers(qPassengers);
                break;
        }
    } while (x != 4);
    return 0;
}

int showMenu()
{
    int select;

    cout << "Menu\n";
    cout << "========\n";
    cout << "1. Add Passenger\n";
    cout << "2. Delete Passenger\n";
    cout << "3. Show Passengers\n";
    cout << "4. Exit\n";
    cout << "Enter choice: ";
    cin >> select;
    return select;
}


void addPassenger(CQueue* crack)

{
    if (crack[BOOKED].IsEmpty())
    {
        cout << "the queue is empty" << endl;
    }
    if (crack[BOOKED].IsFull())
    {
        cout << "the queue is full." << endl;
        crack[WAITING].Enqueue(Passenger());
    }
    else if (crack[BOOKED].IsFull() && crack[WAITING].IsFull())
    {
        cout << "the plane is full try again later" << endl;
    }

    else
    {
        crack[BOOKED].Enqueue(Passenger());

    }
}

void CQueue::Enqueue(Passenger a)
{

    rear = (rear + 1 ) % MAX;
    cin >> a.name;
    passengers[rear]= a;
    cout<<"front: "<<front<<endl<<endl;

}


void deletePassenger(CQueue* crack)
{
    char name [80];
    if (crack[BOOKED].IsEmpty())
    {
        cout << "the queue is empty" << endl;
    }
    else
    {
        crack[BOOKED].Dequeue();
        crack[WAITING].x(name);
        crack[WAITING].Dequeue();
        crack[BOOKED].assign(name, Passenger());
    }

}

char CQueue::x(char a[80])
{
    a = passengers[front].name;
    return *a;
}

void CQueue::assign(string pie, Passenger a)
{
    cout << pie;
    a = pie
    passengers[rear]= a;

}

void CQueue::Dequeue()
{
//    if (IsEmpty())
//    {
//        cout << "the slot is empty" << endl;
//    }
//    else
    {
        front = (front + 1) % MAX;
    }


}

void showPassengers(CQueue* crack)
{
//   string x = crack[BOOKED].Front().name;
//    cout << x;
    crack->showfront();
    if (crack[BOOKED].IsEmpty())
    {
        cout << "No Passengers" << endl;
    }
    else if (crack[BOOKED].IsEmpty() == false)
    {
        cout << "Booked Passengers" << endl
        << "===============" << endl;
        //if (crack[BOOKED].IsEmpty() == false)
        for ( int x = 0 ; x < 3 ; x++)
        {
            cout << crack[BOOKED].Front().name<< endl;
            crack[BOOKED].tempdequeue();
        }
      //  crack->frontback();
        cout << "WAITING LIST" << endl;
             cout << "===============" << endl;
        for ( int x = 0 ; x < 3 ; x++)
        {

            cout << crack[WAITING].Front().name << endl;
            crack[WAITING].tempdequeue();
        }
    }
    crack->frontback();
}



Passenger CQueue::Front(){
   return passengers[(front+1)%MAX];
    //return passengers[front];
}//Front

void CQueue::showfront()
{
     temp = front;
}

void CQueue::frontback()
{
    front = temp;
}
void CQueue::tempdequeue()
{
    front = (front + 1) % MAX;
}


// To do: implement addPassenger, deletePassenger and showPassengers

1 个答案:

答案 0 :(得分:0)

您应该从std::queue

查看标准队列

你有CQueue :: Front(),它返回队列中的第一个元素,然后从该队列中删除它

 Passenger firstElement = cQueue.Front();
 cQueue.Dequeue();

那么你的其他队列应该有

 cQueue2.addPassenger(firstElement);