Assignment No 7: Queue
Practice Programs:
1) Write a C program to Implement Static implementation of circular queue
of integers which includes operation as: a) Initialize() b) insert() c)
delete() d) isempty() e) isfull() f) display() g) peek()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
typedef struct queue
{
int front, rear;
int items[MAX];
} Queue;
void initQueue(Queue *pq)
{
pq->front = pq->rear = -1;
printf("Queue Initialized
!!\n");
}
int isFull(Queue *pq)
{
return ((pq->rear + 1 % MAX)
== pq->front) ? 1 : 0;
}
int isEmpty(Queue *pq)
{
return (pq->rear == -1
&& pq->front == -1) ? 1 : 0;
}
int delete(Queue *pq)
{
int t;
if (isEmpty(pq))
{
printf("Queue is Empty
!!\n");
return -1;
}
else if (pq->front ==
pq->rear)
{
t =
pq->items[pq->front];
pq->front = pq->rear =
-1;
}
else
{
t =
pq->items[pq->front];
pq->front = (pq->front
+ 1) % MAX;
}
printf("%d Deleted\n",
t);
return t;
}
void insert(Queue *pq, int val)
{
if (isFull(pq))
{
printf("The Queue is
Full !!\n");
return;
}
else if (isEmpty(pq))
{
pq->front++;
pq->rear++;
pq->items[pq->rear] =
val;
}
else
{
pq->rear = (pq->rear +
1) % MAX;
pq->items[pq->rear] =
val;
}
printf("%d insert \n",
val);
}
void display(Queue *pq)
{
if (isEmpty(pq))
{
printf("This Queue is
Empty !!\n");
return;
}
else
{
for (int i = pq->front; i
<= pq->rear; i++)
{
printf("%d\t",
pq->items[i]);
}
}
}
int peek(Queue *pq, int pos)
{
int i;
if (isEmpty(pq) || pos < 0 ||
pos > pq->rear)
{
printf("Invalid
Input!!\n");
return -1;
}
for (i = pq->front; i < pos
- 1; i++)
;
return pq->items[i];
}
int main()
{
Queue q;
int choice, data, pos;
initQueue(&q);
do
{
printf("\n--------------
Menu Bar -------------------\n");
printf(" 1.Insert\n
2.Delete\n 3.Peek\n 4.Display\n 5.Exit\n");
printf("---------------------------------------------------------\n");
printf("Enter your
choice : ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data : ");
scanf("%d",
&data);
insert(&q, data);
break;
case 2:
delete (&q);
break;
case 3:
if (!isEmpty(&q))
{
printf("Enter
the peek position : ");
scanf("%d",
&pos);
printf("The peek
element is: %d", peek(&q, pos));
}
else
{
printf("The
Queue is empty !!\n");
}
break;
case 4:
display(&q);
break;
case 5:
exit(0);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 5);
return 0;
}
========================================================
2) Write a C program to Implement Dynamic implementation of circular queue
of integers includes operation as : a)Initialize() b) insert() c)delete() d)
isempty() e)display() f) peek()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct queue
{
int data;
struct queue *next;
} CQueue;
CQueue *front, *rear;
void initQueue()
{
front = NULL;
rear = NULL;
printf("Queue is Initialized
!!\n");
}
int isEmpty()
{
return (front == NULL &&
rear == NULL) ? 1 : 0;
}
void insert(int val)
{
CQueue *new_node;
new_node = (CQueue
*)malloc(sizeof(CQueue));
new_node->data = val;
new_node->next = NULL;
if (front == NULL && rear
== NULL)
{
front = new_node;
rear = new_node;
new_node->next = front;
printf("First
insert\n");
}
else
{
rear->next = new_node;
new_node->next = front;
rear = new_node;
printf("%d: inserted
!!\n", val);
}
}
void display()
{
CQueue *temp = front;
if (isEmpty())
{
printf("The Queue is
empty!!\n");
}
else
{
do
{
printf("%d\t",
temp->data);
temp = temp->next;
} while (temp != front);
}
}
int delete()
{
int t;
CQueue *temp = front;
if (isEmpty())
{
printf("The queue is
Empty !!\n");
}
else if (front == rear)
{
t = front->data;
free(front);
front = NULL;
rear = NULL;
}
else
{
t = temp->data;
front = temp->next;
rear->next = front;
free(temp);
}
return t;
}
int peek(int pos)
{
CQueue *temp = front;
int i = 1;
do
{
if (i == pos)
{
return temp->data;
}
temp = temp->next;
i++;
} while (temp != front);
return -1;
}
int main()
{
int choice, data, pos, p;
initQueue();
do
{
printf("\n--------------
Menu Bar -------------------\n");
printf(" 1.Insert\n
2.Delete\n 3.Peek\n 4.Display\n 5.Exit\n");
printf("---------------------------------------------------------\n");
printf("Enter your
choice : ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data : ");
scanf("%d",
&data);
insert(data);
break;
case 2:
printf("%d
deleted\n", delete ());
break;
case 3:
printf("Enter the
position : ");
scanf("%d",
&pos);
if (isEmpty())
{
printf("Queue is
Empty");
}
else if ((p = peek(pos))
== -1)
{
printf("Invalid
Position !!\n");
}
else
{
printf("Peek
element is: %d", p);
}
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 5);
return 0;
}
======================================================
3) Write a C Program to implement Deque using doubly linked list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node *next, *prev;
} DNode;
typedef struct deque
{
DNode *front, *rear;
} Deque;
Deque *initQueue(Deque *dq)
{
dq = (Deque
*)malloc(sizeof(Deque));
dq->front = NULL;
dq->rear = NULL;
return dq;
}
DNode *createNode(int data)
{
DNode *newNode = (DNode
*)malloc(sizeof(DNode));
newNode->data = data;
newNode->next =
newNode->prev = NULL;
return newNode;
}
int isEmpty(Deque *dq)
{
return (dq->front == NULL
&& dq->rear == NULL);
}
void insertFront(Deque *dq, int data)
{
DNode *newNode;
newNode = createNode(data); //
create node
if (isEmpty(dq))
{
dq->front = dq->rear =
newNode;
}
else
{
newNode->next =
dq->front;
dq->front->prev =
newNode;
dq->front = newNode;
}
}
void insertRear(Deque *dq, int data)
{
DNode *newNode =
createNode(data);
if (isEmpty(dq))
{
dq->front = dq->rear =
newNode;
}
else
{
dq->rear->next =
newNode;
newNode->prev =
dq->rear;
dq->rear = newNode;
}
}
int deleteFront(Deque *dq)
{
DNode *temp = dq->front;
int t;
if (isEmpty(dq))
{
printf("The DEQueue is
Empty !!\n");
return -1;
}
else if (dq->front ==
dq->rear)
{
t = dq->front->data;
free(temp);
dq->front = dq->rear =
NULL;
printf("Equal \n");
}
else
{
t = dq->front->data;
dq->front = temp->next;
dq->front->prev = NULL;
free(temp);
printf("Regular\n");
}
return t;
}
int deleteRear(Deque *dq)
{
int t;
DNode *temp = dq->rear;
if (isEmpty(dq))
{
printf("The DEQueue is
Empty !!\n");
return -1;
}
else if (dq->front ==
dq->rear)
{
t = dq->rear->data;
free(temp);
dq->front = dq->rear =
NULL;
printf("Equal\n");
}
else
{
t = dq->rear->data;
dq->rear = temp->prev;
dq->rear->next = NULL;
printf("Second\n");
}
return t;
}
void display(Deque *dq)
{
DNode *temp = dq->front;
printf("DEQueue is :
");
if (isEmpty(dq))
{
printf("DEQueue is Empty
!!\n");
}
else
{
while (temp != NULL)
{
printf("%d ",
temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main()
{
Deque *dq;
int data, choice;
dq = initQueue(dq);
printf("%d\n",
isEmpty(dq));
do
{
printf("--------------------- Menu Bar
---------------------------\n");
printf(" 1.Insert
Front\n 2.Insert Rear\n 3.Delete Front\n 4.Delete Rear\n 5.display\n
6.Exit\n");
printf("----------------------------------------------------------
\n");
printf("Enter your
choice : ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data : ");
scanf("%d",
&data);
insertFront(dq, data);
break;
case 2:
printf("Enter the
data : ");
scanf("%d",
&data);
insertRear(dq, data);
break;
case 3:
printf("%d: Deleted
from Front !! \n", deleteFront(dq));
break;
case 4:
printf("%d: Deleted
from Rear!! \n", deleteRear(dq));
break;
case 5:
display(dq);
break;
case 6:
exit(1);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 6);
return 0;
}
==========================================================
SET A:
1) Write a C program to Implement Static implementation of Queue of
integers with following operation:
-Initialize(), insert(), delete(), isempty(), isfull(), display(), peek()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
typedef struct queue
{
int front, rear;
int items[MAX];
} Queue;
void initQueue(Queue *pq)
{
pq->front = pq->rear = -1;
printf("Initialized Queue
!!\n");
}
int isEmpty(Queue *pq)
{
return ((pq->front == -1)
&& (pq->rear == -1));
}
int isFull(Queue *pq)
{
return (pq->rear == MAX - 1);
}
void insert(Queue *pq, int data)
{
if (isFull(pq))
{
printf("The Queue is
Full !!\n");
return;
}
else if (isEmpty(pq))
{
pq->front++;
pq->rear++;
pq->items[pq->rear] =
data;
}
else
{
pq->rear++;
pq->items[pq->rear] =
data;
}
}
int deleteVal(Queue *pq)
{
int t;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return -1;
}
else if (pq->front ==
pq->rear)
{
t =
pq->items[pq->front];
pq->front = pq->rear =
-1;
}
else
{
t =
pq->items[pq->front];
pq->front++;
}
return t;
}
int peek(Queue *pq)
{
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return -1;
}
else
{
return
pq->items[pq->front];
}
}
void display(Queue *pq)
{
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
printf("The Queue is:
");
for (int i = pq->front; i
<= pq->rear; i++)
{
printf("%d ",
pq->items[i]);
}
printf("\n");
}
}
int main()
{
Queue q;
int choice, data;
initQueue(&q);
do
{
printf("--------------------- Menu Bar
---------------------\n");
printf(" 1.Insert\n
2.Delete\n 3.peek\n 4.display\n 5.Exit\n");
printf("-----------------------------------------------------\n");
printf("Enter your
choice : ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data : ");
scanf("%d",
&data);
insert(&q, data);
break;
case 2:
printf("%d: deleted
!!\n", deleteVal(&q));
break;
case 3:
printf("%d: Peek
element !!\n", peek(&q));
break;
case 4:
display(&q);
break;
case 5:
exit(1);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 5);
return 0;
}
=========================================================
2) Write a program to reverse the elements of a queue (Use Static
implementation of Queue)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
typedef struct queue
{
int front, rear;
int items[MAX];
} Queue;
void initQueue(Queue *pq)
{
pq->front = pq->rear = -1;
printf("Initialized Queue
!!\n");
}
int isEmpty(Queue *pq)
{
return ((pq->front == -1)
&& (pq->rear == -1));
}
int isFull(Queue *pq)
{
return (pq->rear == MAX - 1);
}
void insert(Queue *pq, int data)
{
if (isFull(pq))
{
printf("The Queue is
Full !!\n");
return;
}
else if (isEmpty(pq))
{
pq->front++;
pq->rear++;
pq->items[pq->rear] =
data;
}
else
{
pq->rear++;
pq->items[pq->rear] =
data;
}
}
void display(Queue *pq)
{
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
printf("The Queue is:
");
for (int i = pq->front; i
<= pq->rear; i++)
{
printf("%d ",
pq->items[i]);
}
printf("\n");
}
}
void reverseQueue(Queue *pq)
{
int i, j, temp;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
i = pq->front;
j = pq->rear;
while (i < j)
{
temp = pq->items[i];
pq->items[i] =
pq->items[j];
pq->items[j] = temp;
i++;
j--;
}
}
}
int main()
{
Queue q;
int n, i, data;
initQueue(&q);
printf("Enter the size of
Queue : ");
scanf("%d", &n);
printf("Enter the Element
into Queue !!\n");
for (i = 0; i < n; i++)
{
printf("Enter data:
");
scanf("%d",
&data);
insert(&q, data);
}
display(&q);
printf("After Reverse Queue
is: ");
reverseQueue(&q);
display(&q);
return 0;
}
===========================================================
SET B:
1) Write a C program to Implement Dynamic implementation of Queue of
integers with following operation:
-Initialize(), insert(), delete(), isempty(), display(), peek()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node *next;
} Node;
typedef struct queue
{
struct node *front, *rear;
} Queue;
Queue *initQueue(Queue *pq)
{
pq = (Queue
*)malloc(sizeof(Queue));
pq->front = pq->rear =
NULL;
printf("Queue is Initialized
!!\n");
return pq;
}
Node *createNode(int data)
{
Node *newNode = (Node
*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int isEmpty(Queue *pq)
{
return (pq->front == NULL
&& pq->rear == NULL);
}
void insert(Queue *pq, int data)
{
Node *newNode = createNode(data);
if (isEmpty(pq))
{
pq->front = pq->rear =
newNode;
}
else
{
pq->rear->next =
newNode;
pq->rear = newNode;
}
}
int deleteVal(Queue *pq)
{
int t = -1;
Node *temp = pq->front;
if (isEmpty(pq))
{
printf("The Queue is
Empty!!\n");
return t;
}
else if (pq->front ==
pq->rear)
{
t = pq->front->data;
pq->front = pq->rear =
NULL;
}
else
{
t = pq->front->data;
pq->front = temp->next;
free(temp);
}
return t;
}
int peek(Queue *pq)
{
int t;
if (isEmpty(pq))
{
printf("The Queue is
Empty!!\n");
return -1;
}
else
{
return pq->front->data;
}
}
void display(Queue *pq)
{
Node *temp = pq->front;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
printf("The Queue is:
");
while (temp != NULL)
{
printf("%d ",
temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main()
{
Queue *que;
int choice, data;
que = initQueue(que);
printf("%d\n",
isEmpty(que));
do
{
printf("\n------------------------ Menu Bar
----------------------\n");
printf(" 1.Insert\n
2.Delete\n 3.peek\n 4.display\n 5.Exit\n");
printf("----------------------------------------------------------\n");
printf("Enter your
choice : ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data: ");
scanf("%d",
&data);
insert(que, data);
break;
case 2:
printf("%d: Deleted
element!!\n", deleteVal(que));
break;
case 3:
printf("%d: peek
element!!\n", peek(que));
break;
case 4:
display(que);
break;
case 5:
exit(1);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 5);
return 0;
}
=======================================================
2) Write a program to reverse the elements of a queue (Use Dynamic
implementation of Queue)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node *next;
} Node;
typedef struct queue
{
struct node *front, *rear;
} Queue;
Queue *initQueue(Queue *pq)
{
pq = (Queue
*)malloc(sizeof(Queue));
pq->front = pq->rear =
NULL;
printf("Queue is Initialized
!!\n");
return pq;
}
Node *createNode(int data)
{
Node *newNode = (Node
*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int isEmpty(Queue *pq)
{
return (pq->front == NULL
&& pq->rear == NULL);
}
void insert(Queue *pq, int data)
{
Node *newNode = createNode(data);
if (isEmpty(pq))
{
pq->front = pq->rear =
newNode;
}
else
{
pq->rear->next =
newNode;
pq->rear = newNode;
}
}
void reverseQueue(Queue *pq)
{
Node *temp = pq->front, *next,
*prev = NULL;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
while (temp != NULL)
{
next = temp->next;
temp->next = prev;
prev = temp;
temp = next;
}
}
pq->rear = pq->front;
pq->front = prev;
}
void display(Queue *pq)
{
Node *temp = pq->front;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
printf("The Queue is:
");
while (temp != NULL)
{
printf("%d ",
temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main()
{
Queue *que;
int n, i, data;
que = initQueue(que);
printf("Enter the size of
Queue: ");
scanf("%d", &n);
printf("Enter the Elements
into Queue : \n");
for (i = 0; i < n; i++)
{
printf("Enter the data:
");
scanf("%d",
&data);
insert(que, data);
}
display(que);
printf("After Reverse :
\n");
reverseQueue(que);
display(que);
return 0;
}
=================================================
3) Write a C program to Implement Static implementation of circular queue
of integers with following operation:
-Initialize(), insert(), delete(), isempty(), isfull(), display(), peek()
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
typedef struct queue
{
int front, rear;
int items[MAX];
} Queue;
void initQueue(Queue *pq)
{
pq->front = pq->rear = -1;
printf("The Queue is
Initialized !!\n");
}
int isEmpty(Queue *pq)
{
return (pq->front == -1
&& pq->rear == -1);
}
int isFull(Queue *pq)
{
return ((pq->rear + 1) % MAX
== pq->front);
}
void insert(Queue *pq, int data)
{
if (isFull(pq))
{
printf("The Queue is
Full!!\n");
return;
}
else if (isEmpty(pq))
{
pq->front++;
pq->rear++;
pq->items[pq->rear] =
data;
printf("first\n");
}
else
{
pq->rear = (pq->rear +
1) % MAX;
pq->items[pq->rear] =
data;
printf("Second\n");
}
}
int deleteVal(Queue *pq)
{
int t;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return -1;
}
else if (pq->front ==
pq->rear)
{
t =
pq->items[pq->front];
pq->front = pq->rear =
-1;
}
else
{
t =
pq->items[pq->front];
pq->front = (pq->front
+ 1) % MAX;
}
return t;
}
void display(Queue *pq)
{
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
printf("The Queue is:
");
for (int i = pq->front; i
<= pq->rear; i++)
{
printf("%d ",
pq->items[i]);
}
printf("\n");
}
}
int peek(Queue *pq)
{
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return -1;
}
else
{
return
(pq->items[pq->front]);
}
}
int main()
{
Queue que;
int choice, data;
initQueue(&que);
printf("%d\n",
isFull(&que));
do
{
printf("--------------------- Menu Bar
---------------------\n");
printf(" 1. Insert\n
2.Delete\n 3.Display\n 4.peek\n 5.Exit\n");
printf("--------------------------------------------------------\n");
printf("Enter your
choice: ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data: ");
scanf("%d",
&data);
insert(&que, data);
break;
case 2:
if (isEmpty(&que))
{
printf("The
Queue is Empty !!\n");
}
else
{
printf("%d:
Deleted element \n", deleteVal(&que));
}
break;
case 3:
display(&que);
break;
case 4:
if (isEmpty(&que))
{
printf("The
Queue is Empty !!\n");
}
else
{
printf("%d: Peek
element \n", peek(&que));
}
break;
case 5:
exit(1);
break;
default:
printf("Wrong Input
!! \n");
break;
}
} while (choice != 5);
return 0;
}
====================================================
SET C:
1) Write a c program to simulate waiting list operations of railway
reservation system.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PASS_SIZE 50
/*
Variable names meaning
Variable Meaning
CON_PASS => Confirmed
Passengers size
conPass => This is an Array to store Confirmed
Passenger
C_count => count the confirmed(fix size) of
passenger
*/
typedef struct passenger
{
char name[50];
int age;
char source[50], destination[50];
} Passenger;
Passenger conPass[PASS_SIZE];
int C_count = 0;
void booking()
{
Passenger pass; // Structure
variable
if (C_count < PASS_SIZE)
{
printf("Enter the
passenger name: ");
scanf("%s",
pass.name);
printf("Enter the
passenger Age: ");
scanf("%d",
&pass.age);
printf("Enter the
Source: ");
scanf("%s",
pass.source);
printf("Enter the
Destination: ");
scanf("%s",
pass.destination);
conPass[C_count] = pass;
C_count++;
printf("Ticket Booked
SuccessFully!!\n");
}
else
{
printf("Cannot Book a
ticket. lists are Full!!\n");
}
}
void passengerDetails()
{
int i, j;
printf("Confirmed
Passengers: \n");
printf("Passenger Details :
\n");
for (i = 0; i < C_count; i++)
{
printf(" Name: %s
\n", conPass[i].name);
printf(" Age: %d \n",
conPass[i].age);
printf(" Source: %s
\n", conPass[i].source);
printf(" Destination: %s
\n", conPass[i].destination);
}
}
int main()
{
int choice;
do
{
printf("\n---------
Railway Reservation System ------------\n");
printf(" 1. Book a
ticket\n 2. Passenger Details\n 3.Exit\n");
printf("-----------------------------------------------------\n\n");
printf("Enter your
choice: \n");
scanf("%d",
&choice);
switch (choice)
{
case 1:
booking();
break;
case 2:
passengerDetails();
break;
case 3:
exit(1);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 3);
return 0;
}
==================================================
2) Implement a priority of integers using a static implementation of the
queue and implementing the below two operations. Write a menu driven program
a) Add an element with its priority into the queue.
b) Delete an element from queue according to its priority.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
int priority;
struct node *next;
} Node;
typedef struct queue
{
struct node *front, *rear;
} Queue;
Queue *initQueue(Queue *pq)
{
pq = (Queue
*)malloc(sizeof(Queue));
pq->front = NULL;
pq->rear = NULL;
printf("Initialized
Queue!!\n");
return pq;
}
int isEmpty(Queue *pq)
{
return (pq->front == NULL
&& pq->rear == NULL);
}
void insert(Queue *pq, int data, int priority)
{
Node *newNode = (Node
*)malloc(sizeof(Node));
newNode->data = data;
newNode->priority = priority;
newNode->next = NULL;
if (isEmpty(pq))
{
pq->front = pq->rear =
newNode;
}
else
{
pq->rear->next =
newNode;
pq->rear = newNode;
}
}
void display(Queue *pq)
{
Node *temp = pq->front;
if (isEmpty(pq))
{
printf("The Queue is
Empty !!\n");
return;
}
else
{
printf("The Queue is:
");
while (temp != NULL)
{
printf("%d ",
temp->data);
temp = temp->next;
}
printf("\n");
}
}
int highPriority(Queue *pq)
{
int min;
Node *temp = pq->front;
min = temp->priority;
while (temp != NULL)
{
if (min >
temp->priority)
{
min = temp->priority;
}
temp = temp->next;
}
return min;
}
int deleteVal(Queue *pq)
{
int p, t;
Node *temp = pq->front,
*temp2;
p = highPriority(pq);
if (temp->priority == p)
{
t = temp->data;
pq->front = temp->next;
free(temp);
}
else
{
while (temp != NULL)
{
if
(temp->next->priority == p)
{
temp2 =
temp->next;
t = temp2->data;
temp->next =
temp2->next;
free(temp2);
return t;
}
temp = temp->next;
}
}
return t;
}
int main()
{
Queue *que;
int choice, data, p;
que = initQueue(que);
do
{
printf("----------------- Menu Bar ---------------------\n");
printf(" 1. Insert\n
2.Delete\n 3.Display\n 4.Exit\n");
printf("--------------------------------------------------\n");
printf("Enter your
choice: ");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the
data: ");
scanf("%d",
&data);
printf("Enter the
Priority: ");
scanf("%d",
&p);
insert(que, data, p);
break;
case 2:
if (isEmpty(que))
{
printf("The
queue is Empty !!\n");
}
else
{
printf("%d:
Delete element\n", deleteVal(que));
}
break;
case 3:
display(que);
break;
case 4:
exit(1);
break;
default:
printf("Wrong Input
!!\n");
break;
}
} while (choice != 4);
return 0;
}
=====================================================
3) A doubly ended queue allows additions and deletions from both the ends
that is front and rear. Initially additions from the front will not be
possible. To avoid this situation, the array can be treated as if it were
circular. Implement a queue library (dstqueue.h) of integers using a static
implementation of the circular queue and implementing the nine operations :
1)init(Q), 2) isempty(Q) 3) isFull(Q) 4)getFront(Q), 5)getRear(Q), 6)addFront(Q,x), 7)deleteFront(Q) 8) addRear(Q,x) 9)deleteRear(Q
0 Comments