Ram CTSD New
Ram CTSD New
Technology
Subject: CTSD-2 (303105151) ERP No: 2303031240642
                                       PRACTICAL-6
        a) Write a c program to implement Insertion sort & Quick sort
       #include<stdio.h>
       void insertionSort(int a[], int n) {
       for (int i = 1; i < n; i++) {
       int temp = a[i];
       int j = i - 1;
       while (j >= 0 && a[j] > temp) {
       a[j + 1] = a[j];
       j--;
       }
       a[j + 1] = temp;
       }
       }
       void swap(int *a, int *b) {
       int temp = *a;
       *a = *b;
       *b = temp;
       }
       int partition(int arr[], int low, int high) {
       int pivot = arr[low];
       int i = low;
       int j = high;
           while (i < j) {
               while (arr[i] <= pivot) {
                   i++;
               }
               while (arr[j] > pivot) {
                   j--;
               }
               if (i < j) {
                   swap(&arr[i], &arr[j]);
               }
           }
           swap(&arr[low], &arr[j]);
           return j;
       }
       void quickSort(int arr[], int low, int high) {
           if (low < high) {
               int pivotPos = partition(arr, low, high);
               quickSort(arr, low, pivotPos - 1);
               quickSort(arr, pivotPos + 1, high);
           }
       }
       void printArray(int arr[], int n) {
           for (int i = 0; i < n; i++)
OUTPUT:
       int main(){
              int n,i,j;
              printf("Enter the size of the array: ");
              scanf("%d", &n);
              int a[n];
              printf("Enter array elements: ");
       for (i = 0; i < n; i++) {
              scanf("%d", &a[i]);
              }
              bubbleSort(a,n);
       for (i = 0; i <n; i++) {
              printf("%d\t", a[i]);
              }
              printf("\n");
                      for (i = 0; i < n-2; i += 2) {
              int n = i+2;
              int product = a[i]*a[n];
              printf("%d\t",product);
              }
                      printf("\n");
              for(i=1;i<n-2;i+=2){
                      int n = i+2;
                      int sum = a[i]+a[n];
                      printf("%d\t",sum);
             }
       }
OUTPUT:
                                           PRACTICAL-7
       Write a c Program to implement Merge Sort
       #include <stdio.h>
       void merge(int arr[], int low, int mid, int high) {
       int i = low;
       int j = mid + 1;
       int k = 0; // Initialize the index for the auxiliary array
       int b[high - low + 1]; // Auxiliary array for merging
       while (i <= mid && j <= high) {
       if (arr[i] < arr[j]) {
       b[k] = arr[i];
       i++;
       } else {
       b[k] = arr[j];
       j++;
       }
       k++;
       }
       while (i <= mid) {
       b[k] = arr[i];
       i++;
       k++;
       }
                                       PRACTICAL-8
       a) Write a c program to sort in ascending
       order and reverse the individual row
       elements of an
       mxn matrix
       input : 3 4
       1423
       7 8 10 9
       6352
       output:
        4321
        10 9 8 7
        6532
       #include <stdio.h>
       void sortRow(int arr[][100], int row, int col)
        { for (int i = 0; i < row; i++) {
            for (int j = 0; j < col - 1; j++) {
               for (int k = 0; k < col - j - 1; k++) {
                   if (arr[i][k] > arr[i][k + 1]) {
                     int temp = arr[i][k];
                     arr[i][k] = arr[i][k + 1];
                     arr[i][k + 1] = temp;
                       }
                   }
               }
           }
       }
       void reverseRow(int arr[][100], int row, int col)
       {
       for (int i = 0; i < row; i++) {
                   for (int j = 0; j < col / 2; j++) {
                           int temp = arr[i][j];
                           arr[i][j] = arr[i][col - j - 1];
                           arr[i][col - j - 1] = temp;
                           }
                   }
       }
       void printArray(int arr[][100],int m,int n){
                   for (int i = 0; i < m; i++) {
                           for (int j = 0; j < n; j++) {
                           printf("%d ", arr[i][j]);
                           }
                   printf("\n");
                   }
       }
       int main() {
             int m, n;
             printf("Enter the number of rows and columns (m x n): ");
             scanf("%d %d", &m, &n);
             int matrix[100][100];
             printf("Enter the matrix elements:\n");
             for (int i = 0; i < m; i++) {
                      for (int j = 0; j < n; j++) {
                             scanf("%d", &matrix[i][j]);
                      }
       }
             sortRow(matrix, m, n);
             printf("sorted array:-\n");
                      printArray(matrix,m,n);
                      printf("reversed array:-\n");
             reverseRow(matrix, m, n);
             printArray(matrix,m,n);
             return 0;
       }
OUTPUT:
                                PRACTICAL-9
       a) Write a c program to perform linear Search
       #include <stdio.h>
       int main() {
               int i, e, n;
               printf("Enter size of array:");
               scanf("%d", &n);
               int a[n];
               printf("Enter array elements:");
               for (i = 0; i < n; i++) {
                       scanf("%d", &a[i]);
                       }
       printf("Enter search element:");
               scanf("%d", &e);
               for (i = 0; i < n; i++) {
                       if (a[i] == e) {
               printf("The element is found at index: %d", i);
       break;
       }
       }
       if (i == n) {
       printf("Element not found in the array.");
       }
       return 0;
       }
OUTPUT:
                     high = mid - 1;
                   else
                     low = mid + 1;
               }
           }
           if (flag == 1)
               printf("\nData found at location: %d\n", mid + 1);
           else
               printf("\nData not found\n");
           return 0;
       }
       OUTPUT:
                             PRACTICAL-10
       Write a c program to Create a single Linked list and
       perform Following Operations
       A. Insertion At Beginning
       B. Insertion At End
       C. Insertion After a particular node
       D. Insertion Before a particular node
       E. Insertion at specific position
       F. Search a particular node
       G. Return a particular node
       H. Deletion at the beginning
       I. Deletion at the end
       J. Deletion after a particular node
       K. Deletion before a particular node
       L. Delete a particular node
       M. Deletion at a specific position
       #include <stdio.h>
       #include <stdlib.h>
       struct Node {
       int data;
       struct Node* next;
       };
       newNode->next = *head;
       *head = newNode;
       return;
       }
       struct Node* temp = *head;
       while (temp->next != nextNode) {
       temp = temp->next;
       }
       newNode->next = nextNode;
       temp->next = newNode;
       }
       void insertAtPosition(struct Node** head, int position,
       int data) {
       if (position < 0) {
       printf("Invalid position.\n");
       return;
       }
       if (position == 0) {
       insertAtBeginning(head, data);
       return;
       }
       struct Node* newNode = createNode(data);
       struct Node* temp = *head;
       }
       struct Node* temp = *head;
       *head = (*head)->next;
       free(temp);
       }
       void deleteAtEnd(struct Node** head) {
       if (*head == NULL) {
       printf("List is empty, deletion not possible.\n");
       return;
       }
       if ((*head)->next == NULL) {
       free(*head);
       *head = NULL;
       return;
       }
       struct Node* secondLast = *head;
       while (secondLast->next->next != NULL) {
       secondLast = secondLast->next;
       }
       free(secondLast->next);
       secondLast->next = NULL;
       }
       void deleteAfterNode(struct Node* prevNode) {
       insertAtBeginning(&head, 5);
       printf("List after inserting at beginning: ");
       printList(head);
       insertAfterNode(head->next, 25);
       printf("List after inserting after a particular node: ");
       printList(head);
       insertBeforeNode(&head, head->next->next->next, 35);
       printf("List after inserting before a particular node: ");
       printList(head);
       insertAtPosition(&head, 2, 15);
       printf("List after inserting at specific position: ");
       printList(head);
       struct Node* searchedNode = searchNode(head, 30);
       if (searchedNode != NULL)
       printf("Node found: %d\n", searchedNode->data);
       else
       printf("Node not found.\n");
       deleteAtBeginning(&head);
       printf("List after deletion at beginning: ");
       printList(head);
       deleteAtEnd(&head);
       printf("List after deletion at end: ");
       printList(head);
       deleteAfterNode(head->next);
       printf("List after deletion after a particular node: ");
       printList(head);
       deleteBeforeNode(&head, head->next->next);
       printf("List after deletion before a particular node: ");
       printList(head);
       deleteNode(&head, head->next);
       printf("List after deleting a particular node: ");
       printList(head);
       deleteAtPosition(&head, 2);
       printf("List after deletion at specific position: ");
       printList(head);
       // Free the memory allocated to the linked list
       freeList(&head);
       return o;
       }
       OUTPUT:
                              PRACTICAL-11
       a) Write a program to Reverse a singly 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 insertAtBeginning(struct Node** head, int data) {
       struct Node* newNode = createNode(data);
       newNode->next = *head;
       *head = newNode;
       }
       insertAtBeginning(&head, 40);
       printf("Original List: ");
       printList(head);
       head = reverseList(head);
       printf("Reversed List: ");
       printList(head);
       struct Node* temp;
       while (head != NULL) {
       temp = head;
       head = head->next;
       free(temp);
       }
       return 0;
       }
