Skip to content

N-queue problem by Love Babbar #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions Stacks&Queues/N_queues.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include<bits/stdc++.h>
using namespace std;

class kQueue{

int n;
int k;
int*arr;
int *front;
int *rear;
int *next;
int freespot;

public:
kQueue(int n, int k){
this-> n = n;
this-> k = k;
front = new int[k];
rear = new int[k];

for(int i=0;i<k;i++){
front[i] = rear[i] = -1;
}

next = new int[n];

for(int i=0;i<n;i++){
next[i] = i+1;
}
next[n-1] = -1;

arr = new int[n];
freespot = 0;
}

void enqueue(int data, int qn){
// Step 1: Check for Overflow
if(freespot == -1){
cout<<"Queue Overflow"<<endl;
return;
}

//if can push, find first free index
int index = freespot;

//update freespot
freespot = next[index];

//check whether pushing first element in the kth queue:
if(front[qn-1] == -1){
front[qn-1] = index;
}else{
// link new element to the previous element
next[rear[qn-1]] = index;
}

next[index] = -1;

rear[qn-1] = index;

arr[index] = data;
}

int dequeue(int qn){
// Check if empty:
if(front[qn-1]==-1){
cout<<"Queue is empty"<<endl;
return -1;
}

// index to pop
int index = front[qn-1];

// front ko aage badhao
front[qn-1] = next[index];

// freespot ko manage
next[index] = freespot;
freespot = index;

return arr[index];
}

};

int main(){
kQueue q(10,3);
q.enqueue(10,1);
q.enqueue(15,1);
q.enqueue(20,2);
q.enqueue(25,1);

cout<<q.dequeue(1)<<endl;
cout<<q.dequeue(2)<<endl;
cout<<q.dequeue(1)<<endl;
cout<<q.dequeue(1)<<endl;
return 0;
}