C Program to implement Priority Queue using structure.

Data structures using C,
Write a C Program to implement Priority Queue using structure.
Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple ascending order priority queue using the structure, here items are inserted in ascending order. Structure is a c composite data type, in which we can define all the data types under the same name or object.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#define SIZE 5 /* Size of Queue */
int f=0,r=-1; /* Global declarations */
typedef struct PRQ
{
int ele;
int pr;
}PriorityQ;

PriorityQ PQ[SIZE];

PQinsert(int elem, int pre)
{
int i; /* Function for Insert operation */
if( Qfull()) printf("nn Overflow!!!!nn");
else
{
i=r;
++r;
while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1].ele=elem;
PQ[i+1].pr=pre;
}
}

PriorityQ PQdelete()
{ /* Function for Delete operation */
PriorityQ p;
if(Qempty()){ printf("nnUnderflow!!!!nn");
p.ele=-1;p.pr=-1;
return(p); }
else
{
p=PQ[f];
f=f+1;
return(p);
}
}

int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}

display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" n Empty Queuen");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("[%d,%d] ",PQ[i].ele,PQ[i].pr);
printf("<-Rear");
}
}

main()
{ /* Main Program */
int opn;
PriorityQ p;
do
{
clrscr();
printf("n ### Priority Queue Operations(DSC order) ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("nnRead the element and its Priority?");
scanf("%d%d",&p.ele,&p.pr);
PQinsert(p.ele,p.pr); break;
case 2: p=PQdelete();
if( p.ele != -1)
printf("nnDeleted Element is %d n",p.ele);
break;
case 3: printf("nnStatus of Queuenn");
display(); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}


Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement QUEUE operations using Linked Lists

Data structures using C,
C Program to implement QUEUE operations using Linked list. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front end. Here to implement queue we used the Linked lists!. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdlib.h>
typedef struct node {
int data;
struct node *link;
} NODE;

void Insert(int);
int Delete();
void Display();
NODE *front, *rear; /* Global Declarations */

main() {
/* Main Program */
int opn, elem;
front = rear = NULL;
do {
clrscr();
printf("n ### Linked List Implementation of QUEUE Operations ### nn");
printf("n Press 1-Insert, 2-Delete, 3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the Element to be Inserted ?");
scanf("%d", &elem);
Insert(elem);
break;
case 2:
elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %dn", elem);
break;
case 3:
printf("Linked List Implementation of Queue: Status:n");
Display();
break;
case 4:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}

void Insert(int info) {
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else {
temp->data = info;
temp->link = NULL;
if (front == NULL) {
front = rear = temp;
} /* First Node? */
else {
rear->link = temp;
rear = temp;
} /* Insert End */
printf(" Node has been inserted at End Successfully !!");
}
}

int Delete() {
int info;
NODE *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1;
} else {
t = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info);
}
}

void Display() {
NODE *t;
if (front == NULL)
printf("Empty Queuen");
else {
t = front;
printf("Front->");
while (t) {
printf("[%d]->", t->data);
t = t->link;
}
printf("Rearn");
}
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program for Circular QUEUE Operations

Data structures using C,
C Program to implement circular queue. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front end. In circular queue is connected to end to end, i,e rear and front end are connected. Compare to normal queue, Circular queue is more advantages. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#define SIZE 5 /* Size of Circular Queue */
int CQ[SIZE], f = -1, r = -1; /* Global declarations */

CQinsert(int elem) { /* Function for Insert operation */
if (CQfull())
printf("nn Overflow!!!!nn");
else {
if (f == -1)
f = 0;
r = (r + 1) % SIZE;
CQ[r] = elem;
}
}

int CQdelete() { /* Function for Delete operation */
int elem;
if (CQempty()) {
printf("nnUnderflow!!!!nn");
return (-1);
} else {
elem = CQ[f];
if (f == r) {
f = -1;
r = -1;
} /* Q has only one element ? */
else
f = (f + 1) % SIZE;
return (elem);
}
}

int CQfull() { /* Function to Check Circular Queue Full */
if ((f == r + 1) || (f == 0 && r == SIZE - 1))
return 1;
return 0;
}

int CQempty() { /* Function to Check Circular Queue Empty */
if (f == -1)
return 1;
return 0;
}

display() { /* Function to display status of Circular Queue */
int i;
if (CQempty())
printf(" n Empty Queuen");
else {
printf("Front[%d]->", f);
for (i = f; i != r; i = (i + 1) % SIZE)
printf("%d ", CQ[i]);
printf("%d ", CQ[i]);
printf("<-[%d]Rear", r);
}
}

main() { /* Main Program */
int opn, elem;
do {
clrscr();
printf("n ### Circular Queue Operations ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the element to be Inserted ?");
scanf("%d", &elem);
CQinsert(elem);
break;
case 2:
elem = CQdelete();
if (elem != -1)
printf("nnDeleted Element is %d n", elem);
break;
case 3:
printf("nnStatus of Circular Queuenn");
display();
break;
case 4:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}

Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program for Simple/Linear QUEUE Operations

Data structures using C,
C Program to implement circular queue. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front end. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#define SIZE 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */

Qinsert(int elem)
{ /* Function for Insert operation */
if( Qfull()) printf("nn Overflow!!!!nn");
else
{
++r;
Q[r]=elem;
}
}

int Qdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
elem=Q[f];
f=f+1;
return(elem);
}
}

int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}

display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" n Empty Queuen");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",Q[i]);
printf("<-Rear");
}
}

main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("n ### Queue Operations ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("nnRead the element to be Inserted ?");
scanf("%d",&elem);
Qinsert(elem); break;
case 2: elem=Qdelete();
if( elem != -1)
printf("nnDeleted Element is %d n",elem);
break;
case 3: printf("nnStatus of Queuenn");
display(); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}



Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com