DS Lab Assignment 4
DS Lab Assignment 4
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 Queue {
struct Node *front, *rear;
};
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);
}
if (q->front == NULL) {
printf("Queue is empty, nothing to delete.\n");
return;
}
if (q->front == NULL) {
q->rear = NULL;
}
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;
}
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
if (q->front == NULL)
return -1;
if (q->front == NULL)
q->rear = NULL;
free(temp);
return data;
}
int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
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;
};
if (q->front == -1)
q->front = 0;
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;
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct CircularQueue {
struct Node *front, *rear;
};
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);
}
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;
};
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);
return 0;
}