using namespace std;
#include<iostream>
#define N 20
class Queue {
public:
int* arr;
int front;
int back;
Queue() {
arr = new int[N];
front = -1;
back = -1;
}
//functions to be made push(); peak(); pop(); empty(); Queue(); ~Queue();
void push(int x) {
if (back == N - 1) {
cout << "Stack Overflowed" << endl;
return;
}
back++;
arr[back] = x;
if (front == -1) {
front++;
}
}
void pop() {
if (front == -1 || front > back) {
cout << "Stack UnderFlow" << endl;
return;
}
front++;
}
int peek() {
if (front == -1 || front > back) {
cout << "Stack is Empty" << endl;
return -1;
}
return arr[front];
}
bool empty() {
if (front == -1 || front > back) {
return true;
}
return false;
}
~Queue() {
delete[] arr;
cout << "Deallocated All the Resources to Computer Again@" << endl;
}
};
int main() {
Queue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout << q.peek() << endl;
q.pop();
cout << q.peek() << endl;
q.pop();
cout << q.peek() << endl;
q.pop();
cout << q.empty() << endl;
system("pause > 0");
return 0;
}
Monday, November 1, 2021
Queue Data Structure
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