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
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() { ...
- 
//See sabse pehle ek khali node bana lena jiska prev and next null ho uske bad use return kar le fir saare operation perform kar lena //Tim...
- 
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void add(struct node** tail, int val...
- 
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void inElFr(struct node** head, int ...
 
No comments:
Post a Comment