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

copying the front of a queue instance to the rear of another queue instance

本文关键字:实例 队列 另一个 前面 复制      更新时间:2023-10-16

我一直在为我的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 << "Menun";
    cout << "========n";
    cout << "1. Add Passengern";
    cout << "2. Delete Passengern";
    cout << "3. Show Passengersn";
    cout << "4. Exitn";
    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

您应该从std::queue 中查看标准队列

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

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

那么你的另一个队列应该有

 cQueue2.addPassenger(firstElement);