OUTPUT:
       *head = newNode;
       return;
       }
       struct Node* temp = *head;
       while (temp->next != NULL) {
       temp = temp->next;
       }
       temp->next = newNode;
       }
       void printList(struct Node* head) {
       while (head != NULL) {
       printf("%c ", head->data);
       head = head->next;
       }
       printf("\n");
       }
       struct Node* reverseList(struct Node* head) {
       struct Node* prevNode = NULL;
       struct Node* currNode = head;
       while (currNode != NULL) {
       struct Node* nextNode = currNode->next;
       currNode->next = prevNode;
       prevNode = currNode;
       currNode = nextNode;
       }
       return prevNode;
       }
       bool isPalindrome(struct Node* head) {
       if (head == NULL || head->next == NULL) {
       return true;
       }
       struct Node* slow = head;
       struct Node* fast = head;
       while (fast->next != NULL && fast->next->next != NULL)
       {
       slow = slow->next;
       fast = fast->next->next;
       }
       struct Node* secondHalf = reverseList(slow->next);
       struct Node* firstHalf = head;
       while (secondHalf != NULL) {
       if (firstHalf->data != secondHalf->data) {
       secondHalf = reverseList(secondHalf);
       return false;
       }
       f
        irstHalf = firstHalf->next;
       secondHalf = secondHalf->next;
       }
       secondHalf = reverseList(secondHalf);
       return true;
       }
       void freeList(struct Node** head) {
       struct Node* temp;
       while (*head != NULL) {
       temp = *head;
       *head = (*head)->next;
       free(temp);
       }
       }
       int main() {
       struct Node* head = NULL;
       insertAtEnd(&head, 'r');
       insertAtEnd(&head, 'a');
       insertAtEnd(&head, 'd');
       insertAtEnd(&head, 'a');
       insertAtEnd(&head, 'r');
       printf("Original List: ");
       printList(head);
       if (isPalindrome(head))
       printf("The linked list is a palindrome.\n");
       else
       printf("The linked list is not a palindrome.\n");
       freeList(&head);
       return 0;
       }
       OUTPUT:
                         PRACTICAL-12
       Write a c program to Create a Circular Linked list and
       perform Following Operations
       A. Insertion At Beginning
       B. Insertion At End
       C. Insertion After a particular node
       D. Insertion Before a particular node
       E. Insertion at specific position
       F. Search a particular node
       G. Return a particular node
       H. Deletion at the beginning
       I. Deletion at the end
       J. Deletion after a particular node
       K. Deletion before a particular node
       L. Delete a particular node
       M. Deletion at a specific position
       #include <stdio.h>
       #include <stdlib.h>
       struct Node {
       int data;
       struct Node* next;
       };
       struct Node* head = NULL;
       // Function to create a new node
       struct Node* createNode(int value) {
       struct Node*newNode = (struct Node*)malloc(sizeof(struct Node));
       if (newNode == NULL) {
       printf("Memory allocation failed.\n");
       exit(1);
       }
       newNode->data = value;
       newNode->next = NULL;
       return newNode;
       }
       // Function to insert a node at the beginning of the list
       void insertAtBeginning(int value) {
       struct Node* newNode = createNode(value);
       if (head == NULL) {
       head = newNode;
       newNode->next = head;
       } else {
       struct Node* temp = head;
       while (temp->next != head) {
       temp = temp->next;
       }
       temp->next = newNode;
       newNode->next = head;
       head = newNode;
       }
       printf("List after insertion at beginning: %d\n", value);
       }
       // Function to insert a node at the end of the list
       void insertAtEnd(int value) {
       struct Node* newNode = createNode(value);
       if (head == NULL) {
       head = newNode;
       newNode->next = head;
       } else {
       struct Node* temp = head;
       while (temp->next != head) {
       temp = temp->next;
       }
       temp->next = newNode;
       newNode->next = head;
       }
       printf("List after insertion at end: ");
       struct Node* temp = head;
       do {
       printf("%d ", temp->data);
       temp = temp->next;
       } while (temp != head);
       printf("\n");
       }
       // Function to insert a node after a particular node
       void insertAfterNode(int key, int value) {
       struct Node* newNode = createNode(value);
       if (head == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* temp = head;
       while (temp->data != key) {
       temp = temp->next;
       if (temp == head) {
       do {
       prev = temp;
              temp = temp->next;
              if (temp->data == key) {
                  prev->next = newNode;
                  newNode->next = temp;
                  printf("List after insertion before a particular node: ");
                  struct Node* current = head;
                  do {
                    printf("%d ", current->data);
                    current = current->next;
                  } while (current != head);
                  printf("\n");
                  return;
              }
           } while (temp != head);
           printf("Node with key %d not found.\n", key);
       }
       return;
       }
       if (position == 1) {
       insertAtBeginning(value);
       return;
       }
       struct Node* newNode = createNode(value);
       struct Node* temp = head;
       for (int i = 1; i < position - 1; i++) {
       if (temp->next == head) {
       printf("Position out of range.\n");
       return;
       }
       temp = temp->next;
       }
       newNode->next = temp->next;
       temp->next = newNode;
       printf("List after insertion at specific position: ");
       struct Node* current = head;
       do {
       printf("%d ", current->data);
       current = current->next;
       } while (current != head);
       printf("\n");
       }
       // Function to search for a particular node
       void searchNode(int key) {
       if (head == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* temp = head;
       do {
       if (temp->data == key) {
       printf("Node found: %d\n", key);
       return;
       }
       temp = temp->next;
       } while (temp != head);
       printf("Node with key %d not found.\n", key);
       }
       // Function to delete the first node
       void deleteAtBeginning() {
       if (head == NULL) {
       printf("List is empty.\n");
       return;
       }
       if (head->next == head) {
       free(head);
       head = NULL;
       } else {
       struct Node* temp = head;
       while (temp->next != head) {
       temp = temp->next;
       }
       temp->next = head->next;
       struct Node* toDelete = head;
       head = head->next;
       free(toDelete);
       }
       printf("List after deletion at beginning: ");
       struct Node* current = head;
       do {
       printf("%d ", current->data);
       current = current->next;
       } while (current != head);
       printf("\n");
       }
       // Function to delete the last node
       void deleteAtEnd() {
       if (head == NULL) {
       printf("List is empty.\n");
       return;
       }
       if (head->next == head) {
       free(head);
       head = NULL;
       } else {
       struct Node* temp = head;
       struct Node* prev = NULL;
       while (temp->next != head) {
       prev = temp;
       temp = temp->next;
       }
       prev->next = head;
       free(temp);
       }
       printf("List after deletion at end: ");
       struct Node* current = head;
       do {
       printf("%d ", current->data);
       current = current->next;
       return;
       }
       struct Node* toDelete = prev->next;
       prev->next = toDelete->next;
       free(toDelete);
       printf("List after deletion before a particular node: ");
       struct Node* current = head;
       do {
       printf("%d ", current->data);
       current = current->next;
       } while (current != head);
       printf("\n");
       }
       // Function to delete a particular node
       void deleteNode(int key) {
       if (head == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* temp = head;
       struct Node* prev = NULL;
       while (temp->data != key) {
       prev = temp;
       temp = temp->next;
       if (temp == head) {
       printf("Node with key %d not found.\n", key);
       return;
       }
       }
       if (temp == head) {
       deleteAtBeginning();
       return;
       }
       prev->next = temp->next;
       free(temp);
       printf("List after deleting a particular node: ");
       struct Node* current = head;
       do {
       printf("%d ", current->data);
       current = current->next;
       } while (current != head);
       printf("\n");
       }
       // Function to delete a node at a specific position
       void deleteAtPosition(int position) {
       if (head == NULL) {
       printf("List is empty.\n");
       return;
       }
       if (position < 1) {
       printf("Invalid position.\n");
       return;
       }
       if (position == 1) {
       deleteAtBeginning();
       return;
       }
       struct Node* temp = head;
       struct Node* prev = NULL;
       for (int i = 1; i < position; i++) {
       prev = temp;
       temp = temp->next;
       if (temp == head) {
       printf("Position out of range.\n");
       return;
       }
       }
       prev->next = temp->next;
       free(temp);
       insertAtBeginning(10);
       insertAtEnd(20);
       insertAfterNode(10, 15);
       insertBeforeNode(20, 25);
       searchNode(15);
       insertAtPosition(3, 30);
       deleteAtBeginning();
       deleteAtEnd();
       deleteAfterNode(10);
       deleteBeforeNode(20);
       deleteNode(30);
       deleteAtPosition(1);
       return 0;
       }OUTPUT:
                                  PRACTICAL-13
       Write a c program to Create a Circular single Linked list and perform Following
       Operations
       A. Insertion After a particular node
       B. Insertion Before a particular node
       C. Search a particular node
       D. Return a particular node
       E. Deletion before a particular node
       F. Delete a particular node
       #include <stdio.h>
       #include <stdlib.h>
       struct Node {
       int data;
       struct Node* next;
       };
       struct Node* createNode(int value) {
       struct Node* newNode = (struct
       Node*)malloc(sizeof(struct Node));
       newNode->data = value;
       newNode->next = NULL;
       return newNode;
       }
       }
       }
       newNode->next = nextNode;
       current->next = newNode;
       if (current == *headRef)
       *headRef = newNode;
       }
       struct Node* searchNode(struct Node* head, int key) {
       if (head == NULL)
       return NULL;
       struct Node* current = head;
       do {
       if (current->data == key)
       return current;
       current = current->next;
       } while (current != head);
       return NULL;
       }
       void deleteBefore(struct Node** headRef, struct Node*
       nextNode) {
       if (*headRef == NULL || (*headRef)->next ==
       nextNode) {
       printf("No node to delete before the given node.\n");
       return;
       }
       struct Node* current = *headRef;
       while (current->next->next != nextNode) {
       current = current->next;
       if (current->next == *headRef) {
       printf("Node not found.\n");
       return;
       }
       }
       struct Node* temp = current->next;
       current->next = nextNode;
       free(temp);
       }
       void deleteNode(struct Node** headRef, struct Node*
       delNode) {
       if (*headRef == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* current = *headRef;
       if (current == delNode) {
       struct Node* temp = *headRef;
       do {
       printf("%d ", current->data);
       current = current->next;
       } while (current != head);
       printf("\n");
       }
       int main() {
       struct Node* head = NULL;
       head = createNode(1);
       head->next = head; // Circular reference
       insertAfter(head, 2);
       insertAfter(head->next, 3);
       insertAfter(head->next->next, 4);
       printf("Circular linked list: ");
       displayList(head);
       insertBefore(&head, head, 0);
       printf("After inserting before head: ");
       displayList(head);
       int key = 3;
       struct Node* foundNode = searchNode(head, key);
       if (foundNode != NULL)
       printf("Node with value %d found.\n", key);
       else
                             PRACTICAL-14
       Write a c program to Create a Circular Double Linked
       list and perform Following Operations
       A. Insertion After a particular node
       B. Insertion Before a particular node
       C. Search a particular node
       D. Return a particular node
       E. Deletion before a particular node
       F. Delete a particular node
       #include <stdio.h>
       #include <stdlib.h>
       struct Node {
       int data;
       struct Node* next;
       struct Node* prev;
       };
       struct Node* createNode(int data);
       void insertAfter(struct Node** head_ref, int value, int key);
       void insertBefore(struct Node** head_ref, int value, int key);
       struct Node* searchNode(struct Node* head, int key);
       struct Node* returnNode(struct Node* head, int key);
       void deleteBefore(struct Node** head_ref, int key);
       newNode->prev = temp;
       newNode->next = temp->next;
       temp->next->prev = newNode;
       temp->next = newNode;
       }
       void insertBefore(struct Node** head_ref, int value, int key) {
       struct Node* newNode = createNode(value);
       if (*head_ref == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* temp = *head_ref;
       while (temp->data != key) {
       temp = temp->next;
       if (temp == *head_ref) {
       printf("Key not found in the list.\n");
       return;
       }
       }
       newNode->prev = temp->prev;
       newNode->next = temp;
       temp->prev->next = newNode;
       temp->prev = newNode;
       if (temp == *head_ref)
       *head_ref = newNode;
       }
       struct Node* searchNode(struct Node* head, int key) {
       if (head == NULL) {
       printf("List is empty.\n");
       return NULL;
       }
       struct Node* temp = head;
       do {
       if (temp->data == key)
       return temp;
       temp = temp->next;
       } while (temp != head);
       printf("Key not found in the list.\n");
       return NULL;
       }
       struct Node* returnNode(struct Node* head, int key) {
       return searchNode(head, key);
       }
       void deleteBefore(struct Node** head_ref, int key) {
       if (*head_ref == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* temp = *head_ref;
       while (temp->data != key) {
       temp = temp->next;
       if (temp == *head_ref) {
       printf("Key not found in the list.\n");
       return;
       }
       }
       struct Node* delNode = temp->prev;
       delNode->prev->next = temp;
       temp->prev = delNode->prev;
       if (delNode == *head_ref)
       *head_ref = temp;
       free(delNode);
       }
       void deleteNode(struct Node** head_ref, int key) {
       if (*head_ref == NULL) {
       printf("List is empty.\n");
       return;
       }
       struct Node* temp = *head_ref;