DS Practical
DS Practical
//QUEUE
#include <stdio.h>
#include <stdlib.h>
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n"); deque
return -1;
} else {
return queue[front++];
}
}
int peek() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
return -1;
} else {
return queue[front];
}
}
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue elements are:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
int choice, value;
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
value = dequeue();
if (value != -1)
printf("Dequeued: %d\n", value);
break;
case 3:
value = peek();
if (value != -1)
printf("Front element: %d\n", value);
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
//BUBBLE SORT
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void displayArray(int arr[], int n) {
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[100], n;
bubbleSort(arr, n);
displayArray(arr, n);
return 0;
}
PRACTICAL 2:
// Quick Sort
#include <stdio.h>
// Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1), temp;
// Main function
int main() {
int arr[100], n;
quickSort(arr, 0, n - 1);
displayArray(arr, n);
return 0;
}
PRACTICAL 3:
// Quick Sort
#include <stdio.h>
// Main function
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
quickSort(arr, 0, n - 1);
displayArray(arr, n);
return 0;
}
PRACTICAL 4:
//Merge Sort
#include <stdio.h>
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
mergeSort(arr, 0, n - 1);
displayArray(arr, n);
return 0;
}
PRACTICAL 5:
#include <stdio.h>
radixSort(arr, n);
displayArray(arr, n);
return 0;
}