0% found this document useful (0 votes)
22 views25 pages

DS Practical File P

Uploaded by

hostelers2369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

DS Practical File P

Uploaded by

hostelers2369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Practical File‬

‭1.‬‭Linear Search‬
‭#include <stdio.h>‬

‭void linearSearch(int arr[], int n, int key) {‬


‭int i;‬
‭for (i = 0; i < n; i++) {‬
‭if (arr[i] == key) {‬
‭printf("Element %d found at index %d\n", key, i);‬
‭return;‬
‭}‬
‭}‬
‭printf("Element %d not found in the array.\n", key);‬
‭}‬

‭int main() {‬
‭int n, i, key;‬

/‭/ Input: Size of the array‬


‭printf("Enter the size of the array: ");‬
‭scanf("%d", &n);‬

‭int arr[n]; // Declare an array of size 'n'‬

/‭/ Input: Elements of the array‬


‭printf("Enter %d elements of the array: ", n);‬
‭for (i = 0; i < n; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬
‭// Input: The element to search for‬
‭printf("Enter the element to search for: ");‬
‭scanf("%d", &key);‬

/‭/ Call the linear search function‬


‭linearSearch(arr, n, key);‬

‭return 0;‬
‭}‬
‭2.‬‭Binary Search‬
‭#include <stdio.h>‬

‭void binarySearch(int arr[], int n, int key) {‬


‭int low = 0, high = n - 1, mid;‬

‭while (low <= high) {‬


‭mid = (low + high) / 2;‬

‭if (arr[mid] == key) {‬


‭printf("Element %d found at index %d\n", key, mid);‬
‭return;‬
‭}‬
‭else if (arr[mid] < key) {‬
‭low = mid + 1;‬
‭}‬
‭else {‬
‭high = mid - 1;‬
‭}‬
}‭ ‬
‭printf("Element %d not found in the array.\n", key);‬
‭}‬

‭int main() {‬
‭int n, i, key;‬

/‭/ Input: Size of the array‬


‭printf("Enter the size of the array: ");‬
‭scanf("%d", &n);‬

‭int arr[n]; // Declare an array of size 'n'‬

/‭/ Input: Elements of the array (must be sorted)‬


‭printf("Enter %d sorted elements of the array: ", n);‬
‭for (i = 0; i < n; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬
/‭/ Input: The element to search for‬
‭printf("Enter the element to search for: ");‬
‭scanf("%d", &key);‬

/‭/ Call the binary search function‬


‭binarySearch(arr, n, key);‬

‭return 0;‬
‭}‬

‭ .‬‭Insertion Sort‬
3
‭#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;‬

/‭/ Move elements of arr[0..i-1], that are greater than key,‬


‭// to one position ahead of their current position‬
‭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, i;‬

/‭/ Input: Size of the array‬


‭printf("Enter the size of the array: ");‬
‭scanf("%d", &n);‬

‭int arr[n]; // Declare an array of size 'n'‬

/‭/ Input: Elements of the array‬


‭printf("Enter %d elements of the array: ", n);‬
‭for (i = 0; i < n; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬

/‭/ Call the insertion sort function‬


‭insertionSort(arr, n);‬

‭ rintf("Sorted array: ");‬


p
‭printArray(arr, n);‬

‭return 0;‬
‭}‬

‭ .‬‭Selection Sort‬
4
‭#include <stdio.h>‬

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


‭int i, j, minIndex, temp;‬

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


‭minIndex = i; // Assume the first unsorted element is the minimum‬

/‭/ Find the minimum element in the unsorted part of the array‬
‭for (j = i + 1; j < n; j++) {‬
‭if (arr[j] < arr[minIndex]) {‬
‭minIndex = j;‬
‭}‬
‭}‬

/‭/ Swap the found minimum element with the first element of the‬
‭unsorted part‬
‭temp = arr[minIndex];‬
‭arr[minIndex] = arr[i];‬
‭arr[i] = 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, i;‬

/‭/ Input: Size of the array‬


‭printf("Enter the size of the array: ");‬
‭scanf("%d", &n);‬

‭int arr[n]; // Declare an array of size 'n'‬

/‭/ Input: Elements of the array‬


‭printf("Enter %d elements of the array: ", n);‬
‭for (i = 0; i < n; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬

/‭/ Call the selection sort function‬


‭selectionSort(arr, n);‬

‭ rintf("Sorted array: ");‬


p
‭printArray(arr, n);‬

‭return 0;‬
‭}‬

‭ .‬‭Quick Sort‬
5
‭#include <stdio.h>‬

/‭/ Function to swap two elements‬


‭void swap(int *a, int *b) {‬
‭int temp = *a;‬
‭*a = *b;‬
‭*b = temp;‬
‭}‬

/‭/ Partition function to rearrange the elements‬


‭int partition(int arr[], int low, int high) {‬
‭int pivot = arr[high]; // Choose the last element as the pivot‬
‭int i = low - 1; // Index of smaller element‬

‭for (int j = low; j < high; j++) {‬


‭if (arr[j] < pivot) {‬
‭i++;‬
‭swap(&arr[i], &arr[j]); // Swap if the current element is smaller‬
‭than the pivot‬
‭}‬
‭}‬
‭swap(&arr[i + 1], &arr[high]); // Swap the pivot element with the‬
‭element at i+1‬
‭return i + 1; // Return the partitioning index‬
‭}‬

/‭/ Recursive Quick Sort function‬


‭void quickSort(int arr[], int low, int high) {‬
‭if (low < high) {‬
‭int pi = partition(arr, low, high); // Partition the array‬

/‭/ Recursively sort the elements before and after partition‬


‭quickSort(arr, low, pi - 1);‬
‭quickSort(arr, pi + 1, high);‬
‭}‬
‭}‬

/‭/ Function to print the array‬


‭void printArray(int arr[], int n) {‬
‭for (int i = 0; i < n; i++) {‬
‭printf("%d ", arr[i]);‬
‭}‬
‭printf("\n");‬
‭}‬

‭int main() {‬
‭int n;‬

/‭/ Input: Size of the array‬


‭printf("Enter the size of the array: ");‬
‭scanf("%d", &n);‬

‭int arr[n]; // Declare an array of size 'n'‬

/‭/ Input: Elements of the array‬


‭printf("Enter %d elements of the array: ", n);‬
‭for (int i = 0; i < n; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬

/‭/ Call the quick sort function‬


‭quickSort(arr, 0, n - 1);‬

‭ rintf("Sorted array: ");‬


p
‭printArray(arr, n);‬

‭return 0;‬
‭}‬

‭ .‬‭Heap Sort‬
6
‭#include <stdio.h>‬

‭// Function to swap two elements‬


‭void swap(int *a, int *b) {‬
‭int temp = *a;‬
‭*a = *b;‬
‭*b = temp;‬
‭}‬

/‭/ Function to "heapify" a subtree rooted at node 'i' in an array of size 'n'‬
‭void heapify(int arr[], int n, int i) {‬
‭int largest = i; // Assume the root is the largest‬
‭int left = 2 * i + 1; // Left child‬
‭int right = 2 * i + 2; // Right child‬

/‭/ Check if the left child exists and is larger than the root‬
‭if (left < n && arr[left] > arr[largest]) {‬
‭largest = left;‬
‭}‬

/‭/ Check if the right child exists and is larger than the current largest‬
‭if (right < n && arr[right] > arr[largest]) {‬
‭largest = right;‬
‭}‬

/‭/ If the largest is not the root, swap and heapify the affected subtree‬
‭if (largest != i) {‬
‭swap(&arr[i], &arr[largest]);‬
‭heapify(arr, n, largest);‬
‭}‬
‭}‬

/‭/ Function to perform heap sort‬


‭void heapSort(int arr[], int n) {‬
‭// Step 1: Build a max heap‬
‭for (int i = n / 2 - 1; i >= 0; i--) {‬
‭heapify(arr, n, i);‬
‭}‬

/‭/ Step 2: Extract elements from the heap‬


‭for (int i = n - 1; i > 0; i--) {‬
‭swap(&arr[0], &arr[i]); // Move current root to end‬
‭heapify(arr, i, 0); // Call heapify on the reduced heap‬
‭}‬
‭}‬

/‭/ Function to print the array‬


‭void printArray(int arr[], int n) {‬
‭for (int i = 0; i < n; i++) {‬
‭printf("%d ", arr[i]);‬
‭}‬
‭printf("\n");‬
‭}‬
‭int main() {‬
‭int n;‬

/‭/ Input: Size of the array‬


‭printf("Enter the size of the array: ");‬
‭scanf("%d", &n);‬

‭int arr[n]; // Declare an array of size 'n'‬

/‭/ Input: Elements of the array‬


‭printf("Enter %d elements of the array: ", n);‬
‭for (int i = 0; i < n; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬
‭// Call the heap sort function‬
‭heapSort(arr, n);‬

‭ rintf("Sorted array: ");‬


p
‭printArray(arr, n);‬

‭return 0;‬
‭}‬

‭ .‬‭Multiplication of two Matrices‬


7
‭#include <stdio.h>‬

v‭ oid multiplyMatrices(int A[][10], int B[][10], int result[][10], int r1, int c1,‬
‭int r2, int c2) {‬
‭// Check if multiplication is possible‬
‭if (c1 != r2) {‬
‭ rintf("Matrix multiplication is not possible. Number of columns of‬
p
‭first matrix must equal number of rows of second matrix.\n");‬
‭return;‬
‭}‬
‭// Initialize the result matrix with zeros‬
‭for (int i = 0; i < r1; i++) {‬
‭for (int j = 0; j < c2; j++) {‬
‭result[i][j] = 0;‬
‭}‬
‭}‬
‭// Multiply the matrices‬
‭for (int i = 0; i < r1; i++) {‬
‭for (int j = 0; j < c2; j++) {‬
‭for (int k = 0; k < c1; k++) {‬
‭result[i][j] += A[i][k] * B[k][j];‬
‭}‬
‭}‬
‭}‬
‭}‬
‭void printMatrix(int matrix[][10], int rows, int columns) {‬
‭for (int i = 0; i < rows; i++) {‬
‭for (int j = 0; j < columns; j++) {‬
‭printf("%d ", matrix[i][j]);‬
‭}‬
‭printf("\n");‬
‭}‬
‭}‬
‭int main() {‬
‭int r1, c1, r2, c2;‬
‭// Input: Rows and columns of first matrix‬
‭printf("Enter the number of rows and columns of the first matrix: ");‬
‭scanf("%d %d", &r1, &c1);‬

‭int A[10][10]; // Declare first matrix‬

/‭/ Input: Elements of the first matrix‬


‭printf("Enter the elements of the first matrix (%d x %d):\n", r1, c1);‬
‭for (int i = 0; i < r1; i++) {‬
‭for (int j = 0; j < c1; j++) {‬
‭scanf("%d", &A[i][j]);‬
‭}‬
}‭ ‬
‭// Input: Rows and columns of second matrix‬
‭printf("Enter the number of rows and columns of the second matrix:‬
‭");‬
‭scanf("%d %d", &r2, &c2);‬

‭int B[10][10]; // Declare second matrix‬

/‭/ Input: Elements of the second matrix‬


‭printf("Enter the elements of the second matrix (%d x %d):\n", r2,‬
‭c2);‬
‭for (int i = 0; i < r2; i++) {‬
‭for (int j = 0; j < c2; j++) {‬
‭scanf("%d", &B[i][j]);‬
‭}‬
‭}‬
‭int result[10][10]; // Declare result matrix‬

/‭/ Call the function to multiply the matrices‬


‭multiplyMatrices(A, B, result, r1, c1, r2, c2);‬

/‭/ Print the resulting matrix‬


‭if (c1 == r2) {‬
‭printf("The product of the matrices is:\n");‬
‭printMatrix(result, r1, c2);‬
‭}‬

‭return 0;‬
‭}‬

‭8.‬‭Implementation of Linked List with following options :‬


‭a.‬‭Insertion‬
‭b.‬‭Deletion‬
‭c.‬‭Searching‬
‭d.‬‭Traversing‬
‭#include <stdio.h>‬
‭#include <stdlib.h>‬
/‭/ Structure of a node‬
‭struct Node {‬
‭int data;‬
‭struct Node* next;‬
‭};‬

/‭/ Function to insert a node at the beginning of the list‬


‭void insertAtBeginning(struct Node** head, int data) {‬
‭struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));‬
‭newNode->data = data;‬
‭newNode->next = *head;‬
‭*head = newNode;‬
‭printf("Inserted %d at the beginning.\n", data);‬
‭}‬

/‭/ Function to insert a node at the end of the list‬


‭void insertAtEnd(struct Node** head, int data) {‬
‭struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));‬
‭newNode->data = data;‬
‭newNode->next = NULL;‬

‭if (*head == NULL) {‬


‭*head = newNode;‬
‭} else {‬
‭struct Node* temp = *head;‬
‭while (temp->next != NULL) {‬
‭temp = temp->next;‬
‭}‬
‭temp->next = newNode;‬
‭}‬
‭printf("Inserted %d at the end.\n", data);‬
‭}‬

/‭/ Function to insert a node after a specific node‬


‭void insertAfterNode(struct Node* prevNode, int data) {‬
‭if (prevNode == NULL) {‬
‭printf("Previous node cannot be NULL.\n");‬
‭return;‬
‭}‬
‭struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));‬
‭ ewNode->data = data;‬
n
‭newNode->next = prevNode->next;‬
‭prevNode->next = newNode;‬
‭printf("Inserted %d after node with data %d.\n", data,‬
‭prevNode->data);‬
‭}‬

