fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. struct Node
  6. {
  7. int data;
  8. Node *next;
  9. Node *prev;
  10. int priority;
  11. };
  12. Node *head = 0;
  13. Node *tail = 0;
  14. int size = 0;
  15. void addFirst(int item, int prio)
  16. {
  17. Node *nn = new Node;
  18. nn->data = item;
  19. nn->priority = prio;
  20. nn->next = head;
  21. nn->prev = 0;
  22. if (head == NULL)
  23. tail = nn;
  24. if (head != 0)
  25. head->prev = nn;
  26. head = nn;
  27. size++;
  28. }
  29. void addLast(int item, int prio)
  30. {
  31. if (head == NULL)
  32. {
  33. addFirst(item, prio);
  34. return;
  35. }
  36. Node *nn = new Node;
  37. nn->data = item;
  38. nn->priority = prio;
  39. nn->next = 0;
  40. nn->prev = tail;
  41. tail->next = nn;
  42. tail = nn;
  43. size++;
  44. }
  45. void Insert(int prio, int item)
  46. {
  47. if (size == 0)
  48. addFirst(item, prio);
  49. else
  50. {
  51. Node *t = head;
  52. while (t and t->priority <= prio)
  53. {
  54. t = t->next;
  55. }
  56. if (t == 0)
  57. {
  58. addLast(item, prio);
  59. return;
  60. }
  61. else if (t == head)
  62. {
  63. addFirst(item, prio);
  64. return;
  65. }
  66. Node *cur = t;
  67. Node *prev = cur->prev;
  68. Node *nn = new Node;
  69. nn->priority=prio;
  70. nn->data = item;
  71. nn->next = cur;
  72. nn->prev = prev;
  73. prev->next = nn;
  74. cur->prev = nn;
  75. }
  76.  
  77. size++;
  78. }
  79. void Delete()
  80. {
  81. if (head == 0)
  82. {
  83. return;
  84. }
  85. Node *temp = head;
  86. head = head->next;
  87. if (head != NULL)
  88. head->prev = 0;
  89. delete temp;
  90. size--;
  91. }
  92. void Print()
  93. {
  94. Node *t = head;
  95. while (t)
  96. {
  97. cout << "Data:" << t->data << " Priority:" << t->priority << endl;
  98. t = t->next;
  99. }
  100. cout << endl;
  101. }
  102.  
  103. int main()
  104. {
  105. Insert(1, 1);
  106. Insert(2, 2);
  107. Insert(3, 3);
  108. Insert(3, 4);
  109. Insert(4, 5);
  110. Insert(2,99);
  111. Print();
  112. Delete();
  113. cout<<"H";
  114. cout<<endl;
  115. Insert(7,10);
  116. Print();
  117.  
  118.  
  119. return 0;
  120. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Data:1 Priority:1
Data:2 Priority:2
Data:99 Priority:2
Data:3 Priority:3
Data:4 Priority:3
Data:5 Priority:4

H
Data:2 Priority:2
Data:99 Priority:2
Data:3 Priority:3
Data:4 Priority:3
Data:5 Priority:4
Data:10 Priority:7