using namespace std;
#include<iostream>
#define N 5
class pq {
public:
int* arr;
int front;
int rear;
pq() {
front = -1;
rear = -1;
arr = new int[N];
}
int isEmpty() {
if((front == -1 && rear == -1) || (front > rear))
return 1;
else
return -1;
}
int isFull() {
if(rear == N -1)
return 1;
else
return -1;
}
void ins(int elt) {
if(isFull() == 1){
cout<<"Full"<<endl;
}else if((front == -1) && (rear == -1)){
front++;
rear++;
arr[rear] = elt;
} else {
if(arr[rear] > elt){
arr[rear + 1] = arr[rear];
arr[rear] = elt;
int current = rear;
rear++;
while(current!=front){
if(arr[current -1] > arr[current]){
int temp;
temp = arr[current -1];
arr[current -1] = elt;
arr[current] = temp;
}
current = current -1;
}
} else {
rear++;
arr[rear] = elt;
}
}
}
void del() {
if(isEmpty() == 1){
cout<<"Empty"<<endl;
} else {
front++;
}
}
void show() {
cout<<"\n____________________"<<endl;
for(int i = 0; i<N; i++){
cout<<arr[i];
}
}
};
int main() {
pq ob;
if(ob.isEmpty() == 1)
cout<<"isEmpty == Yes";
else
cout<<"isEmpty == NO";
ob.ins(4);
ob.ins(2);
ob.ins(1);
ob.ins(3);
ob.ins(5);
ob.show();
cout<<"xxx"<<ob.rear<<endl;
return true;
}
Wednesday, December 8, 2021
Priority queue deleting elements after ascending inputs Gaand Fadd
Binary Search Algorithm
#include<stdio.h>
int show(int* a, int n, int data) {
int l = 0;
int r = n - 1;
int mid ;
while(l <= r) {
mid = ( l + r )/2;
if(data == a[mid]) {
return mid;
break;
} else {
if(data < a[mid]){
r = mid -1;
} else {
l = mid + 1;
}
}
}
return -1;
}
int main() {
int a[10] = {5, 9, 17, 23, 25, 45, 59, 63, 71, 89};
int* arr = a;
int key = 59;
int x = show(arr, 10, key);
if(x == -1)
printf("Data not found");
else
printf("%d", x);
return 0;
}
Circular Queue Insertion Deletion
using namespace std;
#include<stdio.h>
#define N 5
class q{
public:
int* arr;
int front;
int rear;
q() {
arr = new int[N];
front = -1;
rear = -1;
}
int isFull() {
if((front == 0 && rear == N-1) || (front == rear + 1))
return 0;
else
return -1;
}
void enq(int x) {
if(isFull() == 0) {
printf("\nQueue Full\n");
} else if(front == -1 && rear == -1) {
front = 0;
rear = 0;
arr[rear] = x;
} else {
rear = (rear + 1) % N;
arr[rear] = x;
}
}
int isEmpty() {
if(front == -1 && rear == -1)
return 0;
else
return -1;
}
void dq() {
if(isEmpty() == 0) {
printf("\nQueue is Empty\n");
} else if(front == rear) {
printf("\nDeleted element %d\n", arr[rear]);
front = -1;
rear = -1;
} else {
printf("\nDeleted Element %d\n", arr[front]);
front = (front + 1) % N;
}
}
};
int main() {
q ob;
if( ob.isEmpty() == 0)
printf("isEmpty = Yes");
else
printf("isEmpty = No");
ob.dq();
ob.enq(1);
ob.enq(2);
ob.enq(3);
ob.enq(4);
ob.enq(5);
ob.dq();
ob.enq(6);
ob.dq();
ob.dq();
ob.dq();
ob.dq();
ob.dq();
printf("\nFront and Rear After All Del %d and %d", ob.front, ob.rear);
printf("\n");
for(int i = 0; i<=4; i++) {
printf("%d \t", ob.arr[i]);
}
}
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; ...