/‭/ Function to delete a node by value‬


‭void deleteNodeByValue(struct Node** head, int value) {‬
‭struct Node* temp = *head;‬
‭struct Node* prev = NULL;‬

‭if (temp != NULL && temp->data == value) {‬


‭*head = temp->next;‬
‭free(temp);‬
‭printf("Deleted node with value %d.\n", value);‬
‭return;‬
‭}‬

‭while (temp != NULL && temp->data != value) {‬


‭prev = temp;‬
‭temp = temp->next;‬
‭}‬

‭if (temp == NULL) {‬


‭printf("Node with value %d not found.\n", value);‬
‭return;‬
‭}‬

‭ rev->next = temp->next;‬
p
‭free(temp);‬
‭printf("Deleted node with value %d.\n", value);‬
‭}‬

/‭/ Function to delete a node at a given position‬


‭void deleteNodeAtPosition(struct Node** head, int position) {‬
‭if (*head == NULL) {‬
‭printf("List is empty.\n");‬
‭return;‬
‭}‬
‭struct Node* temp = *head;‬

‭if (position == 0) {‬
‭*head = temp->next;‬
‭free(temp);‬
‭printf("Deleted node at position %d.\n", position);‬
‭return;‬
‭}‬

‭for (int i = 0; temp != NULL && i < position - 1; i++) {‬


‭temp = temp->next;‬
‭}‬

‭if (temp == NULL || temp->next == NULL) {‬


‭printf("Position %d does not exist.\n", position);‬
‭return;‬
‭}‬

s‭ truct Node* next = temp->next->next;‬


‭free(temp->next);‬
‭temp->next = next;‬
‭printf("Deleted node at position %d.\n", position);‬
‭}‬

