forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ShreyaDhir#164 from Shreya2221/master
Ascending priority queue
- Loading branch information
Showing
1 changed file
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define MAX 5 | ||
|
||
void insert_by_priority(int); | ||
void delete_by_priority(int); | ||
void create(); | ||
void check(int); | ||
void display_pqueue(); | ||
|
||
int pri_que[MAX]; | ||
int front, rear; | ||
|
||
void main() | ||
{ | ||
int n, ch; | ||
|
||
printf("\n1 - Insert an element into queue"); | ||
printf("\n2 - Delete an element from queue"); | ||
printf("\n3 - Display queue elements"); | ||
printf("\n4 - Exit"); | ||
|
||
create(); | ||
|
||
while (1) | ||
{ | ||
printf("\nEnter your choice : "); | ||
scanf("%d", &ch); | ||
|
||
switch (ch) | ||
{ | ||
case 1: | ||
printf("\nEnter value to be inserted : "); | ||
scanf("%d",&n); | ||
insert_by_priority(n); | ||
break; | ||
case 2: | ||
printf("\nEnter value to delete : "); | ||
scanf("%d",&n); | ||
delete_by_priority(n); | ||
break; | ||
case 3: | ||
display_pqueue(); | ||
break; | ||
case 4: | ||
exit(0); | ||
default: | ||
printf("\nChoice is incorrect, Enter a correct choice"); | ||
} | ||
} | ||
} | ||
|
||
void create() | ||
{ | ||
front = rear = -1; | ||
} | ||
|
||
void insert_by_priority(int data) | ||
{ | ||
if (rear >= MAX - 1) | ||
{ | ||
printf("\nQueue overflow no more elements can be inserted"); | ||
return; | ||
} | ||
if ((front == -1) && (rear == -1)) | ||
{ | ||
front++; | ||
rear++; | ||
pri_que[rear] = data; | ||
return; | ||
} | ||
else | ||
check(data); | ||
rear++; | ||
} | ||
|
||
void check(int data) | ||
{ | ||
int i,j; | ||
|
||
for (i = 0; i <= rear; i++) | ||
{ | ||
if (data >= pri_que[i]) | ||
{ | ||
for (j = rear + 1; j > i; j--) | ||
{ | ||
pri_que[j] = pri_que[j - 1]; | ||
} | ||
pri_que[i] = data; | ||
return; | ||
} | ||
} | ||
pri_que[i] = data; | ||
} | ||
|
||
void delete_by_priority(int data) | ||
{ | ||
int i; | ||
|
||
if ((front==-1) && (rear==-1)) | ||
{ | ||
printf("\nQueue is empty no elements to delete"); | ||
return; | ||
} | ||
|
||
for (i = 0; i <= rear; i++) | ||
{ | ||
if (data == pri_que[i]) | ||
{ | ||
for (; i < rear; i++) | ||
{ | ||
pri_que[i] = pri_que[i + 1]; | ||
} | ||
|
||
pri_que[i] = -99; | ||
rear--; | ||
|
||
if (rear == -1) | ||
front = -1; | ||
return; | ||
} | ||
} | ||
printf("\n%d not found in queue to delete", data); | ||
} | ||
|
||
void display_pqueue() | ||
{ | ||
if ((front == -1) && (rear == -1)) | ||
{ | ||
printf("\nQueue is empty"); | ||
return; | ||
} | ||
|
||
for (; front <= rear; front++) | ||
{ | ||
printf(" %d ", pri_que[front]); | ||
} | ||
|
||
front = 0; | ||
} |