0% found this document useful (0 votes)
5 views61 pages

DS Lab File

The document is a laboratory performance evaluation sheet that includes sections for recording student names, lab codes, and marks obtained in various experiments. It also contains programming exercises in C for matrix operations, stack and queue implementations, and their respective outputs. Each program is aimed at demonstrating fundamental concepts in data structures and algorithms.

Uploaded by

Movies 4 U khan
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)
5 views61 pages

DS Lab File

The document is a laboratory performance evaluation sheet that includes sections for recording student names, lab codes, and marks obtained in various experiments. It also contains programming exercises in C for matrix operations, stack and queue implementations, and their respective outputs. Each program is aimed at demonstrating fundamental concepts in data structures and algorithms.

Uploaded by

Movies 4 U khan
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

Lab Evaluation Sheet

Laboratory Performance Evaluation Sheet


Name of student……………………… Roll No. ………………. Department…………………

Lab Code…………………. Lab Name…………………….. Session………………………

CIE-I
S.N. Date Name of Marks Obtained Remark and
Experiments A(6) C(6) V(5) R(3) Total(20) Signature of
faculty

CIE-II
LT Date Name of Marks Obtained Remark and
Experiment Signature of
A(5) C(5) V(10) R(10) Total(30) faculty
LT-1

LT-2

Marks Obtained
CIE-I CIE-II Total Marks (25) Faculty HoD signature
(Scaled to 10) (Scaled to 15) Signature

A-Arrangement of Experiment setup/Algorithm of Program


C-Conduction/Execution of Experiment with results and calculations
V-Viva-Voce
R-Result and record writing
1
Program #1
1 (a) Addition of Two 2D arrays:

AIM: WAP to find addition of two 2D matrices.

Code :
#include <stdio.h>
#include<conio.h>
void main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element at %d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}

// Adding Two matrices


for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}

// Displaying the result


printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
2
printf("\n\n");
}
}
} // End main()

Output:

Enter number of rows (between 1 and 100): 3


Enter number of columns (between 1 and 100): 3
Enter elements of 1st matrix:
Enter element at 11: 56
Enter element at 12: 7
Enter element at 13: 9
Enter element at 21: 5
Enter element at 22: 2
Enter element at 23: 1
Enter element at 31: 3
Enter element at 32: 9
Enter element at 33: 8
Enter elements of 2nd matrix:
Enter element a11: 3
Enter element a12: 5
Enter element a13: 7
Enter element a21: 4
Enter element a22: 6
Enter element a23: 25
Enter element a31: 9
Enter element a32: 7
Enter element a33: 2

Sum of two matrix is:

8 12 16

9 8 26

12 16 10

3
1 (b) Multiplication of Two 2D arrays:

AIM: WAP to find multiplication of two 2D matrices.

Code :
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);

// Column of first matrix should be equal to column of second matrix and


while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}

// Storing elements of first matrix.


printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}

// Storing elements of second matrix.


printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}

// Initializing all elements of result matrix to 0


for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
4
result[i][j] = 0;
}

// Multiplying matrices a and b and


// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}

// Displaying the result


printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
getch();
} // End main()

Output:

Enter the first matrix:


Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements row-wise:
123
456
Enter the second matrix:
Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements row-wise:
78
9 10
11 12

Resultant Matrix after Multiplication:


[58, 64]
[139, 154]

5
Program #2
2- Transpose of a 2D array:
AIM: Write a program in C to find transpose of a 2D array.
Code :
#include <stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);
for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
printf("Transpose of the matrix:\n");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}
getch();
}

Output:

Original Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Transposed Matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

6
Program #3
3- Implementation of stack using array:

AIM: Write a program in C to implement stack using array.

Code:
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
void main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t Stack Operations ");
printf("\n\t--------------------------------");
printf("\n\t [Link]\n\t [Link]\n\t [Link]\n\t [Link]");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}

case 4:
{
printf("\n\t EXIT POINT ");
7
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
getch();
}

void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}

void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}

void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
8
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}

Output:

Enter the capacity of the stack: 5

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 10
Pushed item: 10

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 20
Pushed item: 20

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top item of the stack: 20

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
9
Enter your choice: 4
Stack contents:
20
10

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped item: 20

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack contents:
10

Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 5
Exiting...

10
Program #4
4- Implementation of queue using array:

AIM: Write a program in C to implement queue using array.

Code:

