DSA Practical File
DSA Practical File
Slot: - B12+B13+E11+E12+E13
By
INDEX
1
S.NO Title Page No
2
Code: -
#include <stdio.h>
int main()
{
int i, n, arr[25];
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter Data:");
for(i = 0; i < n ; i++){
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Output: -
3
Insertion Sort implementation
4
Aim: - write a program to implement Insertion Sort.
Code: -
#include <math.h>
#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;
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Output: -
5
Selection Sort implementation
6
Aim: - write a program to implement Selection Sort.
Code: -
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
if (min_idx != i)
swap(arr[min_idx], arr[i]);
}
}
}
}
int main()
{
int arr[10],n;
cout <<"Enter number of elements"<<endl;
cin >>n;
cout <<"Enter Data:"<<endl;
for(int i=0; i<n; i++){
cin>>arr[i];
}
selectionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
Output: -
7
Linear Search implementation
8
Aim: - write a program to implement Linear search.
Code: -
#include <stdio.h>
int main()
{
int arr[10],n,i;
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter Data:");
for(i = 0; i < n ; i++){
scanf("%d", &arr[i]);
}
int key;
printf("Enter element to be searched");
scanf("%d",&key);
int index = linearSearch(arr,n, key);
if (index == -1) {
printf("The element is not present in the arr.");
}
else {
printf("The element is present at arr[%d].", index);
}
return 0;
}
Output: -
9
Binary Search implementation
10
Aim: - write a program to implement binary search.
Code: -
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x) {
return binarySearch(arr, l, mid - 1, x);
}
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main(void)
{
int arr[10],n,i;
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter Data:");
for(i = 0; i < n ; i++){
scanf("%d", &arr[i]);
}
int key;
printf("Enter element to be searched");
scanf("%d",&key);
if (index == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", index);
}
return 0;
}
Output: -
11
Singly LinkedList implementation
Aim: - write a program to implement Singly LinkedList search.
12
Code: -
#include <stdio.h>
#include <stdlib.h>
struct slinklist {
int data;
struct slinklist *next;
};
int menu() {
int ch;
printf("\n 1. Create a list");
printf("\n 2. Insert a node at the beginning");
printf("\n 3. Insert a node at the end");
printf("\n 4. Insert a node at the middle");
printf("\n 5. Delete a node from the beginning");
printf("\n 6. Delete a node from the end");
printf("\n 7. Delete a node from the middle");
printf("\n 8. Traverse the list (Left to Right)");
printf("\n 9. Traverse the list (Right to Left)");
printf("\n 10. Count nodes");
printf("\n 11. Exit");
printf("\n\n Enter your choice: ");
scanf("%d", &ch);
return ch;
}
node* getnode() {
node *newnode;
newnode = (node *)malloc(sizeof(node));
printf("\n Enter data: ");
scanf("%d", &newnode->data);
newnode->next = NULL;
return newnode;
}
void createlist(int n) {
int i;
node *newnode;
node *temp;
for (i = 0; i < n; i++) {
newnode = getnode();
if (start == NULL) {
start = newnode;
} else {
temp = start;
while (temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
}
}
void traverse() {
13
node *temp;
temp = start;
printf("\n The contents of List (Left to Right): \n");
if (start == NULL) {
printf("\n Empty List");
return;
} else {
while (temp != NULL) {
printf("%d --> ", temp->data);
temp = temp->next;
}
}
printf("X\n");
}
void insert_at_beg() {
node *newnode;
newnode = getnode();
if (start == NULL) {
start = newnode;
} else {
newnode->next = start;
start = newnode;
}
}
void insert_at_end() {
node *newnode, *temp;
newnode = getnode();
if (start == NULL) {
start = newnode;
} else {
temp = start;
while (temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
}
void insert_at_mid() {
node *newnode, *temp, *prev;
int pos, nodectr, ctr = 1;
newnode = getnode();
printf("\n Enter the position: ");
scanf("%d", &pos);
nodectr = countnode(start);
if (pos > 1 && pos < nodectr) {
temp = prev = start;
while (ctr < pos) {
prev = temp;
14
temp = temp->next;
ctr++;
}
prev->next = newnode;
newnode->next = temp;
} else {
printf("Position %d is not a middle position.\n", pos);
}
}
void delete_at_beg() {
node *temp;
if (start == NULL) {
printf("\n No nodes exist.");
return;
} else {
temp = start;
start = temp->next;
free(temp);
printf("\n Node deleted.");
}
}
void delete_at_last() {
node *temp, *prev;
if (start == NULL) {
printf("\n Empty List.");
return;
} else {
temp = start;
prev = start;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
printf("\n Node deleted.");
}
}
void delete_at_mid() {
int ctr = 1, pos, nodectr;
node *temp, *prev;
if (start == NULL) {
printf("\n Empty List.");
return;
}
printf("\n Enter position of node to delete: ");
scanf("%d", &pos);
nodectr = countnode(start);
if (pos > nodectr) {
printf("\n This node does not exist.");
} else if (pos > 1 && pos < nodectr) {
temp = prev = start;
while (ctr < pos) {
prev = temp;
15
temp = temp->next;
ctr++;
}
prev->next = temp->next;
free(temp);
printf("\n Node deleted.");
} else {
printf("\n Invalid position.");
}
}
int main(void) {
int ch, n;
while (1) {
ch = menu();
switch (ch) {
case 1:
if (start == NULL) {
printf("\n Number of nodes you want to create: ");
scanf("%d", &n);
createlist(n);
printf("\n List created.");
} else {
printf("\n List is already created.");
}
break;
case 2:
insert_at_beg();
break;
case 3:
insert_at_end();
break;
case 4:
insert_at_mid();
break;
case 5:
delete_at_beg();
break;
case 6:
delete_at_last();
break;
case 7:
delete_at_mid();
break;
case 8:
traverse();
break;
case 9:
printf("\n The contents of List (Right to Left): \n");
rev_traverse(start);
16
printf("X\n");
break;
case 10:
printf("\n Number of nodes: %d\n", countnode(start));
break;
case 11:
exit(0);
default:
printf("\n Invalid choice. Please try again.");
}
}
return 0;
}
Output: -
17
18
Stack implementation
Aim: - write a program to implement Stack.
Code: -
#include <stdio.h>
#include <stdlib.h>
#define MAX 6
int stack[MAX];
int top = 0;
int menu() {
int ch;
printf("\n... Stack operations using ARRAY ...");
printf("\n-----------**********-------------\n");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Quit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
return ch;
}
void display() {
int i;
if (top == 0) {
printf("\n\nStack empty.");
return;
} else {
printf("\n\nElements in stack:");
for (i = 0; i < top; i++)
printf("\t%d", stack[i]);
}
}
19
void pop() {
if (top == 0) {
printf("\n\nStack Underflow.");
return;
} else
printf("\n\nPopped element is: %d", stack[--top]);
}
void push() {
int data;
if (top == MAX) {
printf("\n\nStack Overflow.");
return;
} else {
printf("\n\nEnter data: ");
scanf("%d", &data);
stack[top] = data;
top++;
printf("\nData pushed into the stack.");
}
}
int main() {
int ch;
do {
ch = menu();
switch (ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
}
} while (1);
}
20
Output: -
21
22