Created
December 10, 2021 00:03
-
-
Save shivajichalise/342f1dcd9e47d7f1cb46a7aab8fbf17a to your computer and use it in GitHub Desktop.
Implementing Queue in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class CircularQueue{ | |
private: | |
int front; | |
int rear; | |
int arr[5]; | |
int itemCount; | |
public: | |
CircularQueue(){ | |
itemCount = 0; | |
front = -1; | |
rear = -1; | |
for(int i = 0; i<5; i++){ | |
arr[i] = 0; | |
} | |
} | |
bool isEmpty(){ | |
if(front == -1 && rear == -1){ | |
return true; | |
} | |
return false; | |
} | |
bool isFull(){ | |
if(((rear+1)%5) == front){ | |
return true; | |
} | |
return false; | |
} | |
void enqueue(int item){ | |
if(isFull()){ | |
cout << "Queue is full" << endl ; | |
return; | |
}else if(isEmpty()){ | |
rear = 0; | |
front = 0; | |
}else{ | |
rear = (rear+1)%5; | |
} | |
arr[rear] = item; | |
itemCount++; | |
} | |
int dequeue(){ | |
int temp; | |
if(isEmpty()){ | |
cout << "Queue is empty" << endl; | |
return 0; | |
}else if(front == rear){ | |
temp = arr[front]; | |
arr[front] = 0; | |
rear = -1; | |
front = -1; | |
}else{ | |
temp = arr[front]; | |
arr[front] = 0; | |
front = (front+1)%5; | |
} | |
itemCount--; | |
return temp; | |
} | |
int count(){ | |
return (itemCount); | |
} | |
}; | |
int main(){ | |
CircularQueue q1; | |
cout << "Queue is " << q1.count() << " long" << endl; | |
q1.enqueue(6); | |
q1.enqueue(9); | |
q1.enqueue(6); | |
cout << "Queue is " << q1.count() << " long" << endl; | |
q1.dequeue(); | |
cout << "Queue is " << q1.count() << " long" << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Queue{ | |
private: | |
int front; | |
int rear; | |
int arr[5]; | |
public: | |
Queue(){ | |
front = -1; | |
rear = -1; | |
for(int i = 0; i<5; i++){ | |
arr[i] = 0; | |
} | |
} | |
bool isEmpty(){ | |
if(front == -1 && rear == -1){ | |
return true; | |
} | |
return false; | |
} | |
bool isFull(){ | |
if(rear == 4){ | |
return true; | |
} | |
return false; | |
} | |
void enqueue(int item){ | |
if(isFull()){ | |
cout << "Queue is full" << endl ; | |
return; | |
}else if(isEmpty()){ | |
rear = 0; | |
front = 0; | |
}else{ | |
rear++; | |
} | |
arr[rear] = item; | |
} | |
int dequeue(){ | |
int temp; | |
if(isEmpty()){ | |
cout << "Queue is empty" << endl; | |
return 0; | |
}else if(front == rear){ | |
temp = arr[front]; | |
arr[front] = 0; | |
rear = -1; | |
front = -1; | |
}else{ | |
temp = arr[front]; | |
arr[front] = 0; | |
front++; | |
} | |
return temp; | |
} | |
int count(){ | |
return (rear-front+1); | |
} | |
}; | |
int main(){ | |
Queue q1; | |
cout << "Queue is " << q1.count() << " long" << endl; | |
q1.enqueue(6); | |
q1.enqueue(9); | |
q1.enqueue(6); | |
cout << "Queue is " << q1.count() << " long" << endl; | |
q1.dequeue(); | |
cout << "Queue is " << q1.count() << " long" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment