Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <cstdlib>
- using namespace std;
- typedef struct kolejka{
- int wartosc;
- int priorytet;
- struct kolejka *next;
- }LIFO;
- kolejka *head;
- void dodajElement(int value, kolejka *head, int priority) {
- kolejka *current;
- if (head->next == NULL) {
- current = (kolejka *) malloc(sizeof(struct kolejka));
- current->wartosc = value;
- current->next = NULL;
- current->priorytet = priority;
- head->next = current;
- } else {
- current = (kolejka *) malloc(sizeof(struct kolejka));
- current->priorytet = priority;
- current->wartosc = value;
- if (head->next->priorytet < priority) {//dodawanie na poczatku
- current->next = head->next;
- head->next = current;
- } else {
- kolejka *temp;
- temp = head;
- while ( temp->next!=NULL && temp->next->priorytet >= priority ) temp = temp->next;
- current->next = temp->next;
- temp->next = current;
- }
- }
- }
- void wykonanie(kolejka *head, int priority){
- kolejka *current;
- if(head->next==NULL);
- else{
- current=head;
- current=head->next;
- cout << current->wartosc;
- head->next = current->next;
- }
- }
- int main() {
- head = (kolejka*)malloc(sizeof(struct kolejka));
- head->next=NULL;
- head->wartosc=-1;
- short int sterowanieNowy;
- int wartoscNowy, priority;
- while(sterowanieNowy!=-1){
- cin >> sterowanieNowy;
- if(sterowanieNowy==1){
- wykonanie(head, priority);
- }
- if(sterowanieNowy==0){
- cin >> wartoscNowy;
- cin >> priority;
- dodajElement(wartoscNowy, head, priority);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement