[go: up one dir, main page]

0% found this document useful (0 votes)
8 views33 pages

DATA STRCTURES Record Book

The document outlines various programming experiments in C, including reversing an array, implementing searching techniques (linear and binary search), and sorting algorithms (bubble, insertion, and selection sort). It also covers the implementation of a singly linked list with insertion and deletion operations, as well as reversing a linked list both iteratively and recursively. Each section includes analysis, expected output, algorithms, and sample code.

Uploaded by

lokeshsivarathri
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)
8 views33 pages

DATA STRCTURES Record Book

The document outlines various programming experiments in C, including reversing an array, implementing searching techniques (linear and binary search), and sorting algorithms (bubble, insertion, and selection sort). It also covers the implementation of a singly linked list with insertion and deletion operations, as well as reversing a linked list both iteratively and recursively. Each section includes analysis, expected output, algorithms, and sample code.

Uploaded by

lokeshsivarathri
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
You are on page 1/ 33

WEEK-1

Experiment 1

Statement: To write a program to reverse an array.

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

Statement: C Programs to implement the Searching Techniques–


Linear & Binary Search.
Linear search:-

Analysis:-

Time complexity:- O(n) in the worst case where n is the no.of the elements in the array.

Space complexity:- O(1) as it required only a constant amount of extra space.

Expected output:-

Element 8 found at index 3

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.

Space complexity:- O(1) as it required only a constant amount of extra time.

Expected output:-

Element 8 found at index 3

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;
}

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 = binarySearch(arr, 0, size - 1, 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:-
Experiment 3

Statement: write a C Programs to implement Sorting Techniques – Bubble,


Selection and Insertion Sort.

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:-

• Iterate through the array using two nested loops.


• Compare each element with the adjacent element.
• If the current element is greater than the adjacent element, swap them.
• Repeat the process until no more swaps are needed.

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:-

Statement:- To write C Programs to implement Sorting Techniques- 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:-

Statement:- To write C Programs to implement Sorting Techniques- 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:-

• Iterate through the array using a loop.


• Find the minimum element in the unsorted portion of the array.
• Swap the minimum element with the first unsorted element.
• Repeat the process until the entire array is sorted.

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;
}
}

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]);
}
selectionSort(arr, size);
printf("\nSorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}

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");
}

int main() { printf(“23811A4237\n”);


struct Node* head = NULL;
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 5);
insertAtBeginning(&head, 7);
printf("Linked list after insertion: ");
printLinkedList(head);
return 0;
}

Output:-

Result:-
Deletion operation:-

Statement:- To write a implementation of a singly linked list in C, including


deletion operations.

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

List after deleting 3: 1 2 4 5

Key not found in the list.

List after deleting 1: 2 4 5

List after deleting 5: 2 4

Program:-
#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));

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;

void deleteNodeByKey(struct Node** head, int key) {

struct Node* temp = *head;

struct Node* prev = NULL;

if (temp != NULL && temp->data == key) {

*head = temp->next;

free(temp);

return;

while (temp != NULL && temp->data != key) {

prev = temp;

temp = temp->next;

if (temp == NULL) {

printf("Key not found in the list.\n");


return;

prev->next = temp->next;

free(temp);

void printList(struct Node* head) {

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

int main() { printf("23811A4237\n");

struct Node* head = NULL;

insertAtBeginning(&head, 5);

insertAtBeginning(&head, 4);

insertAtBeginning(&head, 3);

insertAtBeginning(&head, 2);

insertAtBeginning(&head, 1);

printf("Original list: ");

printList(head);

deleteNodeByKey(&head, 3);

printf("List after deleting 3: ");

printList(head);
deleteNodeByKey(&head, 6);

deleteNodeByKey(&head, 1);

printf("List after deleting 1: ");

printList(head);

deleteNodeByKey(&head, 5);

printf("List after deleting 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

Reversed linked list: 4 -> 3 -> 2 -> 1 -> 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;

struct Node* next;


};

struct Node* reverseList(struct Node* head) {

struct Node* prev = NULL;

struct Node* current = head;

struct Node* next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

head = prev;

return head;

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)

malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

void printList(struct Node* head) {

while (head != NULL) {

printf("%d -> ", head->data);


head = head->next;

printf("NULL\n");

int main() { printf("23811A4237\n");

struct Node* head = createNode(1);

head->next = createNode(2);

head->next->next = createNode(3);

head->next->next->next = createNode(4);

printf("Original linked list: ");

printList(head);

head = reverseList(head);

printf("Reversed linked list: ");

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;

struct Node* next;

};

void reverseListRecursive(struct Node** head) {

struct Node* first;

struct Node* rest;

if (*head == NULL || (*head)->next == NULL) {


return;

first = *head;

rest = first->next;

reverseListRecursive(&rest);

first->next->next = first;

first->next = NULL;

*head = rest;

void insertNode(struct Node** head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = *head;

*head = newNode;

void printList(struct Node* node) {

while (node != NULL) {

printf("%d -> ", node->data);

node = node->next;

printf("NULL\n");

int main() { printf("23811A4237\n");

struct Node* head = NULL;


insertNode(&head, 4);

insertNode(&head, 3);

insertNode(&head, 2);

insertNode(&head, 1);

printf("Original list: ");

printList(head);

reverseListRecursive(&head);

printf("Reversed list: ");

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:-

Original list: 1 -> 2 -> 3 -> 4 -> NULL


Reversed list: 4 -> 3 -> 2 -> 1 -> NULL
Algorithm:-

• Define a function reverseListRecursive 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 reverseListRecursive 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;
struct Node* next;};
void reverseListRecursive(struct Node** head) {
struct Node* first;
struct Node* rest;
if (*head == NULL || (*head)->next == NULL) {
return; }
first = *head;
rest = first->next;
reverseListRecursive(&rest);
first->next->next = first;
first->next = NULL;
*head = rest; }
void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode; }
void printList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next; }
printf("NULL\n"); }
int main() { printf("23811A4237\n");
struct Node* head = NULL;
insertNode(&head, 4);
insertNode(&head, 3);
insertNode(&head, 2);
insertNode(&head, 1);
printf("Original list: ");
printList(head);
reverseListRecursive(&head);
printf("Reversed list: ");
printList(head); }

Output:-

Result:-
Linked list manipulation:-

Expeceted output:-
Original linked list: 1 2 3 4 5

Reversed linked list: 5 4 3 2 1

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;

struct Node* next;

};

struct Node* reverseLinkedList(struct Node* head) {

struct Node* prev = NULL;

struct Node* current = head;

struct Node* next = NULL;


while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

return prev;

void printLinkedList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

int main() { printf("23811A4237\n");

struct Node* head = createNode(1);


head->next = createNode(2);

head->next->next = createNode(3);

head->next->next->next = createNode(4);

head->next->next->next->next = createNode(5);

printf("Original linked list: ");

printLinkedList(head);

head = reverseLinkedList(head);

printf("Reversed linked list: ");

printLinkedList(head);

Output:-

Result:-

You might also like