Queue

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

__________________________________________________________________________________I

mplementation

____________________________________________________linear queue

https://www.codingninjas.com/codestudio/problems/queue-using-array-or-singly-
linked-list_2099908?
leftPanelTab=0&campaign=LoveBabbar_Codestudio&utm_source=youtube&utm_medium=affilia
te&utm_campaign=LoveBabbar_Codestudio

#include <bits/stdc++.h>
class Queue {
int* arr;
int qfront;
int rear;
int size;
public:
Queue() {
// Implement the Constructor
size = 100001;
arr = new int[size];
qfront = 0;
rear = 0;
}

/*----------------- Public Functions of Queue -----------------*/

bool isEmpty() {
if(rear == qfront){
return true;
}
else{
return false;
}
}

void enqueue(int data) {


// Implement the enqueue() function
if(rear == size){
return;
}
else{
arr[rear] = data;
rear++;
}
}

int dequeue() {
if(qfront == rear){
return -1;
}
else{
int ans = arr[qfront];
arr[qfront] = -1;
qfront++;
if(qfront == rear){
qfront = 0;
rear = 0;
}
return ans;
}
}

int front() {
if(rear == qfront){
return -1;
}
else{
return arr[qfront];
}
}
};

________________________________________________________circular queue

https://www.codingninjas.com/codestudio/problems/circular-queue_1170058?
leftPanelTab=1&campaign=LoveBabbar_Codestudio&utm_source=youtube&utm_medium=affilia
te&utm_campaign=LoveBabbar_Codestudio

#include <bits/stdc++.h>
class CircularQueue{
public:
// Initialize your data structure.
int* arr;
int front;
int rear;
int size;
CircularQueue(int n){
// Write your code here.
size = n;
arr = new int[size];
front = rear =-1;
}

// Enqueues 'X' into the queue. Returns true if it gets pushed into the stack,
and false otherwise.
bool enqueue(int value) {
// to check whether queue is full.
if (front == 0 && rear == size - 1){
return false;
}
// to check whether queue is full.
else if(rear == (front-1)%(size-1)){
return false;
}
else if(front == -1){ // first element to push
front = rear = 0;
arr[rear] = value;
}
else if(rear == size-1 && front!=0){ //to maintain cyclic nature
rear = 0;
arr[rear] = value;
}
else{

rear++;
arr[rear] = value;
}
return true;

// Dequeues top element from queue. Returns -1 if the stack is empty, otherwise
returns the popped element.
int dequeue(){
if(front == -1 && rear == -1){ // to check queue is enpty
return -1;
}
else if(front==rear){ // single element is present
int ans = arr[front];
front = rear =-1;
return ans;

}
else if(front == size-1){ // to maintain cyclic nature
int ans = arr[front];
front = 0;
return ans;
}
else{
int ans = arr[front];
front++;
return ans;
}

}
};

_____________________________________________________________________________Q.53)
Queqe Reversal

https://practice.geeksforgeeks.org/problems/queue-reversal/1

void reverse(queue<int> &q){


// base case
if(q.empty()){
return;
}

int a = q.front();
q.pop();

reverse(q);

q.push(a);

return;
}

queue<int> rev(queue<int> q)
{
reverse(q);
return q;
}

_____________________________________________________________________________Q.54)
First negative integer in every window of size k

https://practice.geeksforgeeks.org/problems/first-negative-integer-in-every-window-
of-size-k3345/1

vector<long long> printFirstNegativeInteger(long long int A[],


long long int N, long long int K) {

deque< long long> dq;

vector<long long> ans;

//for first k element


for(int i=0; i<K; i++){
if(A[i]<0){
dq.push_back(i);
}
}

//store ans for first k window


if(dq.size()>0){
ans.push_back(A[dq.front()]);
}
else{
ans.push_back(0);
}

//process for remaining window


for(int i=K; i<N; i++){
//removal
if(!dq.empty() && i-dq.front()>=K){
dq.pop_front();
}
//addition
if(A[i]<0){
dq.push_back(i);
}

//ans store
if(dq.size()>0){
ans.push_back(A[dq.front()]);
}
else{
ans.push_back(0);
}
}
return ans;

___________________________________________________________________________________
_Q.55) Reverse First K elements of Queue

https://practice.geeksforgeeks.org/problems/reverse-first-k-elements-of-queue/1

queue<int> modifyQueue(queue<int> q, int k) {


// step 1
int n = q.size();
stack<int> s;
for(int i=0; i<k; i++){
int a = q.front();
s.push(a);
q.pop();
}

while(!s.empty()){
int a = s.top();
q.push(a);
s.pop();
}

for(int i=0; i<n-k; i++){


int a = q.front();
q.pop();
q.push(a);
}
return q;
}
___________________________________________________________________________Q.56)
First non-repeating character in a stream

https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-
stream1216/1

string FirstNonRepeating(string A){


unordered_map<char,int> count;
queue<char> q;
string ans;

for(int i=0; i<A.size(); i++){


//increase count
count[A[i]]++;

//queue me push
q.push(A[i]);

while(!q.empty()){
if(count[q.front()]>1){
//repeating character
q.pop();
}
else{
//non repeating character mil gya
ans.push_back(q.front());
break;
}
}
if(q.empty()){
ans.push_back('#');
}
}
return ans;
}

______________________________________________________________________________Q.57)
Circular tour
https://practice.geeksforgeeks.org/problems/circular-tour-1587115620/1

int tour(petrolPump p[],int n)


{
int balance = 0;
int deficit = 0;
int start = 0;

for(int i=0; i<n; i++){


balance += p[i].petrol - p[i].distance;
if(balance<0){
deficit += balance;
start = i+1;
balance = 0;
}
}
if(deficit + balance >=0){
return start;
}
else{
return -1;
}
}

You might also like