|
| 1 | +/* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */ |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#define NULL 0 |
| 6 | + |
| 7 | +struct node |
| 8 | +{ |
| 9 | + int data ; |
| 10 | + struct node *next ; |
| 11 | +} ; |
| 12 | + |
| 13 | +struct node *front , *rear ; |
| 14 | + |
| 15 | +/* This function initializes the queue to empty by making both front and rear as NULL */ |
| 16 | +void createqueue() |
| 17 | +{ |
| 18 | + front=rear=NULL ; |
| 19 | +} |
| 20 | + |
| 21 | +int empty() |
| 22 | +{ |
| 23 | + if(front==NULL) |
| 24 | + return 1 ; |
| 25 | + else |
| 26 | + return 0 ; |
| 27 | +} |
| 28 | + |
| 29 | +void insert(int x) |
| 30 | +{ |
| 31 | + struct node *pnode ; |
| 32 | + |
| 33 | + pnode=(struct node*)malloc(sizeof(struct node)) ; |
| 34 | + if(pnode==NULL) |
| 35 | + { |
| 36 | + printf("Memory overflow. Unable to insert.\n") ; |
| 37 | + getch(); |
| 38 | + exit(1) ; |
| 39 | + } |
| 40 | + |
| 41 | + pnode->data=x ; |
| 42 | + pnode->next=NULL; /* New node is always last node */ |
| 43 | + |
| 44 | + if(empty()) |
| 45 | + front=rear=pnode ; |
| 46 | + else |
| 47 | + { |
| 48 | + rear->next=pnode ; |
| 49 | + rear=pnode ; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +int removes() |
| 54 | +{ |
| 55 | + int min ; |
| 56 | + struct node *follow , *follow1 , *p , *p1 ; |
| 57 | + |
| 58 | + if(empty()) |
| 59 | + { |
| 60 | + printf("\nQueue Underflow. Unable to remove.") ; |
| 61 | + getch() ; |
| 62 | + exit(1) ; |
| 63 | + } |
| 64 | + |
| 65 | + /* finding the node with minimum value in the APQ.*/ |
| 66 | + p=p1=front ; |
| 67 | + follow=follow1=NULL ; |
| 68 | + min=front->data ; |
| 69 | + while(p!=NULL) |
| 70 | + { |
| 71 | + if(p->data<min) |
| 72 | + { |
| 73 | + min=p->data ; |
| 74 | + follow1=follow ; |
| 75 | + p1=p ; |
| 76 | + } |
| 77 | + follow=p ; |
| 78 | + p=p->next ; |
| 79 | + } |
| 80 | + |
| 81 | + /* Deleting the node with min value */ |
| 82 | + |
| 83 | + if(p1==front) /* deleting first node.*/ |
| 84 | + { |
| 85 | + front=front->next ; |
| 86 | + if(front==NULL) /* Deleting the only one node */ |
| 87 | + rear=NULL ; |
| 88 | + } |
| 89 | + else if(p1==rear) /* Deleting last node */ |
| 90 | + { |
| 91 | + rear=follow1 ; |
| 92 | + rear->next=NULL ; |
| 93 | + } |
| 94 | + else /* deleting any other node.*/ |
| 95 | + follow1->next=p1->next ; |
| 96 | + |
| 97 | + |
| 98 | + free(p1) ; |
| 99 | + return min ; /* DONT FORGET LAST 2 STATEMENTS.*/ |
| 100 | +} |
| 101 | + |
| 102 | +void show() |
| 103 | +{ |
| 104 | + struct node *p ; |
| 105 | + |
| 106 | + if(empty()) |
| 107 | + printf("Queue empty. No data to display \n") ; |
| 108 | + else |
| 109 | + { |
| 110 | + printf("Queue from front to rear is as shown: \n") ; |
| 111 | + |
| 112 | + p=front ; |
| 113 | + while(p!=NULL) |
| 114 | + { |
| 115 | + printf("%d ",p->data) ; |
| 116 | + p=p->next ; |
| 117 | + } |
| 118 | + |
| 119 | + printf("\n") ; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void destroyqueue() |
| 124 | +{ |
| 125 | + front=rear=NULL ; |
| 126 | +} |
| 127 | + |
| 128 | +int main() |
| 129 | +{ |
| 130 | + int x , ch ; |
| 131 | + |
| 132 | + createqueue() ; |
| 133 | + |
| 134 | + |
| 135 | + do |
| 136 | + { |
| 137 | + printf("\n\n Menu: \n") ; |
| 138 | + printf("1:Insert \n") ; |
| 139 | + printf("2:Remove \n") ; |
| 140 | + printf("3:exit \n") ; |
| 141 | + printf("Enter your choice: ") ; |
| 142 | + scanf("%d",&ch) ; |
| 143 | + |
| 144 | + switch(ch) |
| 145 | + { |
| 146 | + case 1: |
| 147 | + printf("Enter element to be inserted: ") ; |
| 148 | + scanf("%d",&x) ; |
| 149 | + insert(x) ; |
| 150 | + show() ; |
| 151 | + break ; |
| 152 | + |
| 153 | + case 2: |
| 154 | + x=removes() ; |
| 155 | + printf("Element removed is: %d\n",x) ; |
| 156 | + show() ; |
| 157 | + break ; |
| 158 | + |
| 159 | + case 3: |
| 160 | + break ; |
| 161 | + } |
| 162 | + } |
| 163 | + while(ch!=3) ; |
| 164 | + |
| 165 | + destroyqueue() ; |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
0 commit comments