




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
DATA STRUCTURES USING C LAB NOTES JNTUK R23 SYLLABUS
Typology: Lecture notes
1 / 153
This page cannot be seen from the preview
Don't miss anything!





























































































ENGINEERING CURRICULUM - 2023 JNTUK B.Tech. R23 Regulations 87
DATA STRUCTURES LAB
Exercise 1: Array Manipulation i) Write a program to reverse an array. #include <stdio.h> int main() { int arr[100], n, i, temp; // Get the size of the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Get array elements from the user printf("Enter %d elements:\n", n); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Reverse the array in-place for(i = 0; i < n / 2; i++) { temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } // Print the reversed array printf("Reversed array:\n"); for(i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
Output:
printf("Element found at position %d (index %d)\n", i + 1, i); break; } } if(!found) { printf("Element not found in the array.\n"); } return 0; }
Output(s):
printf("Element found at position %d (index %d)\n", mid + 1, mid); return 0; } else if(arr[mid] < key) { low = mid + 1; } else { high = mid - 1; } } printf("Element not found in the array.\n"); return 0; }
Output(s):
printArray(arr, n); bubbleSort(arr, n); printf("Sorted array: "); printArray(arr, n); return 0; } Output:
Selection sort in C #include <stdio.h> void selectionSort(int arr[], int n) { int i, j, min_idx, temp; 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 the found minimum element with the first element temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original array: ");
Insertion Sort in C #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--; } arr[j + 1] = key; } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original array: "); printArray(arr, n);
insertionSort(arr, n); printf("Sorted array: "); printArray(arr, n); return 0; } Output:
// Insertion at the end void insertAtEnd(struct Node** head, int data) { struct Node* newNode = createNode(data); if (head == NULL) { head = newNode; printf("Inserted %d at the end\n", data); return; } struct Node temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; printf("Inserted %d at the end\n", data); } // Insertion after a given node (by value) void insertAfter(struct Node head, int afterData, int data) { struct Node temp = head; while (temp != NULL && temp->data != afterData) { temp = temp->next; } if (temp == NULL) { printf("Node with data %d not found\n", afterData); return; } struct Node* newNode = createNode(data); newNode->next = temp->next; temp->next = newNode; printf("Inserted %d after %d\n", data, afterData); }
// Deletion of the first node void deleteFirst(struct Node** head) { if (head == NULL) { printf("List is empty\n"); return; } struct Node temp = head; head = (head)->next; printf("Deleted %d from the beginning\n", temp->data); free(temp); } // Deletion of the last node void deleteLast(struct Node* head) { if (head == NULL) { printf("List is empty\n"); return; } if ((head)->next == NULL) { printf("Deleted %d from the end\n", (head)->data); free(head); head = NULL; return; } struct Node temp = *head; while (temp->next->next != NULL) { temp = temp->next; } printf("Deleted %d from the end\n", temp->next->data); free(temp->next);