DSA Lab CIA 2
DSA Lab CIA 2
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
06-03-2024
Binary Search Tree (BST) */
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
if (x < root->data) {
root->left = insertBST(root->left, x);
} else if (x > root->data) {
root->right = insertBST(root->right, x);
} else {
printf("Duplicate value. Cannot insert.\n");
}
return root;
}
return root->data;
}
int maximum(struct Node *root) {
if (root == NULL) {
printf("Error: Empty tree.\n");
return -1;
}
return root->data;
}
if (x < root->data) {
return searchBST(root->left, x);
} else if (x > root->data) {
return searchBST(root->right, x);
} else {
return root;
}
}
if (x < root->data) {
root->left = deleteBST(root->left, x);
} else if (x > root->data) {
root->right = deleteBST(root->right, x);
} else {
if (root->left == NULL) {
struct Node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node *temp = root->left;
free(root);
return temp;
}
return root;
}
int main() {
struct Node *root = NULL;
int n, value;
printf("Enter Number of Data to be Inserted: ");
scanf("%d", &n);
for (int i=0; i<n; i++) {
printf("Enter Value to be Inserted: ");
scanf("%d", &value);
root = insertBST(root, value);
}
switch (choice) {
case 1:
printf("Enter Element to Search: ");
scanf("%d", &key);
struct Node *found = searchBST(root, key);
if (found) {
printf("Element %d found\n", found->data);
} else {
printf("Element NOT found\n");
}
minVal = minimum(root);
maxVal = maximum(root);
printf("Minimum Value: %d\n", minVal);
printf("Maximum Value: %d\n", maxVal);
break;
case 2:
printf("Enter Element to Delete: ");
scanf("%d", &del);
root = deleteBST(root, del);
printf("Inorder Traversal After Deletion: ");
inorder(root);
printf("\n");
break;
case 0:
exit(0);
default:
printf("Invalid Choice.\n");
}
}
printf("\n");
return 0;
}
OUTPUT:
Menu:
1. Search Value
2. Delete Value
0. Exit
Enter your choice: 1
Enter Element to Search: 3
Element 3 found
Minimum value: 1
Maximum value: 5
Menu:
1. Search Value
2. Delete Value
0. Exit
Enter your choice: 2
Enter Element to Deleted: 4
Inorder traversal after deletion: 1 2 3 5
Menu:
1. Search Value
2. Delete Value
0. Exit
Enter your choice: 2
Enter Element to Deleted: 5
Inorder traversal after deletion: 1 2 3
Menu:
1. Search Value
2. Delete Value
0. Exit
Enter your choice: 1
Enter Element to Search: 4
Element not found
Minimum value: 1
Maximum value: 3
Menu:
1. Search Value
2. Delete Value
0. Exit
Enter your choice: 0
C Program 06-03-2024 (DSA)
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
06-03-2024
Line Editor */
1. LEdit.c
#include <stdio.h>
#include "Editor.c"
#define MAX_LEN 80
int main()
{
int op;
int lcount=0, lno;
char line[100], filename[25], word[25], rword[25];
char *t;
HEAD *head=NULL;
do
{
printf("\n1. New File\n2. Read from file\n3. Save to file\n4. Display Contents");
printf("\n5. Insert a new line\n6. Find word\n7. Delete a line");
printf("\nEnter your option: ");
scanf("%d", &op);
switch(op)
{
case 1: getchar();
head = createHead();
lcount = 0;
do
{
printf("\n%d. ",++lcount);
if (gets(line)!= NULL)
{
if (strcmp(line, "END")==0)
break;
Append(head, line);
}
}
while (!feof(stdin));
break;
case 2: printf("\nEnter filename : ");
scanf("%s", filename);
head = Read_From_File(filename);
break;
case 3: if (head != NULL)
{
printf("\nEnter filename : ");
scanf("%s", filename);
Save_To_File(head, filename);
}
break;
case 4: if (head != NULL)
{
printf("\nCurrent contents : \n");
Display(head);
}
break;
case 5: if (head != NULL)
{
printf("Enter the line number : ");
scanf("%d", &lno);
getchar();
printf("Enter the text to insert : ");
if (gets(line) != NULL)
Insert_At_Loc(head, lno, line);
}
break;
case 6: if (head != NULL)
{
printf("Enter the word to search : ");
scanf("%s", word);
Find(head, word);
}
break;
case 7: if (head != NULL)
{
printf("Enter the line number to delete : ");
scanf("%d", &lno);
Delete_At_Loc(head,lno);
}
break;
}
}
while(op <= 7);
return 0;
}
2. dll.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dlltype.h"
HEAD *createHead()
{
HEAD *head = (HEAD *) malloc(sizeof(HEAD));
head->start = head->end = NULL;
head->LC = head->TWC = 0;
return head;
}
void Append(HEAD *head, char line[100])
{
int i, k, WC=0;
char word[20];
LINEHEAD *lh;
NODE *last;
NODE *nn;
lh = (LINEHEAD *)malloc(sizeof(LINEHEAD));
lh->P = lh->N =NULL;
lh->first = NULL;
lh->WC = 0;
if (head->end == NULL){
head->start = head->end = lh;
lh->P = lh;
lh->N = lh;
}
else
{
lh->P = head->end;
lh->N = head->end->N;
head->end->N = lh;
head->end = lh;
head->start->P = lh;
}
i=0;
while (line[i]!='\0'){
k = 0;
while (!isspace(line[i]))
{
word[k++]=line[i++];
if (line[i]=='\0') break;
}
word[k]='\0';
(lh->WC)++;
nn = (NODE*)malloc(sizeof(NODE));
nn->next = nn->prev = NULL;
strcpy(nn->word,word);
if (lh->first == NULL)
{
lh->first = nn;
nn->prev = nn->next = NULL;
last = nn;
}
else
{
nn->prev = last;
last->next = nn;
last = nn;
}
while (isspace(line[i])) i++;
}
head->TWC += lh->WC;
(head->LC)++;
}
cur_line = head->start;
while (cur_line != head->end)
{
pos = 0;
cur_word = cur_line->first;
while (cur_word != NULL)
{
pos++;
if (strcmp(cur_word->word, word) == 0)
{
flag = 1;
printf("\nOccurrence of %s is in Line - %d at position %d", word, i, pos);
}
cur_word = cur_word->next;
}
cur_line = cur_line->N;
i++;
}
cur_word = head->end->first;
pos = 0;
while (cur_word != NULL)
{
pos++;
if (strcmp(cur_word->word, word) == 0)
{
flag = 1;
printf("\nOccurrence of %s is in Line - %d at position %d", word, i, pos);
}
cur_word = cur_word->next;
}
if (!flag)
printf("\n%s is not present in the text.", word);
}
void Display(HEAD *head)
{
int i;
LINEHEAD *lh = head->start;
i=1;
while (lh!= head->end){
3. dlltype.h
#ifndef DLLTYPE_H
#define DLLTYPE_H
typedef struct
{
struct linehead *start;
int LC;
int TWC;
struct linehead *end;
}HEAD;
HEAD* createHead();
void Append(HEAD *head, char line[]);
void Insert_At_Loc(HEAD *head, int loc, char *line);
void Delete_At_Loc(HEAD *head, int loc);
void Find(HEAD *head, char word[]);
void Display(HEAD *head);
#endif
4. Editor.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Editor.h"
#include "dll.c"
#define MAX_LEN 80
5. Editor.h
#include "dlltype.h"
HEAD *Read_From_File(char filename[]);
void Save_To_File(HEAD *head, char filename[]);
OUTPUT:
1. New File
2. Read from file
3. Save to file
4. Display Contents
5. Insert a new line
6. Find word
7. Delete a line
Enter your option: 1
(Entering lines...)
Current contents:
1. Line 1
2. Line 2
3. Line 3
No. of lines = 3
No. of words = 9
(Updating lines...)
Occurrences found.
(Deleting line...)
Current contents:
1. Line 1
2. Line 3
No. of lines = 2
No. of words = 6
Current contents:
1. Line 1
2. Line 3
No. of lines = 2
No. of words = 6
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
27-03-2024
Insertion Sort */
#include <stdio.h>
int main()
{
int size, value;
printf("Enter Number of Elements in an Array: ");
scanf("%d", &size);
int arr[size];
for(int i = 0; i < size; i++)
{
printf("Enter Value: ");
scanf("%d", &value);
arr[i] = value;
}
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
OUTPUT:
Enter Number of Elements in an Array: 5
Enter Value: 5
Enter Value: 4
Enter Value: 9
Enter Value: 8
Enter Value: 3
Sorted array:
34589
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
27-03-2024
Merge Sort */
#include <stdio.h>
int i = 0, j = 0, k = low;
while (i < n1 && j < n2)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}
int main()
{
int size, value;
printf("Enter Number of Elements in an Array: ");
scanf("%d", &size);
int arr[size];
for(int i = 0; i < size; i++)
{
printf("Enter Value: ");
scanf("%d", &value);
arr[i] = value;
}
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
return 0;
}
OUTPUT:
Enter Number of Elements in an Array: 5
Enter Value: 9
Enter Value: 4
Enter Value: 5
Enter Value: 8
Enter Value: 3
Sorted array:
34589
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
03-04-2024
BFS */
#include <stdio.h>
#include <stdlib.h>
int queue[MAX_VERTICES];
int front = -1, rear = -1;
int visited[MAX_VERTICES];
struct Node
{
int data;
struct Node* next;
};
struct Graph
{
int numVertices;
struct Node** adjLists;
};
return graph;
}
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
int dequeue()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return -1;
}
else
{
return queue[front++];
}
}
enqueue(startVertex);
markVisited(startVertex);
while (temp)
{
if (!isVisited(temp->data))
{
enqueue(temp->data);
markVisited(temp->data);
}
temp = temp->next;
}
}
}
int main()
{
int numVertices, numEdges, source;
OUTPUT:
Enter the Number of Vertices: 4
Enter the Number of Edges: 3
Enter Source Vertex: 1
Enter Source for Edge 1: 1
Enter Destination for Edge 1: 2
Enter Source for Edge 2: 2
Enter Destination for Edge 2: 3
Enter Source for Edge 3: 3
Enter Destination for Edge 3: 4
BFS Traversal from Source 1:
1234
2. Write a C program for Traversing the Graph in DFS
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
03-04-2024
DFS */
#include <stdio.h>
#include <stdlib.h>
int visited[MAX_VERTICES];
struct Node
{
int data;
struct Node* next;
};
struct Graph
{
int numVertices;
struct Node** adjLists;
};
return graph;
}
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
while (temp)
{
if (!visited[temp->data])
{
DFSUtil(graph, temp->data);
}
temp = temp->next;
}
}
DFSUtil(graph, startVertex);
}
int main()
{
int numVertices, numEdges, source;
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
03-04-2024
AVL */
1. AVLTREE.c
#include <stdio.h>
#include <stdlib.h>
#include "AVL.h"
2. AVL.h
#include <stdio.h>
typedef struct node
{
int data;
struct node *lchild;
struct node *rchild;
}TNODE;
3. AVLMain.c
#include <stdio.h>
#include "AVLTREE.c"
int main()
{
TNODE *root= NULL;
int d, op;
do
{
printf( "1. Insert Element \n2. Search Element \n3. Traverse\n4. Exit\n");
scanf("%d", &op);
switch (op)
{
case 1: {
printf( "Enter element to insert ");
scanf("%d", &d);
root = insertnode(root, d);
printf( "Preorder : ");
preorder(root);
printf("\nInorder : ");
inorder(root);
printf("\nPostorder : ");
postorder(root);
printf("\n");
break;
}
case 2: {
printf( "Enter element to search ");
scanf("%d", &d);
search(root, d);
break;
}
case 3: {
printf( "Preorder : ");
preorder(root);
printf("\nInorder : ");
inorder(root);
printf( "\nPostorder : ");
postorder(root);
printf("\n");
break;
}
}
}
while (op < 4);
}
OUTPUT:
1. Insert Element
2. Search Element
3. Traverse
4. Exit
1
Enter element to insert 10
Preorder : 10
Inorder : 10
Postorder : 10
1. Insert Element
2. Search Element
3. Traverse
4. Exit
1
Enter element to insert 5
Left Rotate on : 10
Preorder : 5 10
Inorder : 5 10
Postorder : 10 5
1. Insert Element
2. Search Element
3. Traverse
4. Exit
1
Enter element to insert 15
Right Rotate on : 10
Preorder : 5 10 15
Inorder : 5 10 15
Postorder : 15 10 5
1. Insert Element
2. Search Element
3. Traverse
4. Exit
1
Enter element to insert 20
Right Rotate on : 10
Left Rotate on : 15
Preorder : 5 15 10 20
Inorder : 5 10 15 20
Postorder : 10 20 15 5
1. Insert Element
2. Search Element
3. Traverse
4. Exit
2
Enter element to search 15
The element is found after 2 comparisons.
1. Insert Element
2. Search Element
3. Traverse
4. Exit
2
Enter element to search 25
The element is not found.
1. Insert Element
2. Search Element
3. Traverse
4. Exit
4
C Program 03-04-2024 (DSA)
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
03-04-2024
Hash Search */
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct Node {
int data;
struct Node* next;
};
void initializeHashTable() {
for (int i = 0; i < SIZE; ++i) {
hashTable[i] = NULL;
}
}
if (hashTable[index] == NULL) {
hashTable[index] = newNode;
} else {
struct Node* temp = hashTable[index];
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
int main() {
initializeHashTable();
while (1) {
printf("\n1. Insert\n2. Search\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(data);
break;
case 2:
printf("Enter data to search: ");
scanf("%d", &data);
search(data);
break;
case 3:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT:
1. Insert
2. Search
3. Exit
Enter your choice: 1
Enter data to insert: 5
1. Insert
2. Search
3. Exit
Enter your choice: 1
Enter data to insert: 15
1. Insert
2. Search
3. Exit
Enter your choice: 1
Enter data to insert: 25
1. Insert
2. Search
3. Exit
Enter your choice: 2
Enter data to search: 15
Data 15 found in hash table.
1. Insert
2. Search
3. Exit
Enter your choice: 2
Enter data to search: 20
Data 20 not found in hash table.
1. Insert
2. Search
3. Exit
Enter your choice: 3
Exiting program.
C Program 10-04-2024 (DSA)
Source Code:
/* Surianandhan S
127018060
CSBS 2nd Semester
10-04-2024
Saving / retrieving non-linear data structure in / from a file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Edge {
int weight;
struct Vertex* destination;
struct Edge* next;
};
struct Vertex {
char name[50];
struct Edge* edge;
};
struct Graph {
struct Vertex vertices[MAX_VERTICES];
int numVertices;
};
fclose(file);
}
int main() {
struct Graph g;
char filename[100];
printf("\nGraph Details:\n");
displayGraph(&g);
return 0;
}
OUTPUT:
Enter file name to store graph details: graph.txt
Graph Details:
Number of vertices: 3
A
→ Weight: 10, Destination: B
→ Weight: 20, Destination: C
B
→ Weight: 15, Destination: C
C
Graph Details:
Number of vertices: 4
A
→ Weight: 5, Destination: B
→ Weight: 10, Destination: C
B
→ Weight: 15, Destination: D
C
→ Weight: 20, Destination: D
D