2014-10-31 3 views
0

Я выполнял задание для своего класса структур данных C++ и действительно застрял. У меня есть большая часть программы в комплекте, но я борюсь с функцией удаления пассажиров.копирование перед экземпляром очереди в конец другого экземпляра очереди

Пункт функции состоит в том, чтобы удалить переднюю часть [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 

ответ

0

Вы должны смотреть на стандартную очереди из std::queue

Вы CQueue :: Front(), которая возвращает первый элемент в очереди, а затем удалить его из этой очереди

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

Тогда ваша вторая очередь должна иметь

cQueue2.addPassenger(firstElement); 
Смежные вопросы