Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- typedef struct list
- {
- struct list *next;
- int value;
- int priority;
- }list;
- struct list *head;
- int size(list *head)
- {
- int s=0;
- list *curr=head;
- while(curr!=NULL)
- {
- curr=curr->next;
- s++;
- }
- return s;
- }
- int max(list *head)
- {
- list *curr, * pmax;
- int n;
- pmax = head;
- if(head)
- for(curr = head->next; curr; curr = curr->next)
- {
- if(curr->priority > pmax->priority)
- pmax = curr;
- }
- n=pmax->priority;
- return n;
- }
- struct list *free(list *head)
- {
- list *curr=head;
- list *help=head;
- while(curr->next->priority!=max(head))
- {
- curr=curr->next;
- }
- help=curr;
- curr=curr->next;
- help->next=curr->next;
- delete curr;
- return head;
- }
- struct list *add(list *head,int value, int priority)
- {
- list *curr=head, *nowy_element;
- nowy_element = (list*) malloc(sizeof(list));
- if(size(head)==0)
- {
- head=nowy_element;
- nowy_element->value=value;
- nowy_element->priority=priority;
- nowy_element->next=NULL;
- return nowy_element;
- }
- while(curr->next!=NULL)
- {
- curr=curr->next;
- }
- nowy_element->next=NULL;
- curr->next=nowy_element;
- nowy_element->value=value;
- nowy_element->priority=priority;
- return head;
- }
- void wypisywanie(list *head)
- {
- list *curr=head;
- while(curr->priority!=max(head))
- {
- curr=curr->next;
- }
- cout<<curr->value;
- }
- int main ()
- {
- head=NULL;
- int wybor,wartosc,priorytet;
- do
- {
- cin>>wybor;
- if(wybor==0)
- {
- cin>>wartosc;
- cin>>priorytet;
- head=add(head,wartosc,priorytet);
- } else if(wybor==1)
- {
- if(size(head)!=0)
- {
- wypisywanie(head);
- head=free(head);
- }
- }
- } while (wybor!=-1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement