DATA STRCTURES Record Book
DATA STRCTURES Record Book
Experiment 1
Analysis:-
The time complexity of this algorithm in far where n is the no.of elements in the
array the algorithm itenation throught half of the array & each itenation involves a constant
amount of operation (swap 2 elements).
Expected Output:
Original array : 1 2 3 4 5
Reversed array : 5 4 3 2 1
Algorithm:
• Define a function reverseArray that takes the array and its size as parameters.
• Initialize two variables start and end to the first and last index of the array, respectively.
• While start is less than end, swap the elements at start and end, and increment start
and decrement end.
• In the main function, define an array and its size.
• Print the original array.
• Call the reverseArray function to reverse the array.
• Print the reversed array
Program:
#include <stdio.h>
void reverseArray(int arr[], int size) {
int start = 0;
int end = size - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main() {
printf(“23811A4237\n”);
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
reverseArray(arr, size);
printf("\nReversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);}}
Output:
Result:
Experiment 2
Analysis:-
Time complexity:- O(n) in the worst case where n is the no.of the elements in the array.
Expected output:-
Algorithm :-
• Define a function linearSearch that takes the array, its size, and the key to be
searched as parameters.
• Iterate through the array using a loop.
• If the current element matches the key, return the index.
• If the key is not found after iterating through the array, return -1.
Program:-
#include <stdio.h>
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main() { printf("23811A4237\n");
int arr[] = {2, 4, 6, 8, 10, 12};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 8;
int index = linearSearch(arr, size, key);
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found in the array\n", key);
}
return 0;}
Output:
Result:
Binary Search:
Analysis:-
Time complexity:- O(n) in the worst case where n is the no.of the elements in the
array.
Expected output:-
Algorithm:
• Define a function binarySearch that takes the array, its lower and upper bounds
(low and high), and the key to be searched as parameters.
• If low is less than or equal to high, calculate the middle index.
• If the element at the middle index matches the key, return the index.
• If the element at the middle index is greater than the key, recursively call
binarySearch with the left half of the array.
• If the element at the middle index is less than the key, recursively call
binarySearch with the right half of the array.
• If the key is not found after the recursion, return -1.
Program:-
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int key) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
}
if (arr[mid] > key) {
return binarySearch(arr, low, mid - 1, key);
} else {
return binarySearch(arr, mid + 1, high, key); // Search in the right half
}}
return -1;
}
Output:-
Result:-
Experiment 3
Bubble sort:-
Analysis:-
Bubble sort is a simple sorting algorithm that repeadly steps throught the
last compass adjacent elements & swap them if they are in the order.It continous
iterating the list until no swaps are needed indicate that the list is stored.
Expected Output:-
Original array: 5 3 8 2 1 4
Sorted array: 1 2 3 4 5 8
Algorithm:-
Program:-
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
} }}}
int main()
{ printf(“23811A4237\n”);
int arr[] = {5, 3, 8, 2, 1, 4};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
bubbleSort(arr, size);
printf("\nSorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:-
Result:-
Insertion sort:-
Analysis:-
Insertion sort is a simple sorting algorithm that builds the final sorted array
one element at a time. It iterates throught the array & far each element, it finds the
proper position to insert it into the already sorted position of the array try shifting all
targen element one position to the right.
Expected Output:-
Original array: 5 3 8 2 1 4
Sorted array: 1 2 3 4 5 8
Algorithm:-
• Iterate through the array using a loop starting from the second element.
• Store the current element in a variable key.
• Compare key with the elements to its left and shift larger elements one position
to the right.
• Insert key into its correct position in the sorted portion of the array.
• Repeat the process until the entire array is sorted.
Program:-
#include <stdio.h>
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1; }
arr[j + 1] = key;}}
int main() { printf(“23811A4237\n”);
int arr[] = {5, 3, 8, 2, 1, 4};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
insertionSort(arr, size);
printf("\nSorted array: ");
for (int i = 0; i < size; i++) {
printf("%d", arr[i]);}
return 0;}
Output:-
Result:-
Selection sort:-
Analysis:-
Selection sort is a simple sorting algorithm that repeated select the minimum
element from the unsorted partion of the array & swaps if with the element of the
beginning of the unsorted position.The process continous until the entrie is sorted.
Expected Output:-
Original array: 5 3 8 2 1 4
Sorted array: 1 2 3 4 5 8
Algorithm:-
Program:-
#include <stdio.h>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
Output:-
Result:-
WEEK-2
Experiment 1
Insertion operation:-
Statement:- To write a Implement a singly linked list and perform insertion and
deletion operations.
Insertion operation:-
Analysis:-
Insertion:-
At the beginning: O(1)
At the end: O(n)
In the middle:O(n)
Deletion:
At the beginning : O(1)
At the end : O(n)
In the middle : O(n)
A single linked list is a data structure where each element (node) in the list contain
data& a reference (or) link to the next node in the sequence. It starts with head node and
the last node points to null.
Expected Output:
Linked list after insertion: 7 -> 5 -> 3 -> NULL
Algorithm:-
• Start
• Define a structure Node with two members: data and next.
• Define functions for insertion (insertAtBeginning) and printing
(printLinkedList).
• In the insertAtBeginning function:
o Allocate memory for a new node.
o Assign the given data to the new node.
o Make the new node point to the current head.
o Move the head to point to the new node.
• In the printLinkedList function:
o Traverse the linked list from the head node.
o Print the data of each node and move to the next node until reaching
NULL.
• In the main function:
o Initialize an empty linked list.
o Insert some elements at the beginning.
o Print the linked list.
Program:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data
= new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void printLinkedList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
Output:-
Result:-
Deletion operation:-
Algorithm:-
• Start from the head node.
• If the key is found at the beginning, update the head pointer to the next node and
free the memory of the current node.
• Traverse the list until either the key is found or reach the end of the list.
• If the key is not found, print a message stating that the key was not found.
• If the key is found, unlink the node containing the key from the list and free its
memory.
Expeceted output:-
Original list: 1 2 3 4 5
Program:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
newNode->next = *head;
*head = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
if (temp == NULL) {
prev->next = temp->next;
free(temp);
head = head->next;
printf("\n");
insertAtBeginning(&head, 5);
insertAtBeginning(&head, 4);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
printList(head);
deleteNodeByKey(&head, 3);
printList(head);
deleteNodeByKey(&head, 6);
deleteNodeByKey(&head, 1);
printList(head);
deleteNodeByKey(&head, 5);
printList(head);
return 0;
Output:-
Result:-
Experiment 2
Statement:- Develop a program to reverse a linked is iteratively and recursively.
Reverse a linked is iteratively:-
Analysis:-
• Insertion atthe beginning.
• Printing the linked list.
• Reversing the linked list iteratively.
• The reverse iterative using three pointer prev current & next.
Expected Output:
Original linked list: 1 -> 2 -> 3 -> 4 -> NULL
Algorithm:-
• Initialize three pointers: prev, current, and next.
• Start with prev pointing to NULL and current pointing to the head of the linked
list.
• Iterate through the linked list:
- Set next to current's next node.
- Set current's next node to prev.
- Move prev to current and current to next.
• Update the head of the linked list to point to prev.
Program:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
next = current->next;
current->next = prev;
prev = current;
current = next;
head = prev;
return head;
malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
printf("NULL\n");
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printList(head);
head = reverseList(head);
printList(head);
return 0;
Output:-
Result:-
Reverse a linked list recursively:-
Analysis:-
• Insertion atthe beginning.
• Printing the linked list.
• Reversing the linked list recursively reverse recursive function reverse the linked
list recursively. It takes two pariontion current & prev.
Algorithm:-
• Define a function reverse List Recursive that takes a pointer to the head of the
linked list.
• If the list is empty or has only one node (base case), return.
• Recursively call reverse List Recursive with the next node of the current node.
• Once the recursive call returns, set the next pointer of the next node to the
current node.
• Set the next pointer of the current node to NULL.
• Update the head pointer to point to the last node (which was the first node in the
original list).
Program:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
first = *head;
rest = first->next;
reverseListRecursive(&rest);
first->next->next = first;
first->next = NULL;
*head = rest;
newNode->data = data;
newNode->next = *head;
*head = newNode;
node = node->next;
printf("NULL\n");
insertNode(&head, 3);
insertNode(&head, 2);
insertNode(&head, 1);
printList(head);
reverseListRecursive(&head);
printList(head);
return 0;
Output:-
Result:-
Experiment 3
Statement:- To solve problems involving linked list traversed & manipulation.
Linked list traversed:-
Analysis:-
Linked lists are linear data structures that hold data in individual objects
called nodes. These nodes hold both the data and a reference to the next node in the list.
Linked lists are often used because of their efficient insertion and deletion.
Expected output:-
Output:-
Result:-
Linked list manipulation:-
Expeceted output:-
Original linked list: 1 2 3 4 5
Algorithm:-
• Initialize three pointers: prev, current, and next.
• Set prev to NULL, current to the head of the linked list, and next to NULL.
• Traverse the linked list:
- Set next to the next node of current.
- Set the next pointer of current to prev.
- Move prev to current and current to next.
• Set the head of the linked list to prev, which is now pointing to the last node.
Program:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
next = current->next;
current->next = prev;
prev = current;
current = next;
return prev;
temp = temp->next;
printf("\n");
newNode->data = data;
newNode->next = NULL;
return newNode;
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printLinkedList(head);
head = reverseLinkedList(head);
printLinkedList(head);
Output:-
Result:-