#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\[Link] \[Link] \[Link] \[Link]");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
11
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}

Output:

Enter the capacity of the queue: 5

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to enqueue: 10
Enqueued: 10

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to enqueue: 20
Enqueued: 20

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
12
5. Exit
Enter your choice: 4
Queue: 10 20

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 3
Peeked item: 10

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 2
Dequeued: 10

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 4
Queue: 20

Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 5
Exiting...

13
Program #5
5- Implementation of circular queue using array:

AIM: Write a program in C to implement a circular queue using array.

Code:
#include <stdio.h>
#include<conio.h>
#define size 5

void insertq(int[], int);


void deleteq(int[]);
void display(int[]);

int front = - 1;
int rear = - 1;
void main()
{
int n, ch;
int queue[size];
do
{
printf("\n\n Circular Queue:\n1. Insert \n2. Delete\[Link]\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number: ");
scanf("%d", &n);
insertq(queue, n);
break;
case 2:
deleteq(queue);
break;
case 3:
display(queue);
break;
}
}while (ch != 0);
}

void insertq(int queue[], int item)


{
if ((front == 0 && rear == size - 1) || (front == rear + 1))
{
14
printf("queue is full");
return;
}
else if (rear == - 1)
{
rear++;
front++;
}
else if (rear == size - 1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}

void display(int queue[])


{
int i;
printf("\n");
if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%d ", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
}

void deleteq(int queue[])


{
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
15
printf("\n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
else
{
printf("\n %d deleted", queue[front]);
front++;
}
}

Output:

Enter the capacity of the circular queue: 5

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 20

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
10 20 30

1. Enqueue
2. Dequeue
3. Display
16
4. Exit
Enter your choice: 2
Dequeued item: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
20 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...

17
Program #6
6- Implementation of a stack using linked list.

AIM: Write a program in C to implement a stack using linked list.

Code:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>

struct Node
{
int Data;
struct Node *next;
}*top;

void pop Stack()


{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}

void push(int value)


{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}

void display()
18
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}

int main(int argc, char *argv[])


{
int i=0;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
19
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

Output:

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 10

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 20

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 30
20
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Current stack:
30 -> 20 -> 10 -> None

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped item: 30

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top item: 20

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 5
Exiting...

21
Program #7
7- Implementation of a queue using linked list.

AIM: Write a program in C to implement a queue using linked list.

Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;

void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}

void push(int value)


{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}

22
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}

void main()
{
int i=0;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
23
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

Output:

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 20

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
10 20 30

24
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued item: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
20 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...

25
Program #8
8- Implementation of a circular queue using linked list.

AIM: Write a program in C to implement a circular queue using linked list.

Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;

void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=rear;
}
}
void display()
{
26
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=front)
{
printf("\t%d",var->Data);
var=var->next;
}
if(var==front)
{
printf("\t%d",var->Data);
}
printf("\n");
}
else
printf("\nQueue is Empty");
}

void main(int argc, char *argv[])


{
int i=0;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
27
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

Output:

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 20

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
28
10 20 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued item: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
20 30

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...

29
Program #9
9- Implementation of a binary tree using linked list.

AIM: Write a program in C to implement a binary tree using linked list.

Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the binary tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a node into the binary tree


struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}

// Function to display the binary tree in inorder traversal


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

// Function to display the binary tree in preorder traversal


void preorder(struct Node* root) {
30
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

// Function to display the binary tree in postorder traversal


void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
31
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}

Output:

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 50

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 30

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 70

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 2
Preorder Traversal: 50 30 70

1. Insert Node
2. Inorder Traversal
32
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 3
Inorder Traversal: 30 50 70

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 4
Postorder Traversal: 30 70 50

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 5
Exiting...

33
Program #10
10- Implementation of a binary search tree using linked list.

AIM: Write a program in C to implement a binary search tree using linked list.

Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the binary search tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a node into the binary search tree


struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}

// Function to display the binary search tree in inorder traversal


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

// Function to display the binary search tree in preorder traversal


34
void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

// Function to display the binary search tree in postorder traversal


void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorder(root);
35
printf("\n");
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}

Output:

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 50

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 30

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 70

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 2
Preorder Traversal: 50 30 70

1. Insert Node
36
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 3
Inorder Traversal: 30 50 70

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 4
Postorder Traversal: 30 70 50

1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 5
Exiting...

37
Program #11
11- Implementation of a tree traversal using linked list.

