Advertisement
skb50bd

Priority Queue

Jun 21st, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. struct Node {
  5.     int data;
  6.     int priority;
  7.     Node *next;
  8. } *Head;
  9.  
  10.  
  11. int display() {
  12.     if (Head == NULL)
  13.         printf("Queue is Empty");
  14.     else {
  15.         printf("Priority\tData\n");
  16.         for (Node *Current = Head; Current != NULL; Current = Current -> next)
  17.             printf("%d\t%d\n", Current -> priority, Current -> data);
  18.         printf("\n\n");
  19.     }
  20. }
  21.  
  22.  
  23. void insertPriority(int P, int D) {
  24.     Node *NewNode = new Node;
  25.     NewNode -> data = D;
  26.     NewNode -> priority = P;
  27.     NewNode -> next = NULL;
  28.  
  29.     if (Head == NULL)
  30.         Head = NewNode;
  31.     else {
  32.         Node *Left, *Right;
  33.         for (Right = Head; Right; Left = Right, Right = Right -> next) {
  34.             if (Right -> priority > NewNode -> priority)
  35.                 break;
  36.             else if (Right -> priority == NewNode -> priority && Right -> data > NewNode -> data)
  37.                 break;
  38.         }
  39.         if (Right == Head) {
  40.             NewNode -> next = Head;
  41.             Head = NewNode;
  42.         }
  43.         else {
  44.             Left -> next = NewNode;
  45.             NewNode -> next = Right;
  46.         }
  47.     }
  48. }
  49.  
  50. int Delete() {
  51.     if (Head == NULL)
  52.         return 0;
  53.     else {
  54.         int num = Head -> data;
  55.         Node *Current = Head;
  56.         Head = Head -> next;
  57.         free(Current);
  58.         return num;
  59.     }
  60. }
  61.  
  62.  
  63. int main() {
  64.     int choice, num, pr;
  65.     Head = NULL;
  66.  
  67.     while (1) {
  68.         printf("List Operations: \n");
  69.         printf("================\n");
  70.         printf("1. Add\n");
  71.         printf("2. Display\n");
  72.         printf("3. Delete\n");
  73.         printf("4. Exit\n");
  74.         printf("Choice: ");
  75.         scanf("%d", &choice);
  76.  
  77.         switch (choice) {
  78.             case 1: {
  79.                 printf("Enter the Priority: ");
  80.                 scanf("%d", &pr);
  81.                 printf("Enter the Number: ");
  82.                 scanf("%d", &num);
  83.                 insertPriority(pr, num);
  84.                 break;
  85.             }
  86.             case 2: {
  87.                 display();
  88.                 break;
  89.             }
  90.             case 3: {
  91.                 if (Head == NULL)
  92.                     printf("List is Empty. Nothing to Delete.\n");
  93.                 else {
  94.                     num = Delete();
  95.                     if (num != 0)
  96.                         printf("Successfully Deleted %d\n", num);
  97.                     else
  98.                         printf("%d Not Deleted.\nValue Might Not Found\n", num);
  99.                 }
  100.                 break;
  101.             }
  102.             case 4: {
  103.                 exit(0);
  104.             }
  105.             default: {
  106.                 printf("Invalid Input. Try Again\n");
  107.             }
  108.         }
  109.         printf("\n\n");
  110.     }
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement