Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define MAX 5
- struct queue {
- int priority;
- int data;
- struct queue *next;
- };
- queue *priorityQueue = NULL;
- int queueSize = 0;
- bool isFull() {
- return queueSize == MAX;
- }
- bool isEmpty() {
- return priorityQueue == NULL;
- }
- void push(int i, int p) {
- if (!isFull()) {
- queue *t, *q;
- t = new queue;
- t->data = i;
- t->priority = p;
- if (isEmpty() || p < priorityQueue->priority) {
- t->next = priorityQueue;
- priorityQueue = t;
- }
- else {
- q = priorityQueue;
- while (q->next != NULL && q->next->priority <= p)
- q = q->next;
- t->next = q->next;
- q->next = t;
- }
- queueSize++;
- }
- else {
- cout << "Kolejka jest pelna" << endl;
- }
- }
- void pop() {
- queue *t;
- if (isEmpty())
- cout << "Kolejka jest pusta" << endl;
- else {
- t = priorityQueue;
- cout << "Usuwany element to : " << t->data << endl;
- priorityQueue = priorityQueue->next;
- free(t);
- queueSize--;
- }
- }
- void show() {
- if (isEmpty())
- cout << "Kolejka jest pusta" << endl;
- else {
- cout << "Kolejka:" << endl;
- cout << "Priorytet | Liczba" << endl;
- while (!isEmpty()) {
- cout << priorityQueue->priority << " | " << priorityQueue->data << endl;
- priorityQueue = priorityQueue->next;
- }
- }
- }
- void peek() {
- cout << "Pierwszy element: " << priorityQueue->priority << " " << priorityQueue->data << endl;
- }
- int main() {
- int choice, value, priority;
- do {
- cout << endl << "1) Wprowadz element do kolejki" << endl;
- cout << "2) Usun element z kolejki" << endl;
- cout << "3) Wyswietl elementy kolejki" << endl;
- cout << "4) Wyswietl pierwszy element kolejki" << endl;
- cout << "5) Wyjscie" << endl;
- cout << "Wybierz opcje : ";
- cin >> choice;
- switch (choice) {
- case 1:
- cout << "Wprowadz liczbe : ";
- cin >> value;
- cout << "Wprowadz priorytet (mniej znaczy lepiej) : ";
- cin >> priority;
- push(value, priority);
- break;
- case 2:
- pop();
- break;
- case 3:
- show();
- break;
- case 4:
- peek();
- break;
- case 5:
- cout << "Wyjscie" << endl;
- break;
- default:
- cout << "Zly wybor" << endl;
- }
- } while (choice != 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement