Entities essential for Array Representation of Queue - Data Structure

Q.  Which among the below mentioned entities is / are essential for an Array Representation of a Queue?
- Published on 19 Oct 15

a. An array to hold queue elements
b. A variable to hold the index of front element
c. A variable to hold the index of rear element
d. All of the above

ANSWER: All of the above
 

    Discussion

  • Nirja Shah   -Posted on 16 Nov 15
    For an Array Representation of a Queue, the following points are necessary.
    - An array to hold queue elements
    - A variable to hold the index of front element
    - A variable to hold the index of rear element
    Consider a simple example.
    class Queue
    {
       int front,rear,size,i,item;
      int ∗que;
    public:
      Queue()
    {
       front=rear= -1;
       cout << "Enter the size of queue\t\t";
       cin >> size;
       que=new int[size];
    }
      void insert()
    {
       if(rear==size-1)
      {
      cout<< "queue is Full";
      }
     else
      {
       cout<< "Enter the "<< size << " item\n";
       for(i=0;i<size;i++)
        {
       cin>> item;
       rear++;
       que[rear]=item;
        }
      if(front==-1)
       front++;
      }
    }
    void deleteItem()
    {
       if( front==-1 || front==rear+1)
      {
       cout<< "queue is empty";
      }
    else
      {
       cout << "The deleted item is "<< que[front] << endl;
       front++;
      }
    }
    void display()
      {
       if(front==-1||front==rear+1)
      {
       cout << "queue is empty";
      }
    else
       {
        for(i=front;i<=rear;i++)
      {
       cout << que[i] << "\t\t";
      }
       }
      }
    };

    int main()
    {
       Queue obj;
       int choice;
    do
      {
        cout << "Enter 1. for Insert\n";
        cout << "Enter 2. for Delete\n";
        cout << "Enter 3. for showItem\n";
        cout << "Enter 4. for Exit\n";
        cout << "Enter you choice\n";
        cin >> choice;
      switch(choice)
     {
        case 1:
       obj.insert();
       break;
        case 2:
       obj.deleteItem();
       break;
        case 3:
       obj.display();
       break;
        case 4:
       break;
        default:
       cout<<"Wrong choice\n";
       break;
       }
     }
       while(choice < 4);
       return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:
 
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)