/‭/ Function to search for a node with a specific value‬


‭void searchNode(struct Node* head, int value) {‬
‭struct Node* temp = head;‬
‭int position = 0;‬

‭while (temp != NULL) {‬


‭if (temp->data == value) {‬
‭printf("Value %d found at position %d.\n", value, position);‬
‭return;‬
‭}‬
‭temp = temp->next;‬
‭position++;‬
‭}‬
‭printf("Value %d not found in the list.\n", value);‬
‭}‬
/‭/ Function to traverse and print the linked list‬
‭void traverseList(struct Node* head) {‬
‭if (head == NULL) {‬
‭printf("The list is empty.\n");‬
‭return;‬
‭}‬

s‭ truct Node* temp = head;‬


‭printf("Linked list: ");‬
‭while (temp != NULL) {‬
‭printf("%d -> ", temp->data);‬
‭temp = temp->next;‬
‭}‬
‭printf("NULL\n");‬
‭}‬

/‭/ Printing of linked list‬


‭void printList(struct Node* head) {‬
‭struct Node* temp = head;‬
‭while (temp != NULL) {‬
‭printf("%d -> ", temp->data);‬
‭temp = temp->next;‬
‭}‬
‭printf("NULL\n"); // Indicate the end of the list‬
‭}‬

/‭/ Main function‬


