using namespace std;
#include<stdio.h>
#define N 5
class q{
public:
int* arr;
int front;
int rear;
q() {
arr = new int[N];
front = -1;
rear = -1;
}
int isFull() {
if((front == 0 && rear == N-1) || (front == rear + 1))
return 0;
else
return -1;
}
void enq(int x) {
if(isFull() == 0) {
printf("\nQueue Full\n");
} else if(front == -1 && rear == -1) {
front = 0;
rear = 0;
arr[rear] = x;
} else {
rear = (rear + 1) % N;
arr[rear] = x;
}
}
int isEmpty() {
if(front == -1 && rear == -1)
return 0;
else
return -1;
}
void dq() {
if(isEmpty() == 0) {
printf("\nQueue is Empty\n");
} else if(front == rear) {
printf("\nDeleted element %d\n", arr[rear]);
front = -1;
rear = -1;
} else {
printf("\nDeleted Element %d\n", arr[front]);
front = (front + 1) % N;
}
}
};
int main() {
q ob;
if( ob.isEmpty() == 0)
printf("isEmpty = Yes");
else
printf("isEmpty = No");
ob.dq();
ob.enq(1);
ob.enq(2);
ob.enq(3);
ob.enq(4);
ob.enq(5);
ob.dq();
ob.enq(6);
ob.dq();
ob.dq();
ob.dq();
ob.dq();
ob.dq();
printf("\nFront and Rear After All Del %d and %d", ob.front, ob.rear);
printf("\n");
for(int i = 0; i<=4; i++) {
printf("%d \t", ob.arr[i]);
}
}
Wednesday, December 8, 2021
Circular Queue Insertion Deletion
Subscribe to:
Post Comments (Atom)
Priority queue deleting elements after ascending inputs Gaand Fadd
using namespace std; #include<iostream> #define N 5 class pq { public: int* arr; int front; int rear; pq() { ...
-
using namespace std; #include<stdio.h> #define N 5 class q{ public: int* arr; int front; int rear; q() { ...
-
#include<stdio.h> int fnCmp(char* str1, char* str2) { while( (*str1 != '\0' && *str2 != '\0') && ...
-
#include<stdio.h> void fncpy(char* str2, char* str1) { int i = 0; while(*str1 != '\0') { str2[i] = *str1; ...
No comments:
Post a Comment