Skip to content

Instantly share code, notes, and snippets.

@mycodeschool
Last active May 16, 2025 08:46
Show Gist options
  • Save mycodeschool/7331785 to your computer and use it in GitHub Desktop.
Save mycodeschool/7331785 to your computer and use it in GitHub Desktop.
Queue - Array Implementation
/* Queue - Circular Array implementation in C++*/
#include<iostream>
using namespace std;
#define MAX_SIZE 101 //maximum size of the array that will store Queue.
// Creating a class named Queue.
class Queue
{
private:
int A[MAX_SIZE];
int front, rear;
public:
// Constructor - set front and rear as -1.
// We are assuming that for an empty Queue, both front and rear will be -1.
Queue()
{
front = -1;
rear = -1;
}
// To check wheter Queue is empty or not
bool IsEmpty()
{
return (front == -1 && rear == -1);
}
// To check whether Queue is full or not
bool IsFull()
{
return (rear+1)%MAX_SIZE == front ? true : false;
}
// Inserts an element in queue at rear end
void Enqueue(int x)
{
cout<<"Enqueuing "<<x<<" \n";
if(IsFull())
{
cout<<"Error: Queue is Full\n";
return;
}
if (IsEmpty())
{
front = rear = 0;
}
else
{
rear = (rear+1)%MAX_SIZE;
}
A[rear] = x;
}
// Removes an element in Queue from front end.
void Dequeue()
{
cout<<"Dequeuing \n";
if(IsEmpty())
{
cout<<"Error: Queue is Empty\n";
return;
}
else if(front == rear )
{
rear = front = -1;
}
else
{
front = (front+1)%MAX_SIZE;
}
}
// Returns element at front of queue.
int Front()
{
if(front == -1)
{
cout<<"Error: cannot return front from empty queue\n";
return -1;
}
return A[front];
}
/*
Printing the elements in queue from front to rear.
This function is only to test the code.
This is not a standard function for Queue implementation.
*/
void Print()
{
// Finding number of elements in queue
int count = (rear+MAX_SIZE-front)%MAX_SIZE + 1;
cout<<"Queue : ";
for(int i = 0; i <count; i++)
{
int index = (front+i) % MAX_SIZE; // Index of element while travesing circularly from front
cout<<A[index]<<" ";
}
cout<<"\n\n";
}
};
int main()
{
/*Driver Code to test the implementation
Printing the elements in Queue after each Enqueue or Dequeue
*/
Queue Q; // creating an instance of Queue.
Q.Enqueue(2); Q.Print();
Q.Enqueue(4); Q.Print();
Q.Enqueue(6); Q.Print();
Q.Dequeue(); Q.Print();
Q.Enqueue(8); Q.Print();
}
@FFcodelab
Copy link

include <stdio.h>

include <stdlib.h>

include <string.h>

define MAX 3

int arr[MAX]; int front, rear;

bool isEmpty(){ return (front == -1 && rear == -1) ? true : false; }

bool isFull(){ return (rear+1)%MAX == front ? true : false; }

void enQueue(int x){ if(isFull()){ printf("queue is full\n"); return; } if(isEmpty()) front = rear = 0; else rear = (rear+1)%MAX;

arr[rear] = x;

} void deQueue(){ if(isEmpty()){ printf("queue is empty\n"); return; } else if(front == rear) front = rear = -1; else front = (front+1)%MAX;

}

void Print(){ int length = (rear + MAX - front)%MAX + 1; int i; for( i = 0; i<length;i++){ printf("%d ", arr[(front+i)%MAX]); } printf("\n"); }

int main(){ front = -1; rear = -1; enQueue(2); Print(); enQueue(4); Print(); enQueue(6); Print(); deQueue(); Print(); enQueue(10); Print(); return 0; }

这是C吧

@Aatrox00
Copy link

Aatrox00 commented May 16, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment