在C ++中使用Array实现队列

时间:2018-03-11 11:43:06

标签: c++ data-structures integer queue

我在C ++中使用数组实现了一个整数类型的Queue:
这不是一个循环队列,因此在填充一次然后删除一些值后,我们不必为空白空间而烦恼。

class IntQueue
{
    private:
        int *queueArray;
        int queueSize;
        int rear, front;

    public:
        IntQueue(int);          // Constructor
        ~IntQueue(void);
        void Enqueue(int);
        int Dequeue(void);
        bool isFull(void);
        bool isEmpty(void);
        void Display(void);
};  

// Constructor
IntQueue::IntQueue(int size)
{
    queueArray = new int[size];
    queueSize = size;
    rear = -1;
    front = -1;
}  

// Destructor
IntQueue::~IntQueue(void) 
{    
    delete [] queueArray;
}  
// Enqueue function
void IntQueue::Enqueue(int num)
{
    if (isFull())
    {
        cout << "The Queue is full.\n";
    }
    else
    {
        rear++;
        queueArray[rear] = num;
    }   
}

// Dequeue function
int IntQueue::Dequeue()
{
    int num;
    if (isEmpty())
    {
        cout << "The Queue is empty.\n";
    }
    else
    {
        front++;
        num = queueArray[front];
    }
    return num;
}

//isFull function
bool IntQueue::isFull(void)
{
    bool status;

    if (rear == queueSize - 1)
        status = true;
    else
        status = false;
    return status;
}

//isEmpty function
bool IntQueue::isEmpty(void)
{
    bool status;

    if (front == rear)
        status = true;
    else
        status = false;
    return status;
}

// Display function
void IntQueue::Display()
{
    if(isEmpty())
    {
        cout << "The Queue is empty.\n";
    }

    for(int i = front++; i <= rear; i++)
    {
        cout << "queueArray[" << i << "] = " << queueArray[i] << endl;
    }
}  
// Main function
int main()
{
    int option, queueCapacity, value;


    cout << endl << "Enter size of queue: ";
    cin  >> queueCapacity;

    IntQueue *queue = new IntQueue(queueCapacity);

    Menu:
    cout << endl << "Queue implementation using Array" << endl;
    cout << "--------------------------------" << endl;
    cout << "1. Enqueue"    << endl;
    cout << "2. Dequeue"     << endl;
    cout << "3. Display Queue" << endl;
    cout << "4. Display Menu" << endl;
    cout << "5. Exit"    << endl;
    cout << "--------------------------------" << endl;

    YourOption:
    cout << endl << "Your option: ";
    cin >> option;

    switch(option)
    {
        case 1:
            // Enqueue
            cout << "Enter value to enqueue: ";
            cin  >> value;
            queue->Enqueue(value);
            goto YourOption;

        case 2:
            // Dequeue
            int data;
            data = queue->Dequeue();
            cout << data << " is dequeued!";
            goto YourOption;    
        case 3:
            // Display
            queue->Display();
            goto YourOption;    
        case 4:
            goto Menu;  
        case 5:
            exit(0);
        default:
            cout << "Invalid option!" << endl;
            break;
    }
    delete queue;
    return 0;
}

变量rear最初为-1。我检查了isFull()函数,好像rear等于Queue的大小,然后队列已满,否则没有。但是,为了检查队列空虚,我比较了rear == front,然后是空,否则。但是在执行它之后,通过从菜单中选择2,它有时会显示一些随机值,并显示消息,即“队列为空”。
这个例子中有什么问题?

2 个答案:

答案 0 :(得分:0)

void enqueue(int x)
{
    //cout << "Enter the DAta " << endl;
    //cin >> x;
    if (front==(rear+1)%size)
    {
        cout << "Queue Is overflow::" << endl;
        return;
    }
    else if (rear==-1&&front==-1)
    {
        //cout << "enter the To EnQueue :" << endl;
        front = rear = 0;
        queue[rear] = x;
    }
    else if (rear==0 && front==0)
    {
        rear = rear + 1 % size;
        queue[rear] = x;
    }
    else
    {
        rear++;
        queue[rear] = x;
    }

}

答案 1 :(得分:0)

您的队列工作正常,但您需要检查一些变量值,例如Enqueue(int num)函数,您忘记更新变量front=0

void IntQueue::Enqueue(int num)
{
   if (isFull())
   {
     cout << "The Queue is full.\n";
   }
   else
   {
     rear++;
     queueArray[rear] = num;
   }   
 }

因为在您显示值front=-1的初始时间。

所以,在front=0函数中添加enqueue,就像这样,

void IntQueue::Enqueue(int num)
{
  if (isFull())
   {
     cout << "The Queue is full.\n";
   } 
  else
   {
     front=0;
     rear++;
     queueArray[rear] = num;
   }   
}  

希望你明白了。如果你有任何疑惑,请评论它。