-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from m-umarjan11/umar
Queue Data Structure
- Loading branch information
Showing
12 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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
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; | ||
} |
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
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 not shown.
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
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 not shown.
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
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 not shown.
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
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.
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
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 not shown.