AIM: Write a program in C to implement a tree traversal using linked list.

Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the binary tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a node into the binary tree


struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}

// Function for inorder traversal


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

// Function for preorder traversal


38
void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

// Function for postorder traversal


void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

int main() {
struct Node* root = NULL;
int choice, data;
// Inserting nodes into the binary tree
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
while (1) {
printf("\n1. Inorder Traversal\n");
printf("2. Preorder Traversal\n");
printf("3. Postorder Traversal\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 2:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 3:
39
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}

Output:

1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 1
Inorder Traversal: 20 30 40 50 60 70 80

1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 2
Preorder Traversal: 50 30 20 40 70 60 80

1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 3
Postorder Traversal: 20 40 30 60 80 70 50

1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 4
Exiting...

40
Program #12
12- Implementation of BFS using linked list.

AIM: Write a program in C to implement BFS using linked list.

Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the adjacency list
struct AdjListNode {
int dest;
struct AdjListNode* next;
};
// Define a structure for the adjacency list
struct AdjList {
struct AdjListNode* head;
};
// Define a structure for the graph
struct Graph {
int V;
struct AdjList* array;
};
// Function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// Function to create a graph with V vertices
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// Function to perform breadth-first search traversal
41
void BFS(struct Graph* graph, int start) {
// Create a queue for BFS
int* queue = (int*)malloc(graph->V * sizeof(int));
int front = 0, rear = 0;
// Mark all the vertices as not visited
int* visited = (int*)malloc(graph->V * sizeof(int));
for (int i = 0; i < graph->V; ++i) {
visited[i] = 0;
}

// Enqueue the starting vertex and mark it as visited


queue[rear++] = start;
visited[start] = 1;
while (front < rear) {
// Dequeue a vertex from the queue and print it
int current = queue[front++];
printf("%d ", current);
// Get all adjacent vertices of the dequeued vertex current
// If an adjacent vertex has not been visited, then enqueue it and mark it as visited
struct AdjListNode* temp = graph->array[current].head;
while (temp) {
int adj = temp->dest;
if (!visited[adj]) {
queue[rear++] = adj;
visited[adj] = 1;
}
temp = temp->next;
}
}
free(queue);
free(visited);
}

int main() {
int V, E, start;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &V);
struct Graph* graph = createGraph(V);
printf("Enter the number of edges in the graph: ");
scanf("%d", &E);
printf("Enter the edges (src dest):\n");
for (int i = 0; i < E; ++i) {
int src, dest;
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
printf("Enter the starting vertex for BFS traversal: ");
42
scanf("%d", &start);
printf("BFS Traversal: ");
BFS(graph, start);
printf("\n");
return 0;
}

Output:

Enter the number of vertices in the graph: 5


Enter the number of edges in the graph: 4
Enter the edges (src dest):
01
02
13
24
Enter the starting vertex for BFS traversal: 0
BFS Traversal: 0 1 2 3 4

43
Program #13
13- Implementation of DFS using linked list.

AIM: Write a program in C to implement DFS using linked list.

Code:
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a node in the adjacency list


struct AdjListNode {
int dest;
struct AdjListNode* next;
};
// Define a structure for the adjacency list
struct AdjList {
struct AdjListNode* head;
};
// Define a structure for the graph
struct Graph {
int V;
struct AdjList* array;
};

// Function to create a new adjacency list node


struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}

// Function to create a graph with V vertices


struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}

// Function to add an edge to the graph


void addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode* newNode = newAdjListNode(dest);
44
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since the graph is undirected, add an edge from dest to src as well
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}

// Function to perform DFS traversal from a given vertex


void DFSUtil(int v, struct Graph* graph, int* visited) {
visited[v] = 1;
printf("%d ", v);
struct AdjListNode* temp = graph->array[v].head;
while (temp) {
if (!visited[temp->dest])
DFSUtil(temp->dest, graph, visited);
temp = temp->next;
}
}

// Function to perform DFS traversal


void DFS(struct Graph* graph, int start) {
int* visited = (int*)malloc(graph->V * sizeof(int));
int i;
for (i = 0; i < graph->V; ++i)
visited[i] = 0;
DFSUtil(start, graph, visited);
}

int main() {
int V, E, i, src, dest;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &V);
printf("Enter the number of edges in the graph: ");
scanf("%d", &E);
struct Graph* graph = createGraph(V);
printf("Enter the edges (src dest):\n");
for (i = 0; i < E; ++i) {
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
int start;
printf("Enter the starting vertex for DFS traversal: ");
scanf("%d", &start);
printf("Depth-First Search (DFS) traversal starting from vertex %d: ", start);
DFS(graph, start);
printf("\n");
45
return 0;
}

