Lab Assignments_04
Lab Assignments_04
Enroll no:-0801IT231043
Batch A2
Lab Assignments_04
Que1) Write a program to implement QUEUE using link list that performs
following operations (a) INSERT (b) DELETE (c) DISPLAY.
● Program:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct Queue {
node* front;
node* rear;
};
void display(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty\n");
} else {
node* temp = q->front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2.Dequeue\n");
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
printf("3. Display\n");
printf("4. Exit\n");
switch (choice) {
case 1:
printf("Enter a number: ");
scanf("%d", &value);
enqueue(q, value);
break;
case 2:
dequeue(q);
break;
case 3:
display(q);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please choose again.\n");
}
}
return 0;
}
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
● Output:-
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
Que2) Write a program to implement Queue (FIFO) using Array.
● Program:-
#include <stdio.h>
#define MAX 5
struct Queue {
int items[MAX];
int front;
int rear;
};
int main() {
struct Queue q;
createQueue(&q);
int choice, value;
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
while (1) {
printf("\nMenu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
● Output:-
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
Que3) Write a program to implement circular Queue using array that performs
following operations (a) INSERT (b) DELETE (c) DISPLAY.
● Program:-
#include<stdio.h>
#define MAX_SIZE 10
int Queue[MAX_SIZE];
int rear = -1;
int front = -1;
void enqueue();
void dequeue();
void display();
void enqueue(){
if((rear+1)%MAX_SIZE == front){
printf("Queue overflow\n");
return;
}
int n;
printf("Please Enter an element: ");
scanf("%d", &n);
if(rear == -1 && front == -1){
rear = 0;
front = 0;
}
else{
rear = (rear+1)%MAX_SIZE;
}
printf("%d is inserted in the Queue", n);
Queue[rear] = n;
}
void dequeue(){
if(rear == -1 && front == -1){
printf("Queue underflow\n");
return;
}
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
int n;
n = Queue[front];
if(front == rear){
front = -1;
rear = -1;
}
else{
front = (front+1)%MAX_SIZE;
}
printf("%d is delete out", n);
void display(){
if(rear == -1 && front == -1){
printf("Queue empty\n");
}
else{
printf("Elements of Queue are: \n");
int i = front;
while(i != rear){
printf("%5d ", Queue[i]);
i = (i+1)%MAX_SIZE;
}
printf("%5d ", Queue[i]);
}
}
int main(){
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
switch (choice) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice. Please choose again.\n");
}
}
return 0;
}
● Output:-
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
● Program:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct Queue {
node* front;
node* rear;
};
}
else{
node* temp = q->front;
q->front = q->front->next;
printf("%d is dequeued\n", temp->data);
free(temp);
}
}
void display(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty\n");
} else {
node* temp = q->front;
printf("Queue elements: ");
do{
printf("%d ", temp->data);
temp = temp->next;
}while (temp != q->front);
printf("\n");
}
}
int main() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2.Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
switch (choice) {
case 1:
printf("Enter a number: ");
scanf("%d", &value);
enqueue(q, value);
break;
case 2:
dequeue(q);
break;
case 3:
display(q);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please choose again.\n");
}
}
return 0;
}
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
● Output:-
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
Que5)Write a program to implement priority queue using link list.
● Program:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
int priority;
struct node* next;
};
struct Queue {
node* front;
};
int main() {
PriorityQueue* pq = (PriorityQueue*)malloc(sizeof(PriorityQueue));
pq->front = NULL;
while (1) {
printf("\nQueue Operations:\n");
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
switch (choice) {
case 1:
printf("Enter a number: ");
scanf("%d", &value);
printf("Enter priority: ");
scanf("%d", &priority);
enqueue(pq, value, priority);
break;
case 2:
dequeue(pq);
break;
case 3:
display(pq);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please choose again.\n");
}
}
return 0;
}
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
● Output:-
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2