‭int main() {‬
‭struct Node* head = NULL;‬
‭int choice, data, position;‬

‭do {‬
‭printf("\n--- Linked List Operations ---\n");‬
‭printf("1. Insert at beginning\n");‬
‭printf("2. Insert at end\n");‬
‭printf("3. Insert after a node\n");‬
‭printf("4. Delete node by value\n");‬
‭printf("5. Delete node at position\n");‬
‭printf("6. Search for a value\n");‬
‭ rintf("7. Traverse the list\n");‬
p
‭printf("8. Print the List\n");‬
‭printf("9. Exit\n");‬
‭printf("Enter your choice: ");‬
‭scanf("%d", &choice);‬

‭switch (choice) {‬
‭case 1:‬
‭printf("Enter data to insert at the beginning: ");‬
‭scanf("%d", &data);‬
‭insertAtBeginning(&head, data);‬
‭break;‬

‭case 2:‬
‭printf("Enter data to insert at the end: ");‬
‭scanf("%d", &data);‬
‭insertAtEnd(&head, data);‬
‭break;‬

‭case 3:‬
‭printf("Enter data to insert: ");‬
‭scanf("%d", &data);‬
‭printf("Enter value of the node after which to insert: ");‬
‭scanf("%d", &position);‬
‭struct Node* temp = head;‬
‭while (temp != NULL && temp->data != position) {‬
‭temp = temp->next;‬
‭}‬
‭if (temp != NULL) {‬
‭insertAfterNode(temp, data);‬
‭} else {‬
‭printf("Node with value %d not found.\n", position);‬
‭}‬
‭break;‬

‭case 4:‬
‭printf("Enter value of the node to delete: ");‬
‭scanf("%d", &data);‬
‭deleteNodeByValue(&head, data);‬
‭break;‬
‭case 5:‬
‭printf("Enter position of the node to delete (starting from 0):‬
‭");‬
s‭ canf("%d", &position);‬
‭deleteNodeAtPosition(&head, position);‬
‭break;‬

‭case 6:‬
‭printf("Enter value to search for: ");‬
‭scanf("%d", &data);‬
‭searchNode(head, data);‬
‭break;‬

‭case 7:‬
‭traverseList(head);‬
‭break;‬

‭case 8:‬
‭printf("Linked list: ");‬
‭printList(head);‬
‭break;‬

‭case 9:‬
‭printf("Exiting...\n");‬
‭break;‬

‭default:‬
‭printf("Invalid choice. Please try again.\n");‬
}‭ ‬
‭} while (choice != 9);‬

‭return 0;‬
‭}‬
‭9.‬‭Implementation of Stack using Array with following options.‬
‭a.‬‭Push‬
‭b.‬‭Pop‬
‭c.‬‭Exit‬
‭#include <stdio.h>‬
‭#include <stdlib.h>‬

‭#define MAX 5 // Maximum size of the stack‬

i‭nt stack[MAX]; // Array to store stack elements‬


‭int top = -1; // Top points to the top element of the stack‬

/‭/ Function to push an element onto the stack‬


‭void push(int value) {‬
‭if (top == MAX - 1) {‬
‭printf("Stack Overflow! Cannot push %d.\n", value);‬
‭} else {‬
‭top++;‬
‭stack[top] = value;‬
‭printf("Pushed %d onto the stack.\n", value);‬
‭}‬
‭}‬

/‭/ Function to pop an element from the stack‬


‭void pop() {‬
‭if (top == -1) {‬
‭printf("Stack Underflow! Stack is empty.\n");‬
‭} else {‬
‭printf("Popped %d from the stack.\n", stack[top]);‬
‭top--;‬
‭}‬
‭}‬

/‭/ Function to display the elements of the stack‬


‭void display() {‬
‭if (top == -1) {‬
‭printf("Stack is empty.\n");‬
‭} else {‬
‭printf("Stack elements are: ");‬
‭for (int i = 0; i <= top; i++) {‬
‭printf("%d ", stack[i]);‬
‭}‬
‭printf("\n");‬
‭}‬
‭}‬

‭int main() {‬
‭int choice, value;‬

‭do {‬
‭printf("\n--- Stack Operations ---\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 the value to push: ");‬
‭scanf("%d", &value);‬
‭push(value);‬
‭break;‬

‭case 2:‬
‭pop();‬
‭break;‬

‭case 3:‬
‭display();‬
‭break;‬

‭case 4:‬
‭printf("Exiting...\n");‬
‭break;‬

‭default:‬
‭printf("Invalid choice. Please try again.\n");‬
}‭ ‬
‭} while (choice != 4);‬

‭return 0;‬
‭}‬
‭ 1. Implementation of Queue using Array with following‬
1
‭options :‬
‭a. Insert an Item into Queue‬
‭b. Delete an Item from Queue‬
‭c. Exit‬
‭#include <stdio.h>‬
‭#include <stdlib.h>‬

