将队列实例的前面复制到另一个队列实例的后面
copying the front of a queue instance to the rear of another queue instance
我一直在为我的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);
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- boost::进程间消息队列引发错误
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- Android NDK传感器向事件队列报告奇怪的间隔
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 如何创建一个将队列作为数据成员的类,该成员在 c++ 中存储另一个类的实例
- 将队列实例的前面复制到另一个队列实例的后面
- 尝试将用户定义类的实例排入队列时出现无限循环(?)