Output:

Enter the number of vertices in the graph: 5


Enter the number of edges in the graph: 4
Enter the edges (src dest):
01
02
13
14
Enter the starting vertex for DFS traversal: 0
Depth-First Search (DFS) traversal starting from vertex 0: 0 1 3 4 2

46
Program #14
14- Implementation of linear search.

AIM: Write a program in C to implement the linear search.

Code:
#include <stdio.h>

// Function for linear search


int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index if key is found
}
}
return -1; // Return -1 if key is not found
}

int main() {
int n, key;
// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Input the elements of the array
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the element to be searched
printf("Enter the element to search: ");
scanf("%d", &key);
// Perform linear search
int index = linearSearch(arr, n, key);
// Display the result
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:

Enter the number of elements in the array: 5


47
Enter the elements of the array:
10 20 30 40 50
Enter the element to search: 30
Element 30 found at index 2.

48
Program #15
15- Implementation of binary search.

AIM: Write a program in C to implement binary search.

Code:
#include <stdio.h>
// Function to perform binary search
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1; // Element not found
}

int main() {
int size, key, result;
// Input size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Input elements of the array
int arr[size];
printf("Enter the elements of the array in sorted order:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the element to search for
printf("Enter the element to search for: ");
scanf("%d", &key);
// Perform binary search
result = binarySearch(arr, size, key);
// Display result
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found\n");
}

return 0;
49
}

Output:

Enter the size of the array: 5


Enter the elements of the array in sorted order:
10
20
30
40
50
Enter the element to search for: 30
Element found at index: 2

50
Program #16
16- Implementation of the bubble sort.

AIM: Write a program in C to implement the bubble sort.

Code:
#include <stdio.h>
// Function to perform bubble sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main() {
int n, i;
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sort the array using bubble sort
bubbleSort(arr, n);
// Display the sorted array
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

51
Enter the number of elements in the array: 5
Enter 5 elements:
12 5 8 19 3
Sorted array: 3 5 8 12 19

52
Program #17
17- Implementation of selection sort.

AIM: Write a program in C to implement the selection sort.

Code:
#include <stdio.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the current element
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform selection sort
selectionSort(arr, n);
// Display the sorted array
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

53
Enter the number of elements: 5
Enter the elements:
12
45
7
23
9
Sorted array:
7 9 12 23 45

54
Program #18
18- Implementation of insertion sort.

AIM: Write a program in C/JAVA to implement the insertion sort.

Code:
#include <stdio.h>
// Function to perform insertion sort
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 = j - 1;
}
arr[j + 1] = key;
}
}

int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform insertion sort
insertionSort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

Enter the number of elements in the array: 5


Enter the elements of the array:
55
12 11 13 5 6
Sorted array: 5 6 11 12 13

56
Program #19
19- Implementation of merge sort.

AIM: Write a program in C to implement the merge sort.

Code:
#include <stdio.h>
#include <stdlib.h>
// Merge two subarrays of arr[]
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temporary arrays
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temporary arrays back into arr[l..r]
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

// Copy the remaining elements of L[], if any


while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy the remaining elements of R[], if any


while (j < n2) {
57
arr[k] = R[j];
j++;
k++;
}
}

// Main function to perform merge sort


void mergeSort(int arr[], int l, int r) {
if (l < r) {
// Same as (l+r)/2, but avoids overflow for large l and r
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}

int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Perform merge sort


mergeSort(arr, 0, n - 1);
// Display the sorted array
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

Enter the number of elements in the array: 5


Enter the elements of the array:
12
11
13
58
5
6
Sorted array: 5 6 11 12 13

59
Program #20
20- Implementation of heap sort.

AIM: Write a program in C/JAVA to implement heap sort.

Code:
#include <stdio.h>
#include <stdlib.h>
// Function to heapify a subtree rooted with node i which is an index in arr[]
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}

// main function to do heap sort


void heapSort(int arr[], int n) {
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}

int main() {
60
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform heap sort
heapSort(arr, n);
// Print the sorted array
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

Enter the number of elements in the array: 5


Enter the elements of the array:
12 11 13 5 6
Sorted array: 5 6 11 12 13

61

You might also like