‭#define MAX 5 // Maximum size of the queue‬

i‭nt queue[MAX]; // Array to store queue elements‬


‭int front = -1; // Index of the front of the queue‬
‭int rear = -1; // Index of the rear of the queue‬

/‭/ Function to insert an item into the queue‬


‭void insert(int value) {‬
‭if (rear == MAX - 1) {‬
‭printf("Queue Overflow! Cannot insert %d.\n", value);‬
‭} else {‬
‭if (front == -1)‬
‭front = 0; // Initialize front to 0 when the first element is added‬
‭rear++;‬
‭queue[rear] = value;‬
‭printf("Inserted %d into the queue.\n", value);‬
‭}‬
‭}‬

/‭/ Function to delete an item from the queue‬


‭void delete() {‬
‭if (front == -1 || front > rear) {‬
‭printf("Queue Underflow! Queue is empty.\n");‬
‭} else {‬
‭printf("Deleted %d from the queue.\n", queue[front]);‬
‭front++;‬
/‭/ Reset front and rear when the queue becomes empty‬
‭if (front > rear) {‬
‭front = -1;‬
‭rear = -1;‬
‭}‬
‭}‬
‭}‬

/‭/ Function to display the elements of the queue‬


‭void display() {‬
‭if (front == -1) {‬
‭printf("Queue is empty.\n");‬
‭} else {‬
‭printf("Queue elements are: ");‬
‭for (int i = front; i <= rear; i++) {‬
‭printf("%d ", queue[i]);‬
‭}‬
‭printf("\n");‬
‭}‬
‭}‬

