DSFILE
DSFILE
OUTPUT:
Enqueued: 1
Enqueued: 2
Enqueued: 3
Queue elements: 1 2 3
Dequeued: 1
Queue elements: 2 3
Enqueued: 4
Queue elements: 2 3 4
3.(b) ARRAY IMPLEMENTAION OF CIRCULAR QUEUE
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
typedef struct {
int data[MAX_SIZE];
int front, rear;
} CircularQueue;
void initializeQueue(CircularQueue *queue) {
queue->front = -1;
queue->rear = -1;
}
int isFull(CircularQueue *queue) {
return (queue->front == (queue->rear + 1) % MAX_SIZE);
}
int isEmpty(CircularQueue *queue) {
return (queue->front == -1 && queue->rear == -1);
}
void enqueue(CircularQueue *queue, int element) {
if (isFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isEmpty(queue)) {
queue->front = 0;
queue->rear = 0;
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
queue->data[queue->rear] = element;
printf("%d enqueued to the queue.\n", element);
}
void dequeue(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return;
}
printf("%d dequeued from the queue.\n", queue->data[queue->front]);
if (queue->front == queue->rear) {
initializeQueue(queue);
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
}
void displayQueue(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
int i = queue->front;
do {
printf("%d ", queue->data[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (queue->rear + 1) % MAX_SIZE);
printf("\n");
}
int main() {
CircularQueue queue;
initializeQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
displayQueue(&queue);
dequeue(&queue);
displayQueue(&queue);
enqueue(&queue, 4);
displayQueue(&queue);
enqueue(&queue, 5);
displayQueue(&queue);
enqueue(&queue, 6);
dequeue(&queue);
displayQueue(&queue);
return 0;
}
OUTPUT:
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
Enter the size of the Hash Table:
6.(b) COLLISION RESOLUTION USING LINEAR PROBING
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct HashTable {
int keys[SIZE];
int values[SIZE];
};
int hash(int key) {
return key % SIZE;
}
void initializeHashTable(struct HashTable* table) {
for (int i = 0; i < SIZE; i++) {
table->keys[i] = -1;
table->values[i] = -1;
}
}
void displayHashTable(struct HashTable table) {
printf("Hash Table:\n");
for (int i = 0; i < SIZE; i++) {
if (table.keys[i] != -1) {
printf("Key: %d, Value: %d\n", table.keys[i], table.values[i]);
} else {
printf("Slot %d: Empty\n", i);
}
}
printf("\n");
}
int insert(struct HashTable* table, int key, int value) {
int index = hash(key);
while (table->keys[index] != -1) {
index = (index + 1) % SIZE;
}
table->keys[index] = key;
table->values[index] = value;
return index;
}
int search(struct HashTable table, int key) {
int index = hash(key);
while (table.keys[index] != key) {
if (table.keys[index] == -1) {
return -1;
}
index = (index + 1) % SIZE;
}
return index;
}
int main() {
struct HashTable table;
initializeHashTable(&table);
insert(&table, 5, 50);
insert(&table, 25, 250);
insert(&table, 15, 150);
insert(&table, 35, 350);
displayHashTable(table);
int searchKey = 15;
int result = search(table, searchKey);
if (result != -1) {
printf("Key %d found at index %d\n", searchKey, result);
} else {
printf("Key %d not found\n", searchKey);
}
return 0;
}
OUTPUT:
OUTPUT:
Sorted array:
11 12 22 25 34 64 90
7.(b) SELECTION SORT
#include <stdio.h>
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
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;
swap(&arr[min_idx], &arr[i]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
OUTPUT:
Sorted array:
11 12 22 25 64
7.(c) 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;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
OUTPUT:
Sorted array:
5 6 11 12 13
7.(d) RADIX SORT
#include <stdio.h>
void countingSort(int array[], int size, int place) {
const int max = 10;
int output[size];
int count[max];
for (int i = 0; i < max; ++i)
count[i] = 0;
for (int i = 0; i < size; i++)
count[(array[i] / place) % 10]++;
for (int i = 1; i < max; i++)
count[i] += count[i - 1];
for (int i = size - 1; i >= 0; i--) {
output[count[(array[i] / place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for (int i = 0; i < size; i++)
array[i] = output[i];
}
int getMax(int array[], int size) {
int max = array[0];
for (int i = 1; i < size; i++)
if (array[i] > max)
max = array[i];
return max;
}
void radixSort(int array[], int size) {
int max = getMax(array, size);
for (int place = 1; max / place > 0; place *= 10)
countingSort(array, size, place);
}
int main() {
int array[] = {170, 45, 75, 90, 802, 24, 2, 66};
int size = sizeof(array) / sizeof(array[0]);
radixSort(array, size);
printf("Sorted array: ");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
return 0;
}
OUTPUT:
Sorted array:
11 12 22 25 64