#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
int priority;
};
Node *head = 0;
Node *tail = 0;
int size = 0;
void addFirst(int item, int prio)
{
Node *nn = new Node;
nn->data = item;
nn->priority = prio;
nn->next = head;
nn->prev = 0;
if (head == NULL)
tail = nn;
if (head != 0)
head->prev = nn;
head = nn;
size++;
}
void addLast(int item, int prio)
{
if (head == NULL)
{
addFirst(item, prio);
return;
}
Node *nn = new Node;
nn->data = item;
nn->priority = prio;
nn->next = 0;
nn->prev = tail;
tail->next = nn;
tail = nn;
size++;
}
void Insert(int prio, int item)
{
if (size == 0)
addFirst(item, prio);
else
{
Node *t = head;
while (t and t->priority <= prio)
{
t = t->next;
}
if (t == 0)
{
addLast(item, prio);
return;
}
else if (t == head)
{
addFirst(item, prio);
return;
}
Node *cur = t;
Node *prev = cur->prev;
Node *nn = new Node;
nn->priority=prio;
nn->data = item;
nn->next = cur;
nn->prev = prev;
prev->next = nn;
cur->prev = nn;
}
size++;
}
void Delete()
{
if (head == 0)
{
return;
}
Node *temp = head;
head = head->next;
if (head != NULL)
head->prev = 0;
delete temp;
size--;
}
void Print()
{
Node *t = head;
while (t)
{
cout << "Data:" << t->data << " Priority:" << t->priority << endl;
t = t->next;
}
cout << endl;
}
int main()
{
Insert(1, 1);
Insert(2, 2);
Insert(3, 3);
Insert(3, 4);
Insert(4, 5);
Insert(2,99);
Print();
Delete();
cout<<"H";
cout<<endl;
Insert(7,10);
Print();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpzdHJ1Y3QgTm9kZQp7CiAgICBpbnQgZGF0YTsKICAgIE5vZGUgKm5leHQ7CiAgICBOb2RlICpwcmV2OwogICAgaW50IHByaW9yaXR5Owp9OwpOb2RlICpoZWFkID0gMDsKTm9kZSAqdGFpbCA9IDA7CmludCBzaXplID0gMDsKdm9pZCBhZGRGaXJzdChpbnQgaXRlbSwgaW50IHByaW8pCnsKICAgIE5vZGUgKm5uID0gbmV3IE5vZGU7CiAgICBubi0+ZGF0YSA9IGl0ZW07CiAgICBubi0+cHJpb3JpdHkgPSBwcmlvOwogICAgbm4tPm5leHQgPSBoZWFkOwogICAgbm4tPnByZXYgPSAwOwogICAgaWYgKGhlYWQgPT0gTlVMTCkKICAgICAgICB0YWlsID0gbm47CiAgICBpZiAoaGVhZCAhPSAwKQogICAgICAgIGhlYWQtPnByZXYgPSBubjsKICAgIGhlYWQgPSBubjsKICAgIHNpemUrKzsKfQp2b2lkIGFkZExhc3QoaW50IGl0ZW0sIGludCBwcmlvKQp7CiAgICBpZiAoaGVhZCA9PSBOVUxMKQogICAgewogICAgICAgIGFkZEZpcnN0KGl0ZW0sIHByaW8pOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIE5vZGUgKm5uID0gbmV3IE5vZGU7CiAgICBubi0+ZGF0YSA9IGl0ZW07CiAgICBubi0+cHJpb3JpdHkgPSBwcmlvOwogICAgbm4tPm5leHQgPSAwOwogICAgbm4tPnByZXYgPSB0YWlsOwogICAgdGFpbC0+bmV4dCA9IG5uOwogICAgdGFpbCA9IG5uOwogICAgc2l6ZSsrOwp9CnZvaWQgSW5zZXJ0KGludCBwcmlvLCBpbnQgaXRlbSkKewogICAgaWYgKHNpemUgPT0gMCkKICAgICAgICBhZGRGaXJzdChpdGVtLCBwcmlvKTsKICAgIGVsc2UKICAgIHsKICAgICAgICBOb2RlICp0ID0gaGVhZDsKICAgICAgICB3aGlsZSAodCBhbmQgdC0+cHJpb3JpdHkgPD0gcHJpbykKICAgICAgICB7CiAgICAgICAgICAgIHQgPSB0LT5uZXh0OwogICAgICAgIH0KICAgICAgICBpZiAodCA9PSAwKQogICAgICAgIHsKICAgICAgICAgICAgYWRkTGFzdChpdGVtLCBwcmlvKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmICh0ID09IGhlYWQpCiAgICAgICAgewogICAgICAgICAgICBhZGRGaXJzdChpdGVtLCBwcmlvKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBOb2RlICpjdXIgPSB0OwogICAgICAgIE5vZGUgKnByZXYgPSBjdXItPnByZXY7CiAgICAgICAgTm9kZSAqbm4gPSBuZXcgTm9kZTsKICAgICAgICBubi0+cHJpb3JpdHk9cHJpbzsKICAgICAgICBubi0+ZGF0YSA9IGl0ZW07CiAgICAgICAgbm4tPm5leHQgPSBjdXI7CiAgICAgICAgbm4tPnByZXYgPSBwcmV2OwogICAgICAgIHByZXYtPm5leHQgPSBubjsKICAgICAgICBjdXItPnByZXYgPSBubjsKICAgIH0KCiAgICBzaXplKys7Cn0Kdm9pZCBEZWxldGUoKQp7CiAgICBpZiAoaGVhZCA9PSAwKQogICAgewogICAgICAgIHJldHVybjsKICAgIH0KICAgIE5vZGUgKnRlbXAgPSBoZWFkOwogICAgaGVhZCA9IGhlYWQtPm5leHQ7CiAgICBpZiAoaGVhZCAhPSBOVUxMKQogICAgICAgIGhlYWQtPnByZXYgPSAwOwogICAgZGVsZXRlIHRlbXA7CiAgICBzaXplLS07Cn0Kdm9pZCBQcmludCgpCnsKICAgIE5vZGUgKnQgPSBoZWFkOwogICAgd2hpbGUgKHQpCiAgICB7CiAgICAgICAgY291dCA8PCAiRGF0YToiIDw8IHQtPmRhdGEgPDwgIiBQcmlvcml0eToiIDw8IHQtPnByaW9yaXR5IDw8IGVuZGw7CiAgICAgICAgdCA9IHQtPm5leHQ7CiAgICB9CiAgICBjb3V0IDw8IGVuZGw7Cn0KCmludCBtYWluKCkKewogICAgSW5zZXJ0KDEsIDEpOwogICAgSW5zZXJ0KDIsIDIpOwogICAgSW5zZXJ0KDMsIDMpOwogICAgSW5zZXJ0KDMsIDQpOwogICAgSW5zZXJ0KDQsIDUpOwogICAgSW5zZXJ0KDIsOTkpOwogICAgUHJpbnQoKTsKICAgIERlbGV0ZSgpOwogICAgY291dDw8IkgiOwogICAgY291dDw8ZW5kbDsKICAgIEluc2VydCg3LDEwKTsKICAgIFByaW50KCk7CgoKICAgIHJldHVybiAwOwp9