‭int main() {‬
‭int choice, value;‬

‭do {‬
‭printf("\n--- 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 value to insert: ");‬
‭scanf("%d", &value);‬
‭insert(value);‬
‭break;‬
‭case 2:‬
‭delete();‬
‭break;‬

‭case 3:‬
‭display();‬
‭break;‬

‭case 4:‬
‭printf("Exiting...\n");‬
‭break;‬

‭default:‬
‭printf("Invalid choice. Please try again.\n");‬
}‭ ‬
‭} while (choice != 4);‬

‭return 0;‬
‭}‬

‭ 2. Implementation of Queue using Linked List with following‬


1
‭options :‬
‭a. Insert an Item into Queue‬
‭b. Delete an Item from Queue‬
‭c. Exit‬
‭#include <stdio.h>‬
‭#include <stdlib.h>‬

/‭/ Structure to represent a node in the linked list‬


‭struct Node {‬
‭int data;‬
‭struct Node* next;‬
‭};‬

/‭/ Front and rear pointers to keep track of the queue‬


‭struct Node* front = NULL;‬
‭struct Node* rear = NULL;‬

/‭/ Function to insert an item into the queue‬


‭void insert(int value) {‬
s‭ truct Node* newNode = (struct Node*)malloc(sizeof(struct Node));‬
‭newNode->data = value;‬
‭newNode->next = NULL;‬

‭if (rear == NULL) {‬


‭// If queue is empty, both front and rear point to the new node‬
‭front = rear = newNode;‬
‭} else {‬
‭rear->next = newNode; // Link the new node at the end of the‬
‭queue‬
‭rear = newNode; // Update rear to point to the new node‬
‭}‬
‭printf("Inserted %d into the queue.\n", value);‬
‭}‬

