pqueue

 avatar
quoc14
c_cpp
5 months ago
1.3 kB
0
Indexable
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;

void swap(int &a, int &b) {
    int tmp = a;
    a = b;
    b = tmp;
    
}

int p_queue[10111];
int cur = 0;

void insert(int x) {
    cur++;
    p_queue[cur] = x;
    // max
    for (int i = 1; i <= cur; i++) {
        for (int j = 1; j <= cur-1; j++) {
            if (p_queue[j] < p_queue[j+1]) {
                swap(p_queue[j], p_queue[j+1]);
            }
        }
    }
    
    // min
    /*
    for (int i = 1; i <= cur; i++) {
        for (int j = 1; j <= cur-1; j++) {
            if (a[j] > a[j+1]) {
                swap(a[j], a[j+1]);
            }
        }
    }
    */
}

void pop() {
    
    cur--;
    for (int i = 1; i <= cur; i++) {
        p_queue[i] = p_queue[i+1];
    }
}

bool isEmpty() {
    if (cur == 0) {
        return true;
    }
    return false;
}

int top() {
    return p_queue[1];
}
void show() {
    
    for (int i = 1; i <= cur; i++) {
        
        cout << p_queue[i] << " ";
    }
    cout << endl;
}

int main() {
    // Write C++ code here
    insert(5);
    insert(2);
    insert(6);
    insert(8);
    show();
    
    pop();
    show();
    
    while (!isEmpty()) {
        
        pop();
    }
    show();
    insert(5);
    show();
    
    return 0;
}
Leave a Comment