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

Dsa Imp Ques

The document contains 23 questions related to data structures and algorithms programs. The questions cover topics like arrays, linked lists, stacks, queues, sorting, and graphs. The programs are meant as practical exercises to implement common data structures and algorithms using C language.

Uploaded by

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

Dsa Imp Ques

The document contains 23 questions related to data structures and algorithms programs. The questions cover topics like arrays, linked lists, stacks, queues, sorting, and graphs. The programs are meant as practical exercises to implement common data structures and algorithms using C language.

Uploaded by

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

NETAJ SUBHAS UNIVERSITY

OF TECHNOLOGY

DSA PRACTICL FILE


ICCS05
INDEX
1. Write a program to find the mean and the
median of the numbers stored in an array.
2. Write a program to insert one element in an
array and delete an element from an array.
3. Write a program to search for a number in an
array.
4. Write a program to sort an array.
5. Write a program to merge two sorted arrays.
6. Write a program to store the marks obtained by
10 students in 5 courses in a two-
dimensional array.
7. Write a program to implement a linked list.
8. Write a program to insert a node in a linked list
and delete a node from a linked
list.
9. Write a program to print the elements of a
linked list in reverse order without
disturbing the linked list.
10. Write a program to reverse a linked list.
11. Write a program to add two polynomials using
linked lists.
12. Write a program to implement a doubly-linked
list.
13. Write a program to implement a stack using an
array.
14. Write a program to implement a stack using a
linked list.
15. Write a program to implement a queue using
an array.
16. Write a program to implement a queue using a
linked list.
17. Write a program to implement a circular queue
using an array.
18. Write a program to implement a priority queue
using a linked list.
19. Write a program to implement a double-ended
queue using a linked list.
20. Construct Write a program to a graph.
21. Write a program for the insertion sort.
22. Write a program for the selection sort.
23. Write a program for the bubble sort.
24. Write a program for the quick sort.

Q-1) Write a program to find mean and median of the numbers


stored in an array.
#include <stdio.h>

#include <stdlib.h>

int compare(const void* a, const void* b) {

return (*(int*)a - *(int*)b);

float calculateMean(int arr[], int size) {

int sum = 0;

for (int i = 0; i < size; i++) {

sum += arr[i];

}
return (float)sum / size;

float calculateMedian(int arr[], int size) {

qsort(arr, size, sizeof(int), compare);

if (size % 2 != 0) {

return arr[size / 2];

return (float)(arr[(size - 1) / 2] + arr[size / 2]) / 2;

int main() {

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of the array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr[i]);

float mean = calculateMean(arr, size);

float median = calculateMedian(arr, size);

printf("Mean: %.2f\n", mean);

printf("Median: %.2f\n", median);

return 0;

Q-2) Write a program to insert one element in an array and delete an


element from an array
#include <stdio.h>

void displayArray(int arr[], int size) {

printf("Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

void insertElement(int arr[], int* size, int position, int element) {

if (position < 0 || position > *size) {


printf("Invalid position\n");

return;

for (int i = *size; i > position; i--) {

arr[i] = arr[i - 1];

arr[position] = element;

(*size)++;

printf("Element %d inserted at position %d\n", element, position);

void deleteElement(int arr[], int* size, int position) {

if (position < 0 || position >= *size) {

printf("Invalid position\n");

return;

int deletedElement = arr[position];

for (int i = position; i < *size - 1; i++) {

arr[i] = arr[i + 1];

(*size)--;

printf("Element %d deleted from position %d\n", deletedElement, position);

int main() {

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of the array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr[i]);

displayArray(arr, size);

int option, position, element;

printf("Choose an option:\n");

printf("1. Insert an element\n");

printf("2. Delete an element\n");

printf("Option: ");

scanf("%d", &option);

switch (option) {
case 1:

printf("Enter the position to insert: ");

scanf("%d", &position);

printf("Enter the element to insert: ");

scanf("%d", &element);

insertElement(arr, &size, position, element);

break;

case 2:

printf("Enter the position to delete: ");

scanf("%d", &position);

deleteElement(arr, &size, position);

break;

default:

printf("Invalid option\n");

break;

displayArray(arr, size);

return 0;

Q-3) Write a program to search for a number in an array.


#include <stdio.h>

int searchNumber(int arr[], int size, int number) {

for (int i = 0; i < size; i++) {

if (arr[i] == number) {

return i;

return -1;

int main() {

int size;
printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of the array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr[i]);

int number;

printf("Enter the number to search: ");

scanf("%d", &number);

int index = searchNumber(arr, size, number);

if (index != -1) {

printf("Number %d found at index %d\n", number, index);

} else {

printf("Number %d not found in the array\n", number);

return 0;

Q-4) Write a program to sort an array.


#include <stdio.h>

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

swap(&arr[j], &arr[j + 1]);

}
}

void displayArray(int arr[], int size) {

printf("Sorted Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

int main() {

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of the array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr[i]);

bubbleSort(arr, size);

displayArray(arr, size);

return 0;

Q-5) Write a program to merge two sorted arrays.


#include <stdio.h>
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int result[]) {
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2) {
if (arr1[i] <= arr2[j]) {
result[k++] = arr1[i++];
} else {
result[k++] = arr2[j++];
}
}
while (i < size1) {
result[k++] = arr1[i++];
}
while (j < size2) {
result[k++] = arr2[j++];
}
}
void displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int size1, size2;
printf("Enter the size of the first array: ");
scanf("%d", &size1);
printf("Enter the elements of the first array in sorted order:\n");
int arr1[size1];
for (int i = 0; i < size1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter the size of the second array: ");
scanf("%d", &size2);
printf("Enter the elements of the second array in sorted order:\n");
int arr2[size2];
for (int i = 0; i < size2; i++) {
scanf("%d", &arr2[i]);
}
int mergedSize = size1 + size2;
int mergedArray[mergedSize];
mergeArrays(arr1, size1, arr2, size2, mergedArray);
printf("Merged Array: ");
displayArray(mergedArray, mergedSize);
return 0;
}

Q-6) Write a program to store the marks obtained by 10 students in 5


courses in a two-dimensional array.
#include <stdio.h>
#define NUM_STUDENTS 5
#define NUM_COURSES 5
int main() {
int arr[NUM_STUDENTS][NUM_COURSES];
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Enter the marks for student %d:\n", i + 1);
for (int j = 0; j < NUM_COURSES; j++) {
printf("Course %d: ", j + 1);
scanf("%d", &arr[i][j]);
}
}
printf("\nMarks obtained:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Student %d: ", i + 1);
for (int j = 0; j < NUM_COURSES; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Q-7) Write a program to implement a linked
list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return 0;
}
Q-8) Write a program to insert one element in an linked-list and
delete an element from a linked-list.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void displayList(struct Node* head) {

printf("Linked List: ");

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

struct Node* insertElement(struct Node* head, int element) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = element;

newNode->next = head;

head = newNode;

printf("Element %d inserted at the beginning of the linked list\n", element);

return head;

struct Node* deleteElement(struct Node* head, int element) {

struct Node* prev = NULL;

struct Node* current = head;

if (current != NULL && current->data == element) {

head = current->next;

free(current);

printf("Element %d deleted from the linked list\n", element);

return head;

while (current != NULL && current->data != element) {

prev = current;

current = current->next;

if (current != NULL) {

prev->next = current->next;

free(current);
printf("Element %d deleted from the linked list\n", element);

} else {

printf("Element %d not found in the linked list\n", element);

return head;

int main() {

struct Node* head = NULL;

head = insertElement(head, 3);

head = insertElement(head, 7);

head = insertElement(head, 11);

displayList(head);

int option, element;

printf("Choose an option:\n");

printf("1. Insert an element\n");

printf("2. Delete an element\n");

printf("Option: ");

scanf("%d", &option);

switch (option) {

case 1:

printf("Enter the element to insert: ");

scanf("%d", &element);

head = insertElement(head, element);

break;

case 2:

printf("Enter the element to delete: ");

scanf("%d", &element);

head = deleteElement(head, element);

break;

default:

printf("Invalid option\n");

break;

displayList(head);

return 0;

}
Q-9) Write a program to print the elements of a linked list in reverse
order without disturbing the linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
void printReverse(struct Node* head) {
if (head == NULL) {
return;
}
printReverse(head->next);
printf("%d ", head->data);
}
void displayList(struct Node* head) {
printf("Linked List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &data);
insertNode(&head, data);
}
displayList(head);
printf("Reversed Linked List: ");
printReverse(head);
printf("\n");
return 0;
}

Q-10) Write a program to reverse a linked list.


#include <stdio.h>

#include <stdlib.h>

struct Node {
int data;

struct Node* next;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed.\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

return newNode;

void insertNode(struct Node** head, int data) {

struct Node* newNode = createNode(data);

newNode->next = *head;

*head = newNode;

void reverseList(struct Node** head) {

struct Node* prev = NULL;

struct Node* current = *head;

struct Node* next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

*head = prev;

void displayList(struct Node* head) {

printf("Linked List: ");

while (head != NULL) {

printf("%d ", head->data);


head = head->next;

printf("\n");

int main() {

struct Node* head = NULL;

int n, data;

printf("Enter the number of elements: ");

scanf("%d", &n);

for (int i = 0; i < n; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &data);

insertNode(&head, data);

printf("Original ");

displayList(head);

reverseList(&head);

printf("Reversed ");

displayList(head);

return 0;

Q-11) Write a program to add two polynomials using linked lists.


#include <stdio.h>

#include <stdlib.h>

struct Term {

int coefficient;

int exponent;
struct Term* next;

};

struct Polynomial {

struct Term* head;

};

struct Polynomial* createPolynomial() {

struct Polynomial* poly = (struct Polynomial*)malloc(sizeof(struct Polynomial));

poly->head = NULL;

return poly;

void insertTerm(struct Polynomial* poly, int coefficient, int exponent) {

struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));

newTerm->coefficient = coefficient;

newTerm->exponent = exponent;

newTerm->next = NULL;

if (poly->head == NULL) {

poly->head = newTerm;

} else {

struct Term* temp = poly->head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newTerm;

void displayPolynomial(struct Polynomial* poly) {

struct Term* term = poly->head;

if (term == NULL) {

printf("Polynomial is empty.\n");

return;

while (term != NULL) {

printf("(%d)x^%d ", term->coefficient, term->exponent);

if (term->next != NULL) {

printf("+ ");

term = term->next;

}
printf("\n");

struct Polynomial* addPolynomials(struct Polynomial* poly1, struct Polynomial* poly2) {

struct Polynomial* sum = createPolynomial();

struct Term* term1 = poly1->head;

struct Term* term2 = poly2->head;

while (term1 != NULL && term2 != NULL) {

if (term1->exponent > term2->exponent) {

insertTerm(sum, term1->coefficient, term1->exponent);

term1 = term1->next;

} else if (term1->exponent < term2->exponent) {

insertTerm(sum, term2->coefficient, term2->exponent);

term2 = term2->next;

} else {

int coefficientSum = term1->coefficient + term2->coefficient;

if (coefficientSum != 0) {

insertTerm(sum, coefficientSum, term1->exponent);

term1 = term1->next;

term2 = term2->next;

while (term1 != NULL) {

insertTerm(sum, term1->coefficient, term1->exponent);

term1 = term1->next;

while (term2 != NULL) {

insertTerm(sum, term2->coefficient, term2->exponent);

term2 = term2->next;

return sum;

void freePolynomial(struct Polynomial* poly) {

struct Term* term = poly->head;

while (term != NULL) {

struct Term* temp = term;

term = term->next;

free(temp);
}

free(poly);

int main() {

struct Polynomial* poly1 = createPolynomial();

struct Polynomial* poly2 = createPolynomial();

struct Polynomial* sum;

insertTerm(poly1, 3, 2);

insertTerm(poly1, 2, 1);

insertTerm(poly1, 5, 0);

insertTerm(poly2, 4, 3);

insertTerm(poly2, -1, 2);

insertTerm(poly2, 2, 0);

printf("Polynomial 1: ");

displayPolynomial(poly1);

printf("Polynomial 2: ");

displayPolynomial(poly2);

sum = addPolynomials(poly1, poly2);

printf("Sum: ");

displayPolynomial(sum);

freePolynomial(poly1);

freePolynomial(poly2);

freePolynomial(sum);

return 0;

Q-12) Write a program to implement a doubly-linked list.


#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};
struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed.\n");

exit(1);

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

void insertAtBeginning(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

newNode->next = *head;

(*head)->prev = newNode;

*head = newNode;

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

}
void displayForward(struct Node* head) {

if (head == NULL) {

printf("Doubly-linked List is empty.\n");

return;

printf("Doubly-linked List (Forward): ");

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

void displayReverse(struct Node* head) {

if (head == NULL) {

printf("Doubly-linked List is empty.\n");

return;

while (head->next != NULL) {

head = head->next;

printf("Doubly-linked List (Reverse): ");

while (head != NULL) {

printf("%d ", head->data);

head = head->prev;

printf("\n");

void freeList(struct Node* head) {

struct Node* temp;

while (head != NULL) {

temp = head;

head = head->next;

free(temp);

}
}

int main() {

struct Node* head = NULL;

int numElements, element;

printf("Enter the number of elements: ");

scanf("%d", &numElements);

printf("Enter the elements:\n");

for (int i = 0; i < numElements; i++) {

scanf("%d", &element);

insertAtEnd(&head, element);

displayForward(head);

displayReverse(head);

freeList(head);

return 0;

Q-13) Write a program to implement a stack using an array.


#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

struct Stack {

int arr[MAX_SIZE];

int top;

};

void createStack(struct Stack* stack) {

stack->top = -1;

int isEmpty(struct Stack* stack) {

return (stack->top == -1);

}
int isFull(struct Stack* stack) {

return (stack->top == MAX_SIZE - 1);

void push(struct Stack* stack, int element) {

if (isFull(stack)) {

printf("Stack Overflow: Cannot push element %d, stack is full.\n", element);

return;

stack->arr[++stack->top] = element;

printf("Element %d pushed to the stack.\n", element);

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow: Cannot pop element, stack is empty.\n");

return -1;

int element = stack->arr[stack->top--];

printf("Element %d popped from the stack.\n", element);

return element;

int peek(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty.\n");

return -1;

return stack->arr[stack->top];

void display(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty.\n");

return;

printf("Stack elements: ");

for (int i = stack->top; i >= 0; i--) {

printf("%d ", stack->arr[i]);

}
printf("\n");

int main() {

struct Stack stack;

createStack(&stack);

int choice, element;

while (1) {

printf("\nStack Operations:\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Peek\n");

printf("4. Display\n");

printf("5. Quit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the element to push: ");

scanf("%d", &element);

push(&stack, element);

break;

case 2:

pop(&stack);

break;

case 3:

element = peek(&stack);

printf("Top element: %d\n", element);

break;

case 4:

display(&stack);

break;

case 5:

printf("Quitting the program.\n");

return 0;

default:

printf("Invalid choice. Please try again.\n");


}

return 0;

Q-14) Write a program to implement a stack using a linked list.


#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed.\n");

exit(1);

newNode->data = data;-

newNode->next = NULL;

return newNode;
}

int isEmpty(struct Node* top) {

return (top == NULL);

void push(struct Node** top, int data) {

struct Node* newNode = createNode(data);

newNode->next = *top;

*top = newNode;

printf("Element %d pushed to the stack.\n", data);

int pop(struct Node** top) {

if (isEmpty(*top)) {

printf("Stack Underflow: Cannot pop element, stack is empty.\n");

return -1;

struct Node* temp = *top;

int data = temp->data;

*top = (*top)->next;

free(temp);

printf("Element %d popped from the stack.\n", data);

return data;

int peek(struct Node* top) {

if (isEmpty(top)) {

printf("Stack is empty.\n");

return -1;

return top->data;

void display(struct Node* top) {

if (isEmpty(top)) {

printf("Stack is empty.\n");

return;

printf("Stack elements: ");

while (top != NULL) {

printf("%d ", top->data);

top = top->next;

printf("\n");

void freeStack(struct Node* top) {


struct Node* temp;

while (top != NULL) {

temp = top;

top = top->next;

free(temp);

int main() {

struct Node* top = NULL;

int choice, element;

while (1) {

printf("\nStack Operations:\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Peek\n");

printf("4. Display\n");

printf("5. Quit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the element to push: ");

scanf("%d", &element);

push(&top, element);

break;

case 2:

pop(&top);

break;

case 3:

element = peek(top);

printf("Top element: %d\n", element);

break;

case 4:

display(top);

break;

case 5:

printf("Quitting the program.\n");

freeStack(top);

return 0;

default:

printf("Invalid choice. Please try again.\n");

}
}

Q-15) Write a program to implement a queue using an array.


#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

typedef struct {

int items[MAX_SIZE];

int front;

int rear;

} Queue;

Queue* createQueue() {

Queue* queue = (Queue*)malloc(sizeof(Queue));

queue->front = -1;

queue->rear = -1;

return queue;

int isFull(Queue* queue) {

return queue->rear == MAX_SIZE - 1;


}

int isEmpty(Queue* queue) {

return queue->front == -1;

void enqueue(Queue* queue, int item) {

if (isFull(queue)) {

printf("Queue is full\n");

return;

if (isEmpty(queue))

queue->front = 0;

queue->rear++;

queue->items[queue->rear] = item;

int dequeue(Queue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty\n");

return -1;

int item = queue->items[queue->front];

if (queue->front == queue->rear)

queue->front = queue->rear = -1;

else

queue->front++;

return item;

void display(Queue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty\n");

return;

printf("Queue elements are: ");

for (int i = queue->front; i <= queue->rear; i++)

printf("%d ", queue->items[i]);

printf("\n");

}
int main() {

Queue* queue = createQueue();

int choice, item;

do {

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 the element to enqueue: ");

scanf("%d", &item);

enqueue(queue, item);

break;

case 2:

item = dequeue(queue);

if (item != -1)

printf("Dequeued element: %d\n", item);

break;

case 3:

display(queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while (choice != 4);

return 0;

}
Q-16) Write a program to implement a queue using a linked-list.
#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct {

Node* front;

Node* rear;

} Queue;

Queue* createQueue() {

Queue* queue = (Queue*)malloc(sizeof(Queue));

queue->front = queue->rear = NULL;

return queue;

}
int isEmpty(Queue* queue) {

return queue->front == NULL;

void enqueue(Queue* queue, int item) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = item;

newNode->next = NULL;

if (isEmpty(queue)) {

queue->front = queue->rear = newNode;

} else {

queue->rear->next = newNode;

queue->rear = newNode;

int dequeue(Queue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty\n");

return -1;

Node* frontNode = queue->front;

int item = frontNode->data;

queue->front = frontNode->next;

if (queue->front == NULL)

queue->rear = NULL;

free(frontNode);

return item;

void display(Queue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty\n");

return;

Node* current = queue->front;

printf("Queue elements are: ");

while (current != NULL) {

printf("%d ", current->data);


current = current->next;

printf("\n");

int main() {

Queue* queue = createQueue();

int choice, item;

do {

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 the element to enqueue: ");

scanf("%d", &item);

enqueue(queue, item);

break;

case 2:

item = dequeue(queue);

if (item != -1)

printf("Dequeued element: %d\n", item);

break;

case 3:

display(queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while (choice != 4);


return 0;

Q-17) Write a program to implement a circular queue using an array.


#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 5

typedef struct {

int items[MAX_SIZE];

int front;

int rear;

} CircularQueue;

CircularQueue* createCircularQueue() {

CircularQueue* queue = (CircularQueue*)malloc(sizeof(CircularQueue));

queue->front = -1;

queue->rear = -1;

return queue;

int isFull(CircularQueue* queue) {

return ((queue->rear + 1) % MAX_SIZE == queue->front);

}
int isEmpty(CircularQueue* queue) {

return (queue->front == -1 && queue->rear == -1);

void enqueue(CircularQueue* queue, int item) {

if (isFull(queue)) {

printf("Circular queue is full\n");

return;

if (isEmpty(queue))

queue->front = 0;

queue->rear = (queue->rear + 1) % MAX_SIZE;

queue->items[queue->rear] = item;

int dequeue(CircularQueue* queue) {

if (isEmpty(queue)) {

printf("Circular queue is empty\n");

return -1;

int item = queue->items[queue->front];

if (queue->front == queue->rear)

queue->front = queue->rear = -1;

else

queue->front = (queue->front + 1) % MAX_SIZE;

return item;

void display(CircularQueue* queue) {

if (isEmpty(queue)) {

printf("Circular queue is empty\n");

return;

int i = queue->front;

printf("Circular queue elements are: ");

if (queue->front <= queue->rear) {

while (i <= queue->rear) {

printf("%d ", queue->items[i]);

i++;

} else {

while (i < MAX_SIZE) {

printf("%d ", queue->items[i]);


i++;

i = 0;

while (i <= queue->rear) {

printf("%d ", queue->items[i]);

i++;

printf("\n");

int main() {

CircularQueue* queue = createCircularQueue();

int choice, item;

do {

printf("\nCircular Queue 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 the element to enqueue: ");

scanf("%d", &item);

enqueue(queue, item);

break;

case 2:

item = dequeue(queue);

if (item != -1)

printf("Dequeued element: %d\n", item);

break;

case 3:

display(queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice. Try again.\n");


}

} while (choice != 4);

return 0;

Q-18) Write a program to implement a priority queue using a linked


list.
#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

int priority;

struct Node* next;

} Node;

typedef struct {

Node* front;

} PriorityQueue;

PriorityQueue* createPriorityQueue() {

PriorityQueue* queue = (PriorityQueue*)malloc(sizeof(PriorityQueue));

queue->front = NULL;

return queue;

int isEmpty(PriorityQueue* queue) {


return queue->front == NULL;

void insert(PriorityQueue* queue, int item, int priority) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = item;

newNode->priority = priority;

newNode->next = NULL;

if (isEmpty(queue) || priority < queue->front->priority) {

newNode->next = queue->front;

queue->front = newNode;

} else {

Node* current = queue->front;

while (current->next != NULL && current->next->priority <= priority)

current = current->next;

newNode->next = current->next;

current->next = newNode;

int delete(PriorityQueue* queue) {

if (isEmpty(queue)) {

printf("Priority queue is empty\n");

return -1;

Node* frontNode = queue->front;

int item = frontNode->data;

queue->front = frontNode->next;

free(frontNode);

return item;

void display(PriorityQueue* queue) {

if (isEmpty(queue)) {

printf("Priority queue is empty\n");

return;

Node* current = queue->front;

printf("Priority queue elements are: ");

while (current != NULL) {

printf("%d ", current->data);


current = current->next;

printf("\n");

int main() {

PriorityQueue* queue = createPriorityQueue();

int choice, item, priority;

do {

printf("\nPriority Queue Operations\n");

printf("1. Insert\n");

printf("2. Delete\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the element to insert: ");

scanf("%d", &item);

printf("Enter the priority of the element: ");

scanf("%d", &priority);

insert(queue, item, priority);

break;

case 2:

item = delete(queue);

if (item != -1)

printf("Deleted element: %d\n", item);

break;

case 3:

display(queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while (choice != 4);


return 0;

Q-19) Write a program to implement a double- ended queue using a


linked list.
#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* prev;

struct Node* next;

} Node;

typedef struct {

Node* front;

Node* rear;

} Deque;

Deque* createDeque() {

Deque* deque = (Deque*)malloc(sizeof(Deque));

deque->front = deque->rear = NULL;

return deque;

}
int isEmpty(Deque* deque) {

return deque->front == NULL;

void insertFront(Deque* deque, int item) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = item;

newNode->prev = NULL;

newNode->next = deque->front;

if (isEmpty(deque))

deque->rear = newNode;

else

deque->front->prev = newNode;

deque->front = newNode;

void insertRear(Deque* deque, int item) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = item;

newNode->prev = deque->rear;

newNode->next = NULL;

if (isEmpty(deque))

deque->front = newNode;

else

deque->rear->next = newNode;

deque->rear = newNode;

int deleteFront(Deque* deque) {

if (isEmpty(deque)) {

printf("Double-ended queue is empty\n");

return -1;

Node* frontNode = deque->front;

int item = frontNode->data;

deque->front = frontNode->next;

if (deque->front == NULL)

deque->rear = NULL;

else
deque->front->prev = NULL;

free(frontNode);

return item;

int deleteRear(Deque* deque) {

if (isEmpty(deque)) {

printf("Double-ended queue is empty\n");

return -1;

Node* rearNode = deque->rear;

int item = rearNode->data;

deque->rear = rearNode->prev;

if (deque->rear == NULL)

deque->front = NULL;

else

deque->rear->next = NULL;

free(rearNode);

return item;

void display(Deque* deque) {

if (isEmpty(deque)) {

printf("Double-ended queue is empty\n");

return;

Node* current = deque->front;

printf("Double-ended queue elements are: ");

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

printf("\n");

int main() {

Deque* deque = createDeque();

int choice, item;

do {
printf("\nDouble-Ended Queue Operations\n");

printf("1. Insert at Front\n");

printf("2. Insert at Rear\n");

printf("3. Delete from Front\n");

printf("4. Delete from Rear\n");

printf("5. Display\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the element to insert at the front: ");

scanf("%d", &item);

insertFront(deque, item);

break;

case 2:

printf("Enter the element to insert at the rear: ");

scanf("%d", &item);

insertRear(deque, item);

break;

case 3:

item = deleteFront(deque);

if (item != -1)

printf("Deleted element from front: %d\n", item);

break;

case 4:

item = deleteRear(deque);

if (item != -1)

printf("Deleted element from rear: %d\n", item);

break;

case 5:

display(deque);

break;

case 6:

printf("Exiting...\n");

break;
default:

printf("Invalid choice. Try again.\n");

} while (choice != 6);

return 0;

Q-20) Write a Program to construct a graph.


#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int vertex;

struct Node* next;

} Node;

typedef struct {

int numVertices;

Node** adjacencyList;

} Graph;

Graph* createGraph(int numVertices) {

Graph* graph = (Graph*)malloc(sizeof(Graph));

graph->numVertices = numVertices;

graph->adjacencyList = (Node**)malloc(numVertices * sizeof(Node*));

for (int i = 0; i < numVertices; i++)

graph->adjacencyList[i] = NULL;

return graph;

void addEdge(Graph* graph, int src, int dest) {


Node* newNode = (Node*)malloc(sizeof(Node));

newNode->vertex = dest;

newNode->next = graph->adjacencyList[src];

graph->adjacencyList[src] = newNode;

newNode = (Node*)malloc(sizeof(Node));

newNode->vertex = src;

newNode->next = graph->adjacencyList[dest];

graph->adjacencyList[dest] = newNode;

void printGraph(Graph* graph) {

for (int i = 0; i < graph->numVertices; i++) {

Node* current = graph->adjacencyList[i];

printf("Adjacency list of vertex %d: ", i);

while (current) {

printf("%d -> ", current->vertex);

current = current->next;

printf("NULL\n");

int main() {

int numVertices, numEdges;

printf("Enter the number of vertices in the graph: ");

scanf("%d", &numVertices);

Graph* graph = createGraph(numVertices);

printf("Enter the number of edges in the graph: ");

scanf("%d", &numEdges);

printf("Enter the edges (source vertex, destination vertex):\n");

for (int i = 0; i < numEdges; i++) {

int src, dest;

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

printf("\nGraph created successfully!\n");

printGraph(graph);

return 0;

}
Q-21) Write a program for the insertion sort.
#include <stdio.h>

void insertionSort(int arr[], int n) {

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

void printArray(int arr[], int n) {

int i;

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Original array: ");

printArray(arr, n);
insertionSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Q-22) Write a program for the selection sort.


#include <stdio.h>

void selectionSort(int arr[], int n) {

int i, j, min_idx;

for (i = 0; i < n - 1; i++) {

min_idx = i;

for (j = i + 1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

int temp = arr[i];

arr[i] = arr[min_idx];

arr[min_idx] = temp;

void printArray(int arr[], int n) {

int i;

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Original array: ");

printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;

Q-23) Write a program for bubble sort.


#include <stdio.h>

void bubbleSort(int arr[], int n) {

int i, j;

for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

void printArray(int arr[], int n) {

int i;

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Original array: ");

printArray(arr, n);

bubbleSort(arr, n);
printf("Sorted array: ");

printArray(arr, n);

return 0;

Q-24) Write a program for quick sort.


#include <stdio.h>

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

if (arr[j] < pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

void quickSort(int arr[], int low, int high) {

if (low < high) {

int partitionIndex = partition(arr, low, high);

quickSort(arr, low, partitionIndex - 1);

quickSort(arr, partitionIndex + 1, high);

void printArray(int arr[], int n) {

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");
}

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Original array: ");

printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: ");

printArray(arr, n);

return 0;

You might also like