/‭/ Function to delete an item from the queue‬


‭void delete() {‬
‭if (front == NULL) {‬
‭printf("Queue Underflow! Queue is empty.\n");‬
‭return;‬
‭}‬

s‭ truct Node* temp = front;‬


‭printf("Deleted %d from the queue.\n", front->data);‬
‭front = front->next; // Move front to the next node‬

/‭/ If the queue becomes empty, update rear to NULL‬


‭if (front == NULL) {‬
‭rear = NULL;‬
‭}‬

‭free(temp);‬
}‭ ‬
‭// Function to display the elements of the queue‬
‭void display() {‬
‭if (front == NULL) {‬
‭printf("Queue is empty.\n");‬
‭} else {‬
‭struct Node* temp = front;‬
‭printf("Queue elements are: ");‬
‭while (temp != NULL) {‬
‭printf("%d ", temp->data);‬
‭temp = temp->next;‬
‭}‬
‭printf("\n");‬
‭}‬
}‭ ‬
‭int main() {‬
‭int choice, value;‬

‭do {‬
‭printf("\n--- 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 value to insert: ");‬
‭scanf("%d", &value);‬
‭insert(value);‬
‭break;‬

‭case 2:‬
‭delete();‬
‭break;‬

‭case 3:‬
‭display();‬
‭break;‬

‭case 4:‬
‭printf("Exiting...\n");‬
‭break;‬

‭default:‬
‭printf("Invalid choice. Please try again.\n");‬
}‭ ‬
‭} while (choice != 4);‬

‭return 0;‬
‭}‬
‭ 3. Factorial of a number using recursion‬
1
‭#include <stdio.h>‬

/‭/ Recursive function to calculate the factorial of a number‬


‭int factorial(int n) {‬
‭if (n == 0 || n == 1) // Base case: factorial of 0 or 1 is 1‬
‭return 1;‬
‭else‬
‭return n * factorial(n - 1); // Recursive case‬
‭}‬

‭int main() {‬
‭int num;‬
‭printf("Enter a number: ");‬
‭scanf("%d", &num);‬

/‭/ Check if the number is negative, as factorial is not defined for‬


‭negative numbers‬
‭if (num < 0) {‬
‭printf("Factorial is not defined for negative numbers.\n");‬
‭} else {‬
‭int result = factorial(num);‬
‭printf("The factorial of %d is %d.\n", num, result);‬
‭}‬

‭return 0;‬
‭}‬

You might also like