基于数组的队列c ++

时间:2015-03-23 16:20:13

标签: c++ queue

我创建了一个基于数组的队列,除了我的队列外,一切似乎都正常。当我在switch语句中选择选项4时,它会打印出队列中的所有项目,但在此之前它应该将1个项目出列,而不是这样做。我应该在出队功能中改变一些东西吗?

using namespace std;

typedef int info;

struct queue
{
    info items[20];
    int back;
};


queue create(){
    queue temp;
    temp.back = -1;
    return temp;

}

void enqu(queue &q, info &item){
    if (q.back > 29) {
        cout << "queue is full";
    }else {
        q.back++;
        q.items[q.back] = item;
    }
}

void purge(queue &q)
{
    q.back = -1;
}



bool empty(queue &q)
{
    if (q.back == -1)
        return true;
    else
        return false;
}

void dequ(queue &q,info &item)
{
    int count;

    if(empty(q) == true){
        cout << "queue is empty";
    } else {
        item = q.items[0];
        for(count = 1; count <= q.back; count++){
            q.items[count - 1] = q.items[count];
        }
        --q.back;
    }
}

void print(queue q)
{
    int item;
    while (empty(q) != true){
        dequ(q, item);
        cout << item << " ";
    }
    cout << endl;
}



int main(int argc, const char * argv[]) {


    queue  data, q19, q1019, q2029, q30;
    data = create();


    int choice;
    info item = 0;


    cout << "Please Enter numbers";

    q19 = create();
    q1019 = create();
    q2029 = create();
    q30 = create();


    int jobs;
    int ct;


    do{
        do
        {
            cout << "------------------------------\n";
            cout << "Enter '1' to Add a job to the Queue.\n";
            cout << "Enter '2' to see what's in the Queue.\n";
            cout << "Enter '3' to Delete the current Queue.\n";
            cout << "Enter '4' to Process Jobs.\n";
            cout << "------------------------------\n";
            cout << "Choice: ";
            cin >> choice;
        } while (choice < 1 || choice > 4);

        switch (choice)
        {
            case 1: cout << "How many jobs would you like to queue?: ";
                cin >> jobs;
                while (jobs < 0 || jobs > 30 ){
                    cout << "This Queue may only handle 30 jobs\n";
                    cout << "at a time, Enter a new Value or try\n";
                    cout << "again later: ";
                    cin >> jobs;
                }
                for(ct = 0; ct < jobs; ct++){
                    cout << "Enter a Value: ";
                    cin >> item;
                    enqu(data, item);


                }
                break;
            case 2: cout << "\nData Queue: ";
                for(ct = 0; ct <= data.back; ct ++){
                    cout << data.items[ct] << " ";
                }
                cout << endl;
                break;

            case 3: purge(data);

                break;

            case 4: while(empty(data) != true)

                {
                   dequ(data, item);
                if (item < 10)                       //If Item < 10
                    enqu(q19, item);
                else if(item >= 10 && item < 20)     //If Item >= 10 & < 20
                    enqu(q1019, item);
                else if(item >= 20 && item < 30)     //If Item >= 20 & < 30
                    enqu(q2029, item);
                else if(item >= 30)                  //If Item >= 30
                    enqu(q30, item);

            }
                cout << "1 to 9        ";
                print(q19);
                cout << "10 to 19      ";
                print(q1019);
                cout << "20 to 29      ";
                print(q2029);
                cout << "30 or more    ";
                print(q30);

                break;
        }
    } while (choice > 0 && choice < 4);



    cout << endl;



    return 0;

}

0 个答案:

没有答案