Skip to content

Commit

Permalink
Merge pull request #34 from m-umarjan11/umar
Browse files Browse the repository at this point in the history
Queue Data Structure
  • Loading branch information
awaissaddiqui authored Oct 20, 2024
2 parents 62663c9 + 03ccbc3 commit 4ecdfe6
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 0 deletions.
Binary file added C++/Queue/Implement-queue-using-Stack.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions C++/Queue/Implement-queue-using-Two-Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <stack>
using namespace std;
class queue{
stack<int> s1;
stack<int> s2;
public:
void Enqueue(int x){
s1.push(x);
}
int Dequeue(){
if(s1.empty() && s2.empty()){
cout<<"Error"<<endl;
}
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
int topval = s2.top();
s2.pop();
return topval;
}
bool empty(){
if(s1.empty() && s2.empty()){
return true;
}
return false;
}
};
int main(){
queue q;
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
cout<<q.Dequeue()<<endl;

cout<<q.empty()<<endl;
cout<<q.Dequeue()<<endl;
}
55 changes: 55 additions & 0 deletions C++/Queue/Implement-queue-using-one-stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <stack>
using namespace std;

class Queue {
stack<int> s1;

public:

void Enqueue(int x) {
s1.push(x);
}


int Dequeue() {
if (s1.empty()) {
cout << "Queue underflow" << endl;
return -1;
}

int x = s1.top();
s1.pop();


if (s1.empty()) {
return x;
}


int temp = Dequeue();
s1.push(x);

return temp;
}


bool empty() {
return s1.empty();
}
};

int main() {
Queue q;
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);

cout << "Dequeue: " << q.Dequeue() << endl;

cout << "Is queue empty? " << (q.empty() ? "Yes" : "No") << endl;

return 0;
}
Binary file added C++/Queue/Implement-queue-using-one-stack.exe
Binary file not shown.
51 changes: 51 additions & 0 deletions C++/Queue/Implement-stack-using-queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <queue>
using namespace std;
class stack{
int N;
queue<int> q1;
queue<int> q2;
public:
stack(){
N = 0;
}
void push(int val){
q2.push(val);
N++;
while(!q1.empty()){
q2.push(q1.front());
q1.pop();
}
queue<int> temp = q1;
q1= q2;
q2 = temp;
}
void pop(){
if (q1.empty()) {
cout << "Stack underflow" << endl;
return;
}
q1.pop();
N--;
if (q1.empty()) {
cout << "Stack is empty" << endl;
return -1;
}
return q1.front();

}
int size(){
return N;
}


};
int main(){
stack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.pop();
cout<<st.top();
}
Binary file added C++/Queue/Implement-stack-using-queue.exe
Binary file not shown.
69 changes: 69 additions & 0 deletions C++/Queue/Queue-linked-list-Implementation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data = val;
next = NULL;
}
};
class Queue{
node* front;
node* back;
public:
Queue(){
front = NULL;
back = NULL;
}

void Enqueue(int x){
node* n = new node(x);

if(front== NULL){
front=n;
back = n;
}
back->next = n;
back = n;
}
void Dequeue(){
if(front==NULL){
cout<<"no element to pop"<<endl;
return;
}
node* todelete = front;
front = front->next;
delete todelete;

}
int peek(){
if(front==NULL){
return -1;
}
return front->data;
}
bool empty(){
if(front==NULL){
return true;
}
return false;
}
};
int main(){
Queue q;
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
cout<<q.peek()<<endl;
q.Dequeue();
cout<<q.peek()<<endl;
q.Dequeue();
q.Dequeue();
q.Dequeue();
cout<<q.empty()<<endl;


}
Binary file added C++/Queue/Queue-linked-list-Implementation.exe
Binary file not shown.
49 changes: 49 additions & 0 deletions C++/Queue/Sliding window find max using dequeue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>
#include <deque>
using namespace std;

int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}

deque<int> d;
vector<int> ans;
int k = 3; // Window size

// Process the first k elements (first window)
for (int i = 0; i < k; i++) {
while (!d.empty() && a[d.back()] < a[i]) {
d.pop_back();
}
d.push_back(i);
}
ans.push_back(a[d.front()]);

// Process the remaining elements
for (int i = k; i < n; i++) {
// Remove elements not within the window
if (d.front() == i - k) {
d.pop_front();
}
// Remove elements that are smaller than the current element
while (!d.empty() && a[d.back()] < a[i]) {
d.pop_back();
}
d.push_back(i);

// The element at the front of the deque is the maximum for this window
ans.push_back(a[d.front()]);
}

// Print the results
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}

return 0;
}
Binary file not shown.
58 changes: 58 additions & 0 deletions C++/Queue/queue-array-implementation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#define n 20
using namespace std;
class queue{
int* arr;
int front;
int back;
public:
queue(){
arr = new int[n];
front = -1;
back = -1;
}
void Enqueue(int x){
if(back == n-1){
cout<<"Queue Overflow"<<endl;
return;
}
back++;
arr[back]= x;

if(front == -1){
front++;
}
}
void Dequeue(){
if(front==-1 || front>back){
cout<<"No Element in queue"<<endl;
return;
}
front++;
}
int peek(){
if(front==-1 || front>back){
cout<<"No Element in queue"<<endl;
return -1;
}
return arr[front];
}
bool empty(){
if(front==-1 || front>back){
return true;
}
return false;
}
};
int main(){
queue q;
q.Enqueue(6);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
cout<<q.peek()<<endl;
q.Dequeue();
q.Dequeue();
q.Dequeue();
cout<<q.empty();
}
Binary file added C++/Queue/queue-array-implementation.exe
Binary file not shown.

0 comments on commit 4ecdfe6

Please sign in to comment.