0% found this document useful (0 votes)
4 views

Lab Assignments_04

Uploaded by

hernotebook96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Assignments_04

Uploaded by

hernotebook96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Name-Devansh Singh Nikam

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;
};

typedef struct node node;

struct Queue {
node* front;
node* rear;
};

typedef struct Queue Queue;

void enqueue(Queue* q, int value);


void dequeue(Queue* q);
void display(Queue* q);

void enqueue(Queue* q, int value) {


node* newnode = (node*)malloc(sizeof(node));
newnode->data = value;
newnode->next=NULL;
if (q->front == NULL && q->rear == NULL) {
q->front = q->rear = newnode;
printf("%d is inserted\n", value);
} else {
q->rear->next = newnode;
q->rear = newnode;
printf("%d is inserted\n", value);
}
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
}

void dequeue(Queue* q){


if(q->front == NULL){
printf("Queue underflow\n");
}
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: ");
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;

int choice, value;

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");

printf("Enter your choice: ");


scanf("%d", &choice);

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;
};

void createQueue(struct Queue* q) {


q->front = -1;
q->rear = -1;
}

int isFull(struct Queue* q) {


return q->rear == MAX - 1;
}

int isEmpty(struct Queue* q) {


return q->front == -1;
}

void enqueue(struct Queue* q, int value) {


if (isFull(q)) {
printf("Queue is full\n");
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
printf("Inserted %d\n", value);
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
}
}

int dequeue(struct Queue* q) {


if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
} else {
int item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
printf("Removed: %d\n", item);
return item;
}
}

void display(struct Queue* q) {


if (isEmpty(q)) {
printf("Queue is empty\n");
} else {
printf("Queue contains: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->items[i]);
}
printf("\n");
}
}

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(){

int choice, value;

while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");

printf("Enter your choice: ");


Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
scanf("%d", &choice);

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

Que4) Write a program to implement circular Queue using link list.

● Program:-

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* next;
};

typedef struct node node;

struct Queue {
node* front;
node* rear;
};

typedef struct Queue Queue;

void enqueue(Queue* q, int value);


void dequeue(Queue* q);
void display(Queue* q);

void enqueue(Queue* q, int value) {


node* newnode = (node*)malloc(sizeof(node));
newnode->data = value;
if (q->front == NULL && q->rear == NULL) {
q->front = q->rear = newnode;
newnode->next = newnode;
printf("%d is inserted\n", value);
} else {
q->rear->next = newnode;
q->rear = newnode;
newnode->next = q->front;
printf("%d is inserted\n", value);
}
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
}

void dequeue(Queue* q){


if(q->front == NULL){
printf("Queue underflow\n");
}
else if (q->front == q->rear) {
printf("%d is dequeued\n", q->front->data);
free(q->front);
q->front = q->rear = NULL;

}
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;

int choice, value;


Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2

while (1) {
printf("\nQueue Operations:\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 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;
};

typedef struct node node;

struct Queue {
node* front;
};

typedef struct Queue PriorityQueue;

void enqueue(PriorityQueue* pq, int value, int priority);


void dequeue(PriorityQueue* pq);
void display(PriorityQueue* pq);

void enqueue(PriorityQueue* pq, int value, int priority) {


node* newnode = (node*)malloc(sizeof(node));
newnode->data = value;
newnode->priority = priority;
if (pq->front == NULL || priority < pq->front->priority) {
newnode->next = pq->front;
pq->front = newnode;
printf("%d is inserted\n", value);
}
else {
node* current = pq->front;
while (current->next != NULL && current->next->priority <= priority) {
current = current->next;
}
newnode->next = current->next;
Name-Devansh Singh Nikam
Enroll no:-0801IT231043
Batch A2
current->next = newnode;
printf("%d is inserted\n", value);
}
}

void dequeue(PriorityQueue* pq){


if(pq->front == NULL){
printf("Queue underflow\n");
}
else{
node* temp = pq->front;
pq->front = pq->front->next;
printf("%d is dequeued\n", temp->data);
free(temp);
}
}

void display(PriorityQueue* pq) {


if (pq->front == NULL) {
printf("Queue is empty\n");
} else {
node* temp = pq->front;
printf("Queue elements: ");
while(temp != NULL){
printf("%d ", temp->data);
temp = temp->next;
};
printf("\n");
}
}

int main() {

PriorityQueue* pq = (PriorityQueue*)malloc(sizeof(PriorityQueue));
pq->front = NULL;

int choice, value, priority;

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");

printf("Enter your choice: ");


scanf("%d", &choice);

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

You might also like