Source Code
Source Code
Source Code
#include <stdio.h>
int main() {
int choice, value;
do {
printf("\n---- Menu ----\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1)
printf("Popped element: %d\n", value);
break;
case 3:
display();
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
if (top == -1) {
printf("Stack Underflow. Cannot pop element.\n");
} else {
value = stack[top];
top--;
}
return value;
}
void display() {
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack elements:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
int main() {
int choice, value;
do {
printf("\n---- Menu ----\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1)
printf("Popped element: %d\n", value);
break;
case 3:
display();
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
int pop() {
int value = -1; // Default value for an empty stack
if (top == stack) {
printf("Stack Underflow. Cannot pop element.\n");
} else {
top--;
value = *top;
}
return value;
}
void display() {
if (top == stack) {
printf("Stack is empty.\n");
} else {
printf("Stack elements:\n");
for (int *ptr = top - 1; ptr >= stack; ptr--) {
printf("%d\n", *ptr);
}
}
}
result[resultIndex] = '\0';
printf("%s\n", result);
}
// Driver code
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
// Function call
infixToPostfix(exp);
return 0;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
int main() {
int n;
printf("Enter the value of n for Fibonacci sequence: ");
scanf("%d", &n);
if (n < 0) {
printf("Fibonacci sequence is not defined for negative numbers.\n");
} else {
printf("Fibonacci sequence up to %d terms:\n", n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
}
return 0;
}
int main() {
int num1, num2;
printf("Enter two numbers to find their GCD: ");
scanf("%d %d", &num1, &num2);
return 0;
}
8. Write a recursive program to implement TOH problem. (Show the output for 3 disks)
#include <stdio.h>
int main() {
int num_disks = 3;
printf("Tower of Hanoi solution for %d disks:\n", num_disks);
towerOfHanoi(num_disks, 'A', 'B', 'C');
return 0;
}
9. Write a menu driven program to illustrate basic operations of Linear queue using array
implementation and pointer implementation.
a) Enqueue
b) Dequeue
c) Display
Stack Implementation:
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void enqueue(Queue *q, int value);
int dequeue(Queue *q);
void display(Queue *q);
int main() {
Queue q;
q.front = -1;
q.rear = -1;
int choice, value;
do {
printf("\nLinear 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 value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
if (q.front == -1) {
printf("Queue is empty, cannot dequeue\n");
} else {
printf("Dequeued element: %d\n", dequeue(&q));
}
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Pointer Implementation:
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void enqueue(Queue *q, int value);
int dequeue(Queue *q);
void display(Queue *q);
int main() {
Queue q;
q.front = NULL;
q.rear = NULL;
int choice, value;
do {
printf("\nLinear 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 value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeue(&q);
if (value != -1) {
printf("Dequeued element: %d\n", value);
}
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
if (q->front == NULL) {
q->rear = NULL;
}
return value;
}
10. Write a menu driven program to illustrate basic operations of circular queue having
following menu:
a) Enqueue
b) Dequeue
c) Traverse
d) Exit
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int main() {
CircularQueue cq;
cq.front = -1;
cq.rear = -1;
int choice, value;
do {
printf("\nCircular Queue Operations\n");
printf("a) Enqueue\n");
printf("b) Dequeue\n");
printf("c) Traverse\n");
printf("d) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&cq, value);
break;
case 'b':
value = dequeue(&cq);
if (value != -1) {
printf("Dequeued element: %d\n", value);
}
break;
case 'c':
traverse(&cq);
break;
case 'd':
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 'd');
return 0;
}
11. Write a program that uses functions to perform the following operations on singly linked list
a) Creation
b) Insertion
1) Insertion at beginning
2) Insertion at specified position
3) Insertion at end
c) Deletion
1) Deletion from the beginning
2) Deletion from the specified position
3) Deletion from the end
d) Traversal.
e) Exit
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, data, position;
while (1) {
printf("\nOperations on Singly Linked List:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at Specified Position\n");
printf("3. Insert at End\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from Specified Position\n");
printf("6. Delete from End\n");
printf("7. Traverse\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at beginning: ");
scanf("%d", &data);
insertBeginning(&head, data);
break;
case 2:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position: ");
scanf("%d", &position);
insertAtPosition(&head, data, position);
break;
case 3:
printf("Enter data to insert at end: ");
scanf("%d", &data);
insertEnd(&head, data);
break;
case 4:
deleteBeginning(&head);
break;
case 5:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteAtPosition(&head, position);
break;
case 6:
deleteEnd(&head);
break;
case 7:
traverse(head);
break;
case 8:
freeList(&head);
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid choice.\n");
}
}
return 0;
}
12. Write a program that uses functions to perform the following operations on circular linked List
a) Creation
b) Insertion
1) Insertion at beginning
2) Insertion at specified position
3) Insertion at end
c) Deletion
1) Deletion from the beginning
2) Deletion from the specified position
3) Deletion from the end
d) Traversal.
e) Exit
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, data, position;
while (1) {
printf("\nOperations on Circular Linked List:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at Specified Position\n");
printf("3. Insert at End\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from Specified Position\n");
printf("6. Delete from End\n");
printf("7. Traverse\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at beginning: ");
scanf("%d", &data);
insertBeginning(&head, data);
break;
case 2:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position: ");
scanf("%d", &position);
insertAtPosition(&head, data, position);
break;
case 3:
printf("Enter data to insert at end: ");
scanf("%d", &data);
insertEnd(&head, data);
break;
case 4:
deleteBeginning(&head);
break;
case 5:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteAtPosition(&head, position);
break;
case 6:
deleteEnd(&head);
break;
case 7:
traverse(head);
break;
case 8:
freeList(&head);
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid choice.\n");
}
}
return 0;
}
13. Write a program to Implement binary tree and traverse tree with user’s choice (Inorder, Preorder,
Postorder).
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* root = NULL;
int choice, data;
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}
int main() {
int arr[] = {12, 45, 78, 23, 56, 34, 89};
int n = sizeof(arr) / sizeof(arr[0]);
int key, result;
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;
}
if (arr[mid] == key) {
return mid; // Return the index if found
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Return -1 if not found
}
int main() {
int arr[] = {12, 23, 34, 45, 56, 78, 89};
int n = sizeof(arr) / sizeof(arr[0]);
int key, result;
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;
}
#define SIZE 10
int main() {
struct Node** hashTable = initializeHashTable();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
// Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Selection Sort
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]);
}
}
// Quick Sort
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);
}
// Merge Sort
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
// Heap Sort
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort
int arr_bubble[n];
for (int i = 0; i < n; i++) {
arr_bubble[i] = arr[i];
}
bubbleSort(arr_bubble, n);
printf("Sorted array using Bubble Sort: ");
printArray(arr_bubble, n);
// Insertion Sort
int arr_insertion[n];
for (int i = 0; i < n; i++) {
arr_insertion[i] = arr[i];
}
insertionSort(arr_insertion, n);
printf("Sorted array using Insertion Sort: ");
printArray(arr_insertion, n);
// Selection Sort
int arr_selection[n];
for (int i = 0; i < n; i++) {
arr_selection[i] = arr[i];
}
selectionSort(arr_selection, n);
printf("Sorted array using Selection Sort: ");
printArray(arr_selection, n);
// Quick Sort
int arr_quick[n];
for (int i = 0; i < n; i++) {
arr_quick[i] = arr[i];
}
quickSort(arr_quick, 0, n - 1);
printf("Sorted array using Quick Sort: ");
printArray(arr_quick, n);
// Merge Sort
int arr_merge[n];
for (int i = 0; i < n; i++) {
arr_merge[i] = arr[i];
}
mergeSort(arr_merge, 0, n - 1);
printf("Sorted array using Merge Sort: ");
printArray(arr_merge, n);
// Heap Sort
int arr_heap[n];
for (int i = 0; i < n; i++) {
arr_heap[i] = arr[i];
}
heapSort(arr_heap, n);
printf("Sorted array using Heap Sort: ");
printArray(arr_heap, n);
return 0;
}
18. Write a program to implement Breadth First Search and Depth First Search in graph.
#include <stdio.h>
#include <stdlib.h>
int main() {
int numVertices = 6; // Example graph has 6 vertices
struct Graph* graph = createGraph(numVertices);
return 0;
}
// If including this edge does not form a cycle, include it in result and increment the index of result for next edge
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
int main() {
int V = 4; // Number of vertices in the graph
int E = 5; // Number of edges in the graph
struct Graph* graph = createGraph(V, E);
// Edge 0-1
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 10;
// Edge 0-2
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;
// Edge 0-3
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;
// Edge 1-3
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 15;
// Edge 2-3
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;
KruskalMST(graph);
return 0;
}
// Function to find the vertex with the minimum distance value, from the set of vertices not yet included in the shortest
path tree
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, min_index;
return min_index;
}
// Function to implement Dijkstra's algorithm for a given graph represented as an adjacency matrix
void dijkstra(int graph[V][V], int src) {
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
int sptSet[V]; // sptSet[i] will be true if vertex i is included in the shortest path tree or shortest distance from src to i is
finalized
int main() {
// Graph representation using adjacency matrix
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}