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

DS Lab Assignment 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DS Lab Assignment 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Name : Shubham Maurya

Enrollment no : 0801IT231074
Btech 2nd Year (A3)

Lab Assignment 4

Q-1) Write a program to implement QUEUE using link list that performs following
operations.
a) INSERT b) DELETE c) DISPLAY

Code:

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

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

struct Node* newNode(int data) {


struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
return temp;
}

struct Queue {
struct Node *front, *rear;
};

struct Queue* createQueue() {


struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

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


struct Node* temp = newNode(data);

if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}

q->rear->next = temp;
q->rear = temp;
printf("%d inserted into the queue.\n", data);
}

void dequeue(struct Queue* q) {

if (q->front == NULL) {
printf("Queue is empty, nothing to delete.\n");
return;
}

struct Node* temp = q->front;


q->front = q->front->next;

if (q->front == NULL) {
q->rear = NULL;
}

printf("%d deleted from the queue.\n", temp->data);


free(temp);
}

void display(struct Queue* q) {


if (q->front == NULL) {
printf("Queue is empty.\n");
return;
}

struct Node* temp = q->front;


printf("Queue: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
display(q);
dequeue(q);
display(q);
dequeue(q);
display(q);
dequeue(q);
display(q);
dequeue(q);
return 0;
}

Q-2) Write a program to implement Queue (FIFO) using link list.

Code :

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

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

struct Queue {
struct Node *front, *rear;
};
struct Node* newNode(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
return temp;
}

struct Queue* createQueue() {


struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

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


// Create a new node
struct Node* temp = newNode(data);

if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}

q->rear->next = temp;
q->rear = temp;
}

int dequeue(struct Queue* q) {

if (q->front == NULL)
return -1;

struct Node* temp = q->front;


int data = temp->data;
q->front = q->front->next;

if (q->front == NULL)
q->rear = NULL;

free(temp);
return data;
}

void displayQueue(struct Queue* q) {


struct Node* temp = q->front;
if (temp == NULL) {
printf("Queue is empty\n");
return;
}
printf("Queue: ");
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct Queue* q = createQueue();

enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);

displayQueue(q);

printf("Dequeued: %d\n", dequeue(q));


displayQueue(q);

enqueue(q, 40);
displayQueue(q);

return 0;
}

Q-3) Write a program to implement circular Queue using array that perforoms following
operations a) INSERT b) DELETE c) DISPLAY.
Code :

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

#define SIZE 5

struct CircularQueue {
int items[SIZE];
int front, rear;
};

void initQueue(struct CircularQueue* q) {


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

int isFull(struct CircularQueue* q) {


if ((q->front == 0 && q->rear == SIZE - 1) || (q->rear == (q->front - 1) % (SIZE - 1))) {
return 1;
}
return 0;
}

int isEmpty(struct CircularQueue* q) {


if (q->front == -1) {
return 1;
}
return 0;
}

void insert(struct CircularQueue* q, int value) {


if (isFull(q)) {
printf("Queue is full!\n");
return;
}

if (q->front == -1)
q->front = 0;

q->rear = (q->rear + 1) % SIZE;


q->items[q->rear] = value;
printf("Inserted %d\n", value);
}

void delete(struct CircularQueue* q) {


if (isEmpty(q)) {
printf("Queue is empty!\n");
return;
}

int value = q->items[q->front];


if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front = (q->front + 1) % SIZE;
}
printf("Deleted %d\n", value);
}

void display(struct CircularQueue* q) {


if (isEmpty(q)) {
printf("Queue is empty!\n");
return;
}

printf("Queue: ");
int i = q->front;
while (i != q->rear) {
printf("%d ", q->items[i]);
i = (i + 1) % SIZE;
}
printf("%d\n", q->items[q->rear]);
}

int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct CircularQueue q;
initQueue(&q);

insert(&q, 10);
insert(&q, 20);
insert(&q, 30);
insert(&q, 40);
insert(&q, 50);

display(&q);

delete(&q);
delete(&q);

display(&q);

insert(&q, 60);
insert(&q, 70);

display(&q);

return 0;

Q-4) Write a program to implement circular queue using link list.

Code :

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

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

struct CircularQueue {
struct Node *front, *rear;
};

struct Node* createNode(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

struct CircularQueue* createQueue() {


struct CircularQueue* queue = (struct CircularQueue*)malloc(sizeof(struct
CircularQueue));
queue->front = queue->rear = NULL;
return queue;
}

void enqueue(struct CircularQueue* queue, int value) {


struct Node* newNode = createNode(value);
if (queue->rear == NULL) {
queue->front = queue->rear = newNode;
queue->rear->next = queue->front;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
queue->rear->next = queue->front;
}
printf("Enqueued %d\n", value);
}

int dequeue(struct CircularQueue* queue) {


if (queue->front == NULL) {
printf("Queue is empty, cannot dequeue.\n");
return -1;
}

int value;
struct Node* temp = queue->front;

if (queue->front == queue->rear) {
value = queue->front->data;
free(queue->front);
queue->front = queue->rear = NULL;
} else {
value = queue->front->data;
queue->front = queue->front->next;
queue->rear->next = queue->front;
free(temp);
}

printf("Dequeued %d\n", value);


return value;
}

void displayQueue(struct CircularQueue* queue) {


if (queue->front == NULL) {
printf("Queue is empty.\n");
return;
}

struct Node* temp = queue->front;


printf("Queue elements are: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != queue->front);
printf("\n");
}

int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct CircularQueue* queue = createQueue();

enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);

displayQueue(queue);

dequeue(queue);
displayQueue(queue);

enqueue(queue, 50);
displayQueue(queue);

return 0;
}
Q-5) Write a program to implement priority queue using link list.

Code :

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

struct Node {
int data;
int priority;
struct Node* next;
};

struct Node* createNode(int value, int priority) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->priority = priority;
newNode->next = NULL;
return newNode;
}

void enqueue(struct Node** head, int value, int priority) {


struct Node* newNode = createNode(value, priority);
if (*head == NULL || (*head)->priority > priority) {
newNode->next = *head;
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL && temp->next->priority <= priority) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
printf("Enqueued %d with priority %d\n", value, priority);
}

int dequeue(struct Node** head) {


if (*head == NULL) {
printf("Priority Queue is empty, cannot dequeue.\n");
return -1;
}

int value = (*head)->data;


struct Node* temp = *head;
*head = (*head)->next;
free(temp);

printf("Dequeued %d\n", value);


return value;
}
void displayQueue(struct Node* head) {
if (head == NULL) {
printf("Priority Queue is empty.\n");
return;
}

struct Node* temp = head;


printf("Priority Queue elements (value, priority): ");
while (temp != NULL) {
printf("(%d, %d) ", temp->data, temp->priority);
temp = temp->next;
}
printf("\n");
}

int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct Node* priorityQueue = NULL;
enqueue(&priorityQueue, 10, 2);
enqueue(&priorityQueue, 30, 1);
enqueue(&priorityQueue, 20, 3);
enqueue(&priorityQueue, 40, 0);

displayQueue(priorityQueue);

dequeue(&priorityQueue);
displayQueue(priorityQueue);

enqueue(&priorityQueue, 50, 1);


displayQueue(priorityQueue);

return 0;
}

You might also like