Friday 1 January 2021

What is Queue In Data Structures? Implementation Using C++.

 

Queue in Data Structures:

The queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

For Example:


A real-world example of a queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops.

queue in data structures and algorithms

Queue Implementation Using C++


#include<iostream>
using namespace::std;
 class ArrayBasedQueue{
  private:
    int array[5];
    int rear,front,noofelem;
    public:
     ArrayBasedQueue()
     {
      front=-1;
      rear=0;
      noofelem=0;
     }
  void enqueue(int value){
   if(noofelem==5)
   cout<<"Queue Is Full !"<<endl;
   else{
    array[rear]=value;
    cout<<"Value Is Enqueue At Index No. "<<rear;
    cout<<" Is :"<<array[rear++]<<endl;
    noofelem++;
   }
  }
 void dequeue(){
  if(noofelem==0)
  cout<<"Queue Is Empty !"<<endl;
       else{
         cout<<"The Value Dequeue From Index No. "<<front<< " Is :"<<array[++front]<<endl;
         noofelem--;
    }
 }
 };
 int main(){
  ArrayBasedQueue* queue =new ArrayBasedQueue();
  queue->enqueue(33);
  queue->enqueue(18);
  queue->enqueue(20);
  queue->enqueue(74);
  queue->enqueue(65);
  queue->enqueue(34);
  queue->enqueue(65);
  queue->enqueue(34);
  queue->dequeue();
  queue->dequeue();
  queue->dequeue();
  queue->dequeue();
  queue->dequeue();
  queue->dequeue();
  queue->dequeue();
  queue->dequeue();
  }


No comments:

Post a Comment