0% found this document useful (0 votes)
62 views203 pages

Lab Record Download 2

Uploaded by

rishabhtyagi2005
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)
62 views203 pages

Lab Record Download 2

Uploaded by

rishabhtyagi2005
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/ 203

Exp.

Name: Write a C program to Sort

Page No: 1
Date: 2024-12-
S.No: 1 the given elements in Ascending order
15
using Bubble Sort

ID: 2300331530093
Aim:
Write a program to sort the given elements using
Bubble sort technique ( Ascending order ) .
Source Code:

bubbleSort.c
#include<stdio.h>
void main(){
int a[10];
int i,j,num,temp;

2023-27-CSE-AIML-B
printf("n : ");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("a[%d] = ",i);
scanf(" %d",&a[i]);
}
printf("Before sorting : \n");
for(i=0;i<num;i++){
printf("a[%d] = %d\n",i,a[i]);

Raj Kumar Goel Institute Of Technology


}
for(i=0;i<num;i++){
for(j=0;j<(num-i-1);j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting : \n");
for(i=0;i<num;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 2
User Output
n :
5

ID: 2300331530093
a[0] =
2
a[1] =
7
a[2] =
6
a[3] =
4
a[4] =
1

2023-27-CSE-AIML-B
Before sorting :
a[0] = 2
a[1] = 7
a[2] = 6
a[3] = 4
a[4] = 1
After sorting :

Raj Kumar Goel Institute Of Technology


a[0] = 1
a[1] = 2
a[2] = 4
a[3] = 6
a[4] = 7

Test Case - 2

User Output
n :
4
a[0] =
28
a[1] =
34
a[2] =
26
a[3] =
29
Before sorting :

Page No: 3
a[0] = 28
a[1] = 34
a[2] = 26
a[3] = 29

ID: 2300331530093
After sorting :
a[0] = 26
a[1] = 28
a[2] = 29
a[3] = 34

Test Case - 3

User Output

2023-27-CSE-AIML-B
n :
5
a[0] =
-45
a[1] =
-12
a[2] =

Raj Kumar Goel Institute Of Technology


-77
a[3] =
-21
a[4] =
-100
Before sorting :
a[0] = -45
a[1] = -12
a[2] = -77
a[3] = -21
a[4] = -100
After sorting :
a[0] = -100
a[1] = -77
a[2] = -45
a[3] = -21
a[4] = -12
Exp. Name: Write a C program to Sort
Date: 2024-12-

Page No: 4
S.No: 2 the elements using Insertion Sort
20
Technique

Aim:

ID: 2300331530093
Write a program to sort the given elements using
Insertion sort technique ( Ascending order ).
Source Code:

insertionSort.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

Page No: 5
void main() {
int a[20], i, n, j, temp;
printf("n = ");
scanf("%d", &n);
// Write the for loop to read array elements

ID: 2300331530093
for(i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}

printf("Before sorting : \n");


// Write the for loop to display array elements before sorting
for(i=0;i<n;i++)
{

2023-27-CSE-AIML-B
printf("a[%d] = %d\n",i,a[i]);
}

//Write the code to sort elements


for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)

Raj Kumar Goel Institute Of Technology


{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting : \n");
// Write the for loop to display array elements after sorting
for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 6
n =
5
a[0] =
12

ID: 2300331530093
a[1] =
4
a[2] =
27
a[3] =
37
a[4] =
41
Before sorting :
a[0] = 12

2023-27-CSE-AIML-B
a[1] = 4
a[2] = 27
a[3] = 37
a[4] = 41
After sorting :
a[0] = 4
a[1] = 12

Raj Kumar Goel Institute Of Technology


a[2] = 27
a[3] = 37
a[4] = 41

Test Case - 2

User Output
n =
7
a[0] =
3
a[1] =
8
a[2] =
2
a[3] =
1
a[4] =
4

Page No: 7
a[5] =
9
a[6] =
4

ID: 2300331530093
Before sorting :
a[0] = 3
a[1] = 8
a[2] = 2
a[3] = 1
a[4] = 4
a[5] = 9
a[6] = 4
After sorting :
a[0] = 1

2023-27-CSE-AIML-B
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 4
a[5] = 8
a[6] = 9

Raj Kumar Goel Institute Of Technology


Test Case - 3

User Output
n =
5
a[0] =
-36
a[1] =
-100
a[2] =
-43
a[3] =
54
a[4] =
0
Before sorting :
a[0] = -36
a[1] = -100
a[2] = -43

Page No: 8
a[3] = 54
a[4] = 0
After sorting :
a[0] = -100

ID: 2300331530093
a[1] = -43
a[2] = -36
a[3] = 0
a[4] = 54

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a C program to Sort
Date: 2024-12-

Page No: 9
S.No: 3 given elements using Selection sort
15
largest element method

Aim:

ID: 2300331530093
Write a program to sort the given array elements using
Selection sort largest element method( Ascending order ).
Source Code:

selectionSort.c
#include<stdio.h>
void main(){
int a [10];
int i,j,n,temp,max;
printf("n = ");

2023-27-CSE-AIML-B
scanf("%d",&n);
for(i=0;i<n;i++){
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("Before sorting : \n");
for(i=0;i<n;i++){
printf("a[%d] = %d\n",i,a[i]);
}

Raj Kumar Goel Institute Of Technology


printf("After sorting : \n");
for(i=0;i<n-1;i++){
for(j=i;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++){
printf("a[%d] = %d\n",i,a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 10
User Output
n =
7
a[0] =

ID: 2300331530093
5
a[1] =
2
a[2] =
3
a[3] =
4
a[4] =
6

2023-27-CSE-AIML-B
a[5] =
1
a[6] =
9
Before sorting :
a[0] = 5
a[1] = 2

Raj Kumar Goel Institute Of Technology


a[2] = 3
a[3] = 4
a[4] = 6
a[5] = 1
a[6] = 9
After sorting :
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 9

Test Case - 2

User Output
n =
5
a[0] =

Page No: 11
45
a[1] =
25
a[2] =

ID: 2300331530093
67
a[3] =
89
a[4] =
44
Before sorting :
a[0] = 45
a[1] = 25
a[2] = 67

2023-27-CSE-AIML-B
a[3] = 89
a[4] = 44
After sorting :
a[0] = 25
a[1] = 44
a[2] = 45
a[3] = 67
a[4] = 89

Raj Kumar Goel Institute Of Technology


Test Case - 3

User Output
n =
4
a[0] =
-9
a[1] =
-54
a[2] =
-12
a[3] =
-369
Before sorting :
a[0] = -9
a[1] = -54
a[3] = -9
a[2] = -12
a[1] = -54
a[2] = -12

a[0] = -369
a[3] = -369
After sorting :

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 12
Exp. Name: Write a C program to sort
Date: 2024-12-

Page No: 13
S.No: 4 given elements using Selection sort
15
smallest element method

Aim:

ID: 2300331530093
Write a program to sort the given array elements using
Selection sort smallest element method( Ascending order ).
Source Code:

selctionSmallest.c

#include<stdio.h>
void main(){
int a[10];
int i,j,n,temp,max;
printf("n = ");

2023-27-CSE-AIML-B
scanf("%d",&n);
for(i=0;i<n;i++){
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("Before sorting : \n");
for(i=0;i<n;i++){
printf("a[%d] = %d\n",i,a[i]);
}

Raj Kumar Goel Institute Of Technology


printf("After sorting : \n");
for(i=0;i<n;i++){
for(j=i;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++){
printf("a[%d] = %d\n",i,a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 14
User Output
n =
5
a[0] =

ID: 2300331530093
15
a[1] =
45
a[2] =
1
a[3] =
20
a[4] =
30

2023-27-CSE-AIML-B
Before sorting :
a[0] = 15
a[1] = 45
a[2] = 1
a[3] = 20
a[4] = 30
After sorting :

Raj Kumar Goel Institute Of Technology


a[0] = 1
a[1] = 15
a[2] = 20
a[3] = 30
a[4] = 45

Test Case - 2

User Output
n =
4
a[0] =
-15
a[1] =
-12
a[2] =
-48
a[3] =
-79
Before sorting :

Page No: 15
a[0] = -15
a[1] = -12
a[2] = -48
a[3] = -79

ID: 2300331530093
After sorting :
a[0] = -79
a[1] = -48
a[2] = -15
a[3] = -12

Test Case - 3

User Output

2023-27-CSE-AIML-B
n =
5
a[0] =
34
a[1] =
68
a[2] =

Raj Kumar Goel Institute Of Technology


95
a[3] =
41
a[4] =
23
Before sorting :
a[0] = 34
a[1] = 68
a[2] = 95
a[3] = 41
a[4] = 23
After sorting :
a[0] = 23
a[1] = 34
a[2] = 41
a[3] = 68
a[4] = 95
Exp. Name: Write a C program to Sort Date: 2024-12-
S.No: 5

Page No: 16
given elements using Merge sort 15

Aim:
Write a program to sort ( Ascending order ) the given elements using merge sort

ID: 2300331530093
technique.

At the time of execution, the program should print the message on the console as:

Enter array size :

For example, if the user gives the input as:

Enter array size : 5

Next, the program should print the following message on the console as:

2023-27-CSE-AIML-B
Enter 5 elements :

if the user gives the input as:

Enter 5 elements : 34 67 12 45 22

then the program should print the result as:

Raj Kumar Goel Institute Of Technology


Before sorting the elements are : 34 67 12 45 22
After sorting the elements are : 12 22 34 45 67

Note: Do use the printf() function with a newline character ( \n ).


Source Code:

MergeSortMain.c
#include <stdio.h>

Page No: 17
#include "MergeSortFunctions.c"
void main() {
int arr[15], i, n;
printf("Enter array size : ");
scanf("%d", &n);

ID: 2300331530093
printf("Enter %d elements : ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Before sorting the elements are : ");
display(arr, n);
splitAndMerge(arr, 0, n - 1);
printf("After sorting the elements are : ");
display(arr, n);
}

2023-27-CSE-AIML-B
MergeSortFunctions.c

Raj Kumar Goel Institute Of Technology


void display(int arr[15], int n) {

Page No: 18
int i;
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");

ID: 2300331530093
}
void merge(int arr[15], int low, int mid, int high) {
int i,j,k;
int n1=mid-low+1;
int n2=high-mid;
int L[n1],R[n2];
for(i=0;i<n1;i++){
L[i]=arr[low+i];
}
for(j=0;j<n2;j++){
R[j]=arr[mid+1+j];

2023-27-CSE-AIML-B
}
i=0;
j=0;
k=low;
while(i<n1 && j<n2){
if(L[i]<+R[j]){
arr[k]=L[i];
i++;
}

Raj Kumar Goel Institute Of Technology


else{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1){
arr[k]=L[i];
i++;
k++;
}
while(j<n2){
arr[k]=R[j];
j++;
k++;
}
}
void splitAndMerge(int arr[15], int low, int high) {
if(low<high){
int mid=low+(high-low)/2;
splitAndMerge(arr,low,mid);

Page No: 19
splitAndMerge(arr,mid+1,high);
merge(arr,low,mid,high);
}
}

ID: 2300331530093
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter array size :
5
Enter 5 elements :

2023-27-CSE-AIML-B
34 67 12 45 22
Before sorting the elements are : 34 67 12 45 22
After sorting the elements are : 12 22 34 45 67

Test Case - 2

User Output

Raj Kumar Goel Institute Of Technology


Enter array size :
8
Enter 8 elements :
77 55 22 44 99 33 11 66
Before sorting the elements are : 77 55 22 44 99 33 11 66
After sorting the elements are : 11 22 33 44 55 66 77 99

Test Case - 3

User Output
Enter array size :
5
Enter 5 elements :
-32 -45 -67 -46 -14
Before sorting the elements are : -32 -45 -67 -46 -14
After sorting the elements are : -67 -46 -45 -32 -14
Exp. Name: C program to Sort given Date: 2024-12-
S.No: 6

Page No: 20
elements using Quick sort 15

Aim:
Write a C program to sort (Ascending order) the given elements using quick sort

ID: 2300331530093
technique.

Note:
• Pick the first element as pivot. You will not be awarded marks if you do not follow
this instruction.
• Do use the printf() function with a newline character (\n).
Source Code:

QuickSortMain.c
#include <stdio.h>

2023-27-CSE-AIML-B
#include "QuickSortFunctions.c"
void main() {
int arr[15], i, n;
printf("Enter array size : ");
scanf("%d", &n);
printf("Enter %d elements : ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

Raj Kumar Goel Institute Of Technology


printf("Before sorting the elements are : ");
display(arr, n);
quickSort(arr, 0, n - 1);
printf("After sorting the elements are : ");
display(arr, n);
}

QuickSortFunctions.c
void display(int arr[15], int n) {

Page No: 21
//write your code here..
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");

ID: 2300331530093
}
int partition(int arr[15], int lb, int ub) {
//write your code here..
int pivot=arr[ub];
int i=lb-1;
for(int j=lb;j<ub;j++){
if(arr[j]<=pivot){
i++;
int temp=arr[i];
arr[i]=arr[j];

2023-27-CSE-AIML-B
arr[j]=temp;
}
}
int temp=arr[i+1];
arr[i+1]=arr[ub];
arr[ub]=temp;
return i+1;
}
void quickSort(int arr[15], int low, int high) {

Raj Kumar Goel Institute Of Technology


//write your code here..
if(low<high){
int pi=partition(arr,low,high);
quickSort(arr,low,pi-1);
quickSort(arr,pi+1,high);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter array size :
5
Enter 5 elements :
34 67 12 45 22
Before sorting the elements are : 34 67 12 45 22
After sorting the elements are : 12 22 34 45 67

Page No: 22
Test Case - 2

User Output

ID: 2300331530093
Enter array size :
8
Enter 8 elements :
77 55 22 44 99 33 11 66
Before sorting the elements are : 77 55 22 44 99 33 11 66
After sorting the elements are : 11 22 33 44 55 66 77 99

Test Case - 3

2023-27-CSE-AIML-B
User Output
Enter array size :
5
Enter 5 elements :
-32 -45 -67 -46 -14
Before sorting the elements are : -32 -45 -67 -46 -14
After sorting the elements are : -67 -46 -45 -32 -14

Raj Kumar Goel Institute Of Technology


Exp. Name: Write a C program to Search Date: 2024-12-
S.No: 7

Page No: 23
an element using Linear Search process 15

Aim:
Write a program to search a key element within the given array of elements using

ID: 2300331530093
Linear search process.
Source Code:

linearSearch.c

#include<stdio.h>
int linear_search(int arr[],int n,int key){
for(int i=0;i<n;i++){
if(arr[i]==key)
return i;
}

2023-27-CSE-AIML-B
return -1;
}
int main(){
int n,key;
printf("n = ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){

Raj Kumar Goel Institute Of Technology


printf("a[%d] = ",i);
scanf("%d",&arr[i]);
}
printf("Search key : ");
scanf("%d",&key);
int index=linear_search(arr,n,key);
if(index==-1){
printf("Key %d is not found.\n",key);
}
else{
printf("Key %d is found at position
%d.\n",key,index);
}
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 24
User Output
n =
4
a[0] =

ID: 2300331530093
153
a[1] =
264
a[2] =
357
a[3] =
598
Search key :
100

2023-27-CSE-AIML-B
Key 100 is not found.

Test Case - 2

User Output
n =
5

Raj Kumar Goel Institute Of Technology


a[0] =
-15
a[1] =
-24
a[2] =
-36
a[3] =
-11
a[4] =
-20
Search key :
-11
Key -11 is found at position 3.

Test Case - 3

User Output
n =
5

Page No: 25
a[0] =
24
a[1] =
36

ID: 2300331530093
a[2] =
11
a[3] =
45
a[4] =
28
Search key :
11
Key 11 is found at position 2.

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a C program to Search Date: 2024-12-
S.No: 8

Page No: 26
an element using Binary Search process 20

Aim:
Write a program to search a key element in the given array of elements using

ID: 2300331530093
Binary search .
Source Code:

binarySearch.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

Page No: 27
void main(){
int i,j,low,mid,n,key,a[100],swap,high;
printf("n = ");
scanf("%d",&n);
for( i=0; i<n; i++){

ID: 2300331530093
printf("a[%d] = ",i);
scanf("%d" ,&a[i]);
}
printf("Search key = ");
scanf("%d",&key);
for( i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;

2023-27-CSE-AIML-B
}
}
}
printf("After sorting :\n");
for(i=0;i<n;i++){
printf("a[%d] = %d\n",i,a[i]);
}
low=0;

Raj Kumar Goel Institute Of Technology


high=n-1;
mid=(low+high)/2;
while(low<=high){
if(a[mid]<key)
low=mid+1;
else if(a[mid]==key){
printf("Key %d is found at position %d." ,
key,mid);
break;
}
else
high=mid-1;
mid=(low+high)/2;
}
if(low>high){
printf("Key %d is not found in the array.",key);
}
printf("\n");
}
Execution Results - All test cases have succeeded!

Page No: 28
Test Case - 1

User Output
n =

ID: 2300331530093
5
a[0] =
15
a[1] =
29
a[2] =
67
a[3] =
10

2023-27-CSE-AIML-B
a[4] =
23
Search key =
10
After sorting :
a[0] = 10
a[1] = 15
a[2] = 23

Raj Kumar Goel Institute Of Technology


a[3] = 29
a[4] = 67
Key 10 is found at position 0.

Test Case - 2

User Output
n =
4
a[0] =
-24
a[1] =
-36
a[2] =
-10
a[3] =
-87
Search key =
-10

Page No: 29
After sorting :
a[0] = -87
a[1] = -36
a[2] = -24

ID: 2300331530093
a[3] = -10
Key -10 is found at position 3.

Test Case - 3

User Output
n =
5
a[0] =

2023-27-CSE-AIML-B
2
a[1] =
3
a[2] =
4
a[3] =
1

Raj Kumar Goel Institute Of Technology


a[4] =
5
Search key =
9
After sorting :
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
Key 9 is not found in the array.
Exp. Name: Operations on Stack using Date: 2024-12-
S.No: 9

Page No: 30
Arrays 15

Aim:
Write a C program to implement stack operations using arrays. Write the code in

ID: 2300331530093
StackOperations.c file.

Input Format:
The user can enter commands to perform operations on the stack. The commands can be
as follows:
• push <element>: Push an integer onto the stack.
• pop: Pop the top element from the stack.
• peek: Display the top element of the stack without removing it.
• isEmpty: Check if the stack is empty.
• display: Display all elements of the stack.
• exit: Exit the program.

2023-27-CSE-AIML-B
Each operation should be handled one at a time.

Output Format:
For each operation, the corresponding output will be printed.

For the push <element> operation:


• If the stack is full, print:

Raj Kumar Goel Institute Of Technology


Stack is overflow.
• Otherwise, print:
Successfully pushed.

For the pop operation:


• If the stack is empty, print:
Stack is underflow.
• Otherwise, print:
Popped value = <value>

For the peek operation:


• If the stack is empty, print:
Stack is underflow.
• Otherwise, print:
Peek value = <value>

For the isEmpty operation:


• If the stack is empty, print:
Stack is empty.
• Otherwise, print:
Stack is not empty.
For the display operation:

Page No: 31
If the stack is empty, print:
Stack is empty.
Otherwise, print the elements from top to bottom:
Elements of the stack are: <value1> <value2> ... <valueN>
Source Code:

ID: 2300331530093
StackUsingArray.c

#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10
#include "StackOperations.c"

int main() {
int op, x;
while(1) {

2023-27-CSE-AIML-B
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek
6.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);

Raj Kumar Goel Institute Of Technology


push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}
StackOperations.c

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 32
//declare the size of the array

Page No: 33
#include<stdio.h>
#define STACK_MAX_SIZE 10
int stack[STACK_MAX_SIZE];
//define the top to -1
int top = -1;

ID: 2300331530093
void push(int element) {
//write your code here to push an element
if(top == STACK_MAX_SIZE -1){
printf("Stack is overflow.\n");
}else{
stack[++top] = element;
printf("Successfully pushed.\n");
}
}

void display() {

2023-27-CSE-AIML-B
//write your code here to display the stack
if (top == -1){
printf("Stack is empty.\n");
} else {
printf("Elements of the stack are : ");
for (int i = top; i>=0; i--){
printf("%d ", stack[i]);
}
printf("\n");}}

Raj Kumar Goel Institute Of Technology


void pop() {
//write your code here to pop an element
if (top == -1){
printf("Stack is underflow.\n");
}else{
printf("Popped value = %d\n", stack[top--]);
}}

int peek(){
//write your code here to find the peek element
if (top == -1){
printf("Stack is underflow.\n");
return -1;
}else{
printf("Peek value = %d\n",
stack[top]);
return stack[top];
}
}
Page No: 34
int isEmpty() {
//write your code here to check whether the stack is empty not
if (top == -1){
printf("Stack is empty.\n");
return 1;

ID: 2300331530093
}else{
printf("Stack is not empty.\n");
return 0;
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

2023-27-CSE-AIML-B
User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
10

Raj Kumar Goel Institute Of Technology


Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
20
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
30
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 30 20 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5

Page No: 35
Peek value = 30
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2

ID: 2300331530093
Popped value = 30
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 20
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 10

2023-27-CSE-AIML-B
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Peek value = 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is not empty.

Raj Kumar Goel Institute Of Technology


1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Test Case - 2
User Output

Page No: 36
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
1

ID: 2300331530093
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
2
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

2023-27-CSE-AIML-B
1
Enter element :
3
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1

Raj Kumar Goel Institute Of Technology


Enter element :
4
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
5
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
6
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
7

Page No: 37
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1

ID: 2300331530093
Enter element :
8
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
9
Successfully pushed.

2023-27-CSE-AIML-B
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
10
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


1
Enter element :
11
Stack is overflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Test Case - 3

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Stack is underflow.

Page No: 38
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Stack is underflow.

ID: 2300331530093
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: Operations on Queue using Date: 2024-12-
S.No: 10

Page No: 39
Arrays 21

Aim:
Write a C program to implement queue using arrays.

ID: 2300331530093
Input Format
The program will repeatedly prompt the user with a menu to perform operations on the
queue.
1. Enqueue: The user will be prompted to enter an integer value to be added to the
queue.
2. Dequeue: No additional input is needed.
3. Display: No additional input is needed.
4. Is Empty: No additional input is needed.
5. Size: No additional input is needed.
6. Exit: No additional input is needed.

2023-27-CSE-AIML-B
Output Format
7. Enqueue:
iv. If the queue is not full, the output will be: Successfully inserted.
iv. If the queue is full, the output will be: Queue is overflow.
10. Dequeue:
iv. If the queue is not empty, the output will be: Deleted element = X where X
is the element removed from the queue.

Raj Kumar Goel Institute Of Technology


iv. If the queue is empty, the output will be: Queue is underflow.
13. Display:
iv. If the queue is empty, the output will be: Queue is empty.
iv. Otherwise, the output will list the elements in the queue in the order from
front to rear, e.g., Elements in the queue: 10 20
16. Is Empty:
iv. If the queue is empty, the output will be: Queue is empty.
iv. Otherwise, the output will be: Queue is not empty.
19. Size:
iv. If the queue is empty, the output will be: Queue size: 0
iv. Otherwise, the output will display the current number of elements, e.g.,
Queue size: 3
22. Exit:
iv. The program terminates without any additional output.
Source Code:

QueueUsingArray.c
#include <stdlib.h>

Page No: 40
#include <stdio.h>
#include "QueueOperations.c"
int main() {
int op, x;
while(1) {

ID: 2300331530093
printf("1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size
6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:

2023-27-CSE-AIML-B
dequeue();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:

Raj Kumar Goel Institute Of Technology


size();
break;
case 6: exit(0);
}
}
}

QueueOperations.c
//Write a c program toimplement different operations on queue using

Page No: 41
array representation

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100

ID: 2300331530093
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
//Function to check if the queue is empty
void isEmpty(){
if ((front == -1 && rear == -1) || front>rear){
printf("Queue is empty.\n");
} else {
printf("Queue is not empty.\n");
}
}

2023-27-CSE-AIML-B
//Function to check the size of the queue
void size() {
if(front == -1 && rear == -1){
printf("Queue size : 0\n");
} else {
printf("Queue size : %d\n",rear - front
+ 1);
}
}

Raj Kumar Goel Institute Of Technology


//Function to enqueue an element into the queue
void enqueue(int element){
if (rear == MAX_SIZE -1) {
printf("Queue is overflow.\n");
} else {
if (front == -1) {
front = 0;
}
queue[++rear]=element;
printf("Successfully inserted.\n");}
}
//Function to dequeue an element from the queue
void dequeue(){
if (front == -1 || front > rear) {
printf("Queue is underflow.\n");
} else {
printf("Deleted element = %d\n",
queue[front++]);
}
}
//Function to display the element in the queue

Page No: 42
void display() {
if (front == -1 || front >rear) {
printf("Queue is empty.\n");
} else {
printf("Elements in the queue : ");

ID: 2300331530093
for (int i = front; i<=rear; i++) {
printf("%d ",
queue[i]);
}
printf("\n");
}}

Execution Results - All test cases have succeeded!

2023-27-CSE-AIML-B
Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Queue is underflow.

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
14
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Page No: 43
1
Enter element :
78
Successfully inserted.

ID: 2300331530093
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
53
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3

2023-27-CSE-AIML-B
Elements in the queue : 14 78 53
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6

Raj Kumar Goel Institute Of Technology


Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
25
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 25
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

Page No: 44
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ID: 2300331530093
Enter your option :
1
Enter element :
65
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 65

2023-27-CSE-AIML-B
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 65

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
63
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 1
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

Page No: 45
Enter your option :
6

ID: 2300331530093
2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: Implementation of Circular Date: 2024-12-
S.No: 11

Page No: 46
Queue using Arrays 21

Aim:
Write a C program to implement circular queue using arrays.

ID: 2300331530093
Note: Define the MAX value as 5.
Source Code:

CQueueUsingArray.c

#include <stdio.h>
#include <stdlib.h>
#include "CQueueOperations.c"

int main() {

2023-27-CSE-AIML-B
int op, x;
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size
6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");

Raj Kumar Goel Institute Of Technology


scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}
}
CQueueOperations.c

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 47
//Implementation of circular queue using arrays

Page No: 48
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 //Maximumsize of the circular queue
int front = -1, rear = -1;

ID: 2300331530093
int queue[MAX];
//Function to check if the circular queueu is empty
void isEmpty() {
if (front == -1 && rear == -1) {
printf("Circular queue is empty.\n");
}else{
printf("Circular queue is not empty.\n");
}
}
//Function tp check if the circular queue is full
int isFull() {

2023-27-CSE-AIML-B
if((front == 0 && rear == MAX - 1) || (front == rear + 1)){
return 1;
}
return 0;
}
//Function to enqueue an element into the circular queue
void enqueue(int x) {
if (isFull()) {
printf("Circular queue is overflow.\n");

Raj Kumar Goel Institute Of Technology


} else {
if (front == -1) {
front = 0;
}
rear = (rear + 1) % MAX;
queue[rear] = x;
printf("Successfully inserted.\n");
}
}
//Function to dequeue an element from the circular queue
void dequeue() {
if (front == -1 && rear ==-1) {
printf("Circular queue is underflow.\n");
} else {
printf("Deleted element = %d\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}

Page No: 49
}
//Function to display element in the circular queue
void display() {
int i;
if (front == -1 && rear == -1) {

ID: 2300331530093
printf("Circular queue is empty.\n");
} else {
printf("Elements in the circular queue : ");
i = front;
while(1) {
printf("%d ",
queue[i]);
if (i == rear) {
break;
}
i = (i + 1) % MAX;

2023-27-CSE-AIML-B
}
printf("\n");
}
}
//Function to get the size of circular queue
void size() {
int size; {
if (front ==-1 && rear == -1) {
size = 0;

Raj Kumar Goel Institute Of Technology


}else if(front <= rear){
size = rear - front + 1;
} else {
size = MAX - front + rear + 1;
}
printf("Circular queue size :
%d\n",size);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Circular queue is underflow.

Page No: 50
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Circular queue is empty.

ID: 2300331530093
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
12

2023-27-CSE-AIML-B
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
34
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option :
1
Enter element :
56
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 12 34 56
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
38
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :

Page No: 51
25
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

ID: 2300331530093
3
Elements in the circular queue : 12 34 56 38 25
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
56
Circular queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

2023-27-CSE-AIML-B
Enter your option :
2
Deleted element = 12
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 34
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option :
3
Elements in the circular queue : 56 38 25
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
11
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

Page No: 52
3
Elements in the circular queue : 56 38 25 11
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

ID: 2300331530093
6

Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 0

2023-27-CSE-AIML-B
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
34
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option :
1
Enter element :
55
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
26
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
77
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1

Page No: 53
Enter element :
38
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2300331530093
Enter your option :
1
Enter element :
59
Circular queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 34 55 26 77 38

2023-27-CSE-AIML-B
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 5
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 34

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 55
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 26
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 77 38
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5

Page No: 54
Circular queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6

ID: 2300331530093
2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: C program to implement
Date: 2024-12-

Page No: 55
S.No: 12 different Operations on Stack using
29
Linked Lists

Aim:

ID: 2300331530093
Write a program to implement a stack using linked lists.

Input Format
The user is presented with a menu of options and provides input according to the desired
operation:
24. Push Operation:
nt. Input: Integer value to be pushed onto the stack.
26. Pop Operation:
nt. No additional input is required.
28. Display Operation:
nt. No additional input is required.

2023-27-CSE-AIML-B
30. Is Empty Operation:
nt. No additional input is required.
32. Peek Operation:
nt. No additional input is required.
34. Exit Operation:
35. No additional input is required.

Output Format

Raj Kumar Goel Institute Of Technology


The output will vary depending on the selected option:
36. Push Operation:
nt. If the stack is not full (no overflow), the output will be: Successfully
pushed.If memory allocation fails, it will print: Stack is overflow.
38. Pop Operation:
nt. If the stack is not empty, it will print: Popped value = X where X is the value
removed from the stack.If the stack is empty, it will print: Stack is
underflow.
40. Display Operation:
nt. If the stack is not empty, it will print: Elements of the stack are : X Y Z ...
where X, Y, Z, etc., are the elements from top to bottom.If the stack is
empty, it will print: Stack is empty.
42. Is Empty Operation:
nt. If the stack is empty, it will print: Stack is empty.If the stack is not empty, it
will print: Stack is not empty.
44. Peek Operation:
nt. If the stack is not empty, it will print: Peek value = X where X is the top
element of the stack.If the stack is empty, it will print: Stack is underflow.
46. Exit Operation:
47. The program terminates with no additional output.
Note:

Page No: 56
The partial code has been provided to you in the editor, you are required to fill in the
missing code.
Source Code:

StackUsingLL.c

ID: 2300331530093
#include <stdio.h>
#include <stdlib.h>
#include "StackOperationsLL.c"

int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek
6.Exit\n");
printf("Enter your option : ");

2023-27-CSE-AIML-B
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:

Raj Kumar Goel Institute Of Technology


pop();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}

StackOperationsLL.c
struct stack {

Page No: 57
int data;
struct stack *next;
};

typedef struct stack *stk;

ID: 2300331530093
stk top = NULL;
void push(int x) {
struct stack* newNode = (struct stack*)malloc(sizeof(struct stack));
if (newNode == NULL) {
printf("Stack overflow.\n");
return;
}
newNode->data = x;
newNode->next = top;
top = newNode;
printf("Successfully pushed.\n");

2023-27-CSE-AIML-B
}
// Function to pop an element from the stack
void pop() {
if (top == NULL) {
printf("Stack is underflow.\n");
return;
}
struct stack*temp = top;
top = top->next;

Raj Kumar Goel Institute Of Technology


int poppedValue = temp->data;
free(temp);
printf("Popped value = %d\n", poppedValue);
}
void peek() {
if (top == NULL) {
printf("Stack is underflow.\n");
}
else {
printf("Peek value = %d\n", top->data);
}
}

void isEmpty() {
if (top == NULL) {
printf("Stack is empty.\n");}
else{
printf("Stack is not empty.\n");
}
}
void display() {

Page No: 58
stk temp = top;
if(temp == NULL) {
printf("Stack is empty.\n");
} else {
printf("Elements of the stack are : ");

ID: 2300331530093
while(temp != NULL) {
printf("%d ", temp -> data);
temp = temp -> next;
}
printf("\n");
}
}

2023-27-CSE-AIML-B
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1

Raj Kumar Goel Institute Of Technology


Enter element :
33
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
22
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
55
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :

Page No: 59
66
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

ID: 2300331530093
3
Elements of the stack are : 66 55 22 33
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 66
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2

2023-27-CSE-AIML-B
Popped value = 55
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 22 33
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5

Raj Kumar Goel Institute Of Technology


Peek value = 22
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is not empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Test Case - 2

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3

Page No: 60
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5

ID: 2300331530093
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
23

2023-27-CSE-AIML-B
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
24
Successfully pushed.

Raj Kumar Goel Institute Of Technology


1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 24 23
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Peek value = 24
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 24
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 23
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2

Page No: 61
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4

ID: 2300331530093
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: C program to implement
Date: 2024-12-

Page No: 62
S.No: 13 different Operations on Queue using
28
Linked Lists

Aim:

ID: 2300331530093
Write a program to implement a queue using linked lists. Implement the queue
operations as separate functions:
• enqueue(int element): Adds an element to the queue.
• dequeue(): Removes and displays the element at the front of the queue.
• display(): Prints all the elements in the queue.
• isEmpty(): Displays whether the queue is empty or not.
• size(): Displays the number of elements currently in the queue.

Note:
The partial code has been provided to you in the editor, you are required to fill in the

2023-27-CSE-AIML-B
missing code in the function bodies.

Source Code:

QueueUsingLL.c

Raj Kumar Goel Institute Of Technology


#include <stdlib.h>

Page No: 63
#include <stdio.h>
#include "QueueOperationsLL.c"
int main() {
int op, x;
while(1) {

ID: 2300331530093
printf("1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size
6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:

2023-27-CSE-AIML-B
dequeue();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:

Raj Kumar Goel Institute Of Technology


size();
break;
case 6: exit(0);
}
}
}

QueueOperationsLL.c
struct queue {

Page No: 64
int data;
struct queue *next;
};

typedef struct queue *Q;

ID: 2300331530093
Q front = NULL, rear = NULL;

void enqueue(int element) {


struct queue* newNode = (struct queue*)malloc(sizeof(struct queue));
if (newNode == NULL) {
printf("Queue is full. Unable to enqueue.\n");
return;
}
newNode->data = element;
newNode->next = NULL;
if (front == NULL) {

2023-27-CSE-AIML-B
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Successfully inserted.\n");

Raj Kumar Goel Institute Of Technology


void dequeue() {
if (front == NULL) {
printf("Queue is underflow.\n");
return;
}
struct queue* temp = front;
front = front->next;
int deletedValue = temp->data;
free(temp);
printf("Deleted value = %d\n",deletedValue);

void size() {

int count = 0;
struct queue* current = front;
while (current != NULL) {
count++;
current = current->next;

Page No: 65
}
printf("Queue size : %d\n",count);
}

void isEmpty() {

ID: 2300331530093
if (front == NULL) {
printf("Queue is empty.\n");
} else {
printf("Queue is not empty.\n");
}

void display() {
if(front == NULL) {

2023-27-CSE-AIML-B
printf("Queue is empty.\n");
} else {
Q temp = front;
printf("Elements in the queue : ");
while(temp != NULL) {
printf("%d ", temp -> data);
temp = temp -> next;
}
printf("\n");

Raj Kumar Goel Institute Of Technology


}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

Page No: 66
Enter your option :
4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ID: 2300331530093
Enter your option :
5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
44
Successfully inserted.

2023-27-CSE-AIML-B
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
55
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


1
Enter element :
66
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
67
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 44 55 66 67
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 44
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

Page No: 67
Enter your option :
2
Deleted value = 55
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ID: 2300331530093
Enter your option :
5
Queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6

2023-27-CSE-AIML-B
Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


1
Enter element :
23
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
234
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
45
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :

Page No: 68
456
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

ID: 2300331530093
2
Deleted value = 23
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 234 45 456
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2

2023-27-CSE-AIML-B
Deleted value = 234
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 45 456
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4

Raj Kumar Goel Institute Of Technology


Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6
Exp. Name: Implementation of Circular Date: 2024-12-
S.No: 14

Page No: 69
Queue using Linked List 28

Aim:
Write a program to implement circular queue using linked lists.

ID: 2300331530093
Source Code:

CQueueLL.c
#include <stdlib.h>
#include <stdio.h>
#include "CQueueOperationsLL.c"
int main() {
int op, x;
while(1) {

2023-27-CSE-AIML-B
printf("1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size
6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);

Raj Kumar Goel Institute Of Technology


enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}
}
CQueueOperationsLL.c

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 70
struct queue {

Page No: 71
int data;
struct queue *next;
};
typedef struct queue *CircularQueue;
CircularQueue front = NULL, rear = NULL;

ID: 2300331530093
//complete the below dequeue() and enqueue() functions
void dequeue() {
if(front == NULL){
printf("Circular queue is underflow.\n");
return;
}
struct queue* temp = front;
if(front == rear){
front = rear = NULL;
}else{
front = front->next;

2023-27-CSE-AIML-B
rear->next = front;
}
int deletedValue = temp->data;
free(temp);
printf("Deleted value = %d\n", deletedValue);
}

void size() {
int count =0;

Raj Kumar Goel Institute Of Technology


if(front == NULL) {
printf("Circular queue size : 0\n");
return;
}
CircularQueue temp = front;
do {
temp = temp -> next;
count = count + 1;
} while(temp != front);
printf("Circular queue size : %d\n",count);
}

void isEmpty() {
if(front == NULL ) {
printf("Circular queue is empty.\n");
} else {
printf("Circular queue is not empty.\n");
}
}
void enqueue(int element) {

Page No: 72
struct queue* newnode = (struct queue*)malloc(sizeof(struct
queue));
if (newnode == NULL)
{
printf("Circular queue is underflow.\n");

ID: 2300331530093
return;
}
newnode->data = element;
newnode->next = NULL;
if(rear == NULL){
front = rear = newnode;
rear->next = front;
}
else
{
rear->next = newnode;

2023-27-CSE-AIML-B
rear = newnode;
rear->next = front;
}
printf("Successfully inserted.\n");
}

void display() {
if(front == NULL) {
printf("Circular queue is empty.\n");

Raj Kumar Goel Institute Of Technology


} else {
CircularQueue temp = front;
printf("Elements in the circular queue : ");
do {
printf("%d ", temp -> data);
temp = temp -> next;
} while(temp != front);
printf("\n");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1

Page No: 73
Enter element :
15
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2300331530093
Enter your option :
1
Enter element :
16
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :

2023-27-CSE-AIML-B
17
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 15 16 17
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


5
Circular queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 15
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 16
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 17
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Page No: 74
Enter your option :
4
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2300331530093
Enter your option :
5
Circular queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6

Test Case - 2

2023-27-CSE-AIML-B
User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Circular queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


5
Circular queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
143
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :

Page No: 75
153
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

ID: 2300331530093
1
Enter element :
163
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
173

2023-27-CSE-AIML-B
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 143 153 163 173
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2

Raj Kumar Goel Institute Of Technology


Deleted value = 143
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 153
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6
Exp. Name: To implement binary tree Date: 2024-12-
S.No: 15

Page No: 76
using Arrays. 28

Aim:
Write a C program to implement Binary tree using Array.

ID: 2300331530093
Note : Driver code is already provided for you to run the test cases.
Source Code:

TreeMain.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include <math.h>

Page No: 77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void updateElement(char *tree, int arraySize, char s, char t) {

ID: 2300331530093
for (int i = 0; i < arraySize; i++) {
if (tree[i] == s) {
tree[i] = t;
}
}
}
int setRoot(int dataKey, char *tree) {
//write your code here.
if(tree[0]!='\0'){
printf("Root is already available\n");
}

2023-27-CSE-AIML-B
else{
tree[0]=dataKey;
}
return 0;
}

int addLeft(char dataKey, int parentIndex, char *tree) {


//write your code here.
if(tree[parentIndex]=='\0')

Raj Kumar Goel Institute Of Technology


printf("No parent found\n");
else
tree[(parentIndex*2)+1]=dataKey;
return 0;
}

int addRight(char dataKey, int parentIndex, char *tree) {


//write your code here.
if(tree[parentIndex]=='\0')
printf("No parent found\n");
else
tree[(parentIndex*2)+2]=dataKey;
return 0;
}

int displayTree(char *tree, int arraySize) {


//write your code here to display the tree.
for(int i=0;i<arraySize;i++){
if(tree[i]!='\0')
printf("%c ",tree[i]);}
return 0;

Page No: 78
}

int deleteNode(char *tree, char dataKey, int arraySize) {


//write your code here to delete nodeand replace it with rightmost node
in our constructed binary tree.

ID: 2300331530093
int k,l;
char lastNode;
for(int i=0;i<arraySize;i++){
if(tree[i]==dataKey)
k=i;
if(tree[i]!='\0')
l=i;
}tree[k]=tree[l];
tree[l]='\0';
return 0;
}

2023-27-CSE-AIML-B
// Driver Code
int main() {
char *tree;
char ch, choice, delNode;
printf("Enter the number of nodes: ");
int n, index;
scanf("%d", &n);
int height = round(log2(n) + 1);
printf("height: %d\n", height);

Raj Kumar Goel Institute Of Technology


int arraySize = pow(2, height) - 1;
tree = (char *)malloc(arraySize * sizeof(char));
memset(tree, '\0', sizeof(char *) * arraySize);
printf("Enter the data key in root node: ");
scanf(" %c", &ch);
setRoot(ch, tree);
if (n > 1) {
printf("Enter rest of the nodes in the binary tree: \n");
for (int i = 1; i < n; i++) {
printf("Enter R or r for right and L or l for left side
insertion: ");
scanf(" %c", &choice);
if (choice == 'R' || choice == 'r') {
printf("Enter datakey and parent index: ");
scanf(" %c", &ch);
scanf("%d", &index);
addRight(ch, index, tree);
} else {
printf("Enter datakey and parent index: ");
scanf(" %c", &ch);
scanf("%d", &index);

Page No: 79
addLeft(ch, index, tree);
}
}
}
printf("Binary tree is:\n");

ID: 2300331530093
displayTree(tree, arraySize);
printf("\nEnter the data element you want to update: ");
char s, t;
scanf(" %c", &s);
scanf(" %c", &t);
updateElement(tree, arraySize, s, t);
printf("Binary tree is:\n");
displayTree(tree, arraySize);

printf("\nEnter DeleteNode: ");


scanf(" %c", &delNode);

2023-27-CSE-AIML-B
deleteNode(tree, delNode, arraySize);
printf("Tree after deletion\n");
displayTree(tree, arraySize);
return 0;
}

Execution Results - All test cases have succeeded!

Raj Kumar Goel Institute Of Technology


Test Case - 1

User Output
Enter the number of nodes:
5
height: 3
Enter the data key in root node:
A
Enter rest of the nodes in the binary tree:
Enter R or r for right and L or l for left side insertion:
r
Enter datakey and parent index:
B0
Enter R or r for right and L or l for left side insertion:
l
Enter datakey and parent index:
C0
Enter R or r for right and L or l for left side insertion:

Page No: 80
r
Enter datakey and parent index:
D1
Enter R or r for right and L or l for left side insertion:

ID: 2300331530093
l
Enter datakey and parent index:
E2
Binary tree is:
A C B D E
Enter the data element you want to update:
BZ
Binary tree is:
A C Z D E

2023-27-CSE-AIML-B
Enter DeleteNode:
C
Tree after deletion
A E Z D

Test Case - 2

Raj Kumar Goel Institute Of Technology


User Output
Enter the number of nodes:
6
height: 4
Enter the data key in root node:
a
Enter rest of the nodes in the binary tree:
Enter R or r for right and L or l for left side insertion:
r
Enter datakey and parent index:
b0
Enter R or r for right and L or l for left side insertion:
l
Enter datakey and parent index:
C0
Enter R or r for right and L or l for left side insertion:
r
Enter datakey and parent index:
e2

Page No: 81
Enter R or r for right and L or l for left side insertion:
r
Enter datakey and parent index:
f3

ID: 2300331530093
No parent found
Enter R or r for right and L or l for left side insertion:
r
Enter datakey and parent index:
f2
Binary tree is:
a C b f
Enter the data element you want to update:
C

2023-27-CSE-AIML-B
c
Binary tree is:
a c b f
Enter DeleteNode:
a
Tree after deletion
f c b

Raj Kumar Goel Institute Of Technology


Exp. Name: Implementation of binary Date: 2024-12-
S.No: 16

Page No: 82
tree using linked list. 21

Aim:
Write a C program to implement Binary tree using Linked list.

ID: 2300331530093
Driver code is already provided for you to run the test cases.
Source Code:

TreeMain.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include <stdio.h>

Page No: 83
#include <stdlib.h>
#include"TreeStructure.c"
// For Queue Size
#define SIZE 50
// A utility function to create a new Queue

ID: 2300331530093
struct Queue* createQueue(int size) {
struct Queue* queue = (struct Queue*) malloc(sizeof( struct
Queue ));
queue->front = queue->rear = -1;
queue->size = size;
queue->array = (struct node**) malloc(queue->size * sizeof(
struct node* ));
int i;
for (i = 0; i < size; ++i)
queue->array[i] = NULL;
return queue;

2023-27-CSE-AIML-B
}
// Standard Queue Functions
int isEmpty(struct Queue* queue) {
return queue->front == -1;
}
int isFull(struct Queue* queue) {
return queue->rear == queue->size - 1;
}
int hasOnlyOneItem(struct Queue* queue) {

Raj Kumar Goel Institute Of Technology


return queue->front == queue->rear;
}
void Enqueue(struct node *root, struct Queue* queue) {
if (isFull(queue))
return;
queue->array[++queue->rear] = root;
if (isEmpty(queue))
++queue->front;
}
struct node* Dequeue(struct Queue* queue) {
if (isEmpty(queue))
return NULL;
struct node* temp = queue->array[queue->front];
if (hasOnlyOneItem(queue))
queue->front = queue->rear = -1;
else
++queue->front;
return temp;
}
struct node* getFront(struct Queue* queue) {
return queue->array[queue->front];

Page No: 84
}
int hasBothChild(struct node* temp) {
return temp && temp->left && temp->right;
}
void insert(struct node **root, int data, struct Queue* queue) {

ID: 2300331530093
// Write your code here to insert a new node in complete binary tree
struct node* newNode = (struct node*)malloc(sizeof(struct
node));
newNode->data=data;
newNode->left=newNode->right=NULL;
if(*root==NULL){
*root = newNode;
}else{
struct node*front=getFront(queue);
if(front->left==NULL){
front->left = newNode;

2023-27-CSE-AIML-B
}else if(front->right==NULL){
front->right=newNode;
}
if(front->left&&front->right){
Dequeue(queue);
}
}
Enqueue(newNode,queue);
}

Raj Kumar Goel Institute Of Technology


void removethedeepestnode(struct node *root, struct node *d_node){
struct Queue* queue = createQueue(SIZE);
// Write your code here to remove the deepest node in a binary tree
Enqueue(root,queue);
struct node*temp;
while(!isEmpty(queue)){
temp = Dequeue(queue);
if(temp==d_node){
temp=NULL;
free(d_node);
return;
}
if(temp->right){
if(temp->right==d_node){
temp->right=NULL;
free(d_node);
return;
} else {
Enqueue(temp->right,queue);
}
}

Page No: 85
if(temp->left==d_node){
temp->left=NULL;
free(d_node);
return;
} else {

ID: 2300331530093
Enqueue(temp->left,queue);}
}

}
void levelOrder(struct node* root) {
struct Queue* queue = createQueue(SIZE);
Enqueue(root, queue);
while (!isEmpty(queue)) {
struct node* temp = Dequeue(queue);
printf("%d ", temp->data);
if (temp->left)

2023-27-CSE-AIML-B
Enqueue(temp->left, queue);
if (temp->right)
Enqueue(temp->right, queue);
}
}

void findDeepestnode(struct node *root, struct node *search){


struct Queue* queue = createQueue(SIZE);
Enqueue(root, queue);

Raj Kumar Goel Institute Of Technology


struct node *temp, *par;
while (!isEmpty(queue))
{ par = temp;
temp = Dequeue(queue);
if (temp->left)
Enqueue(temp->left, queue);
if (temp->right)
Enqueue(temp->right, queue);
}
search->data = temp->data;
removethedeepestnode(root,temp);
}
struct node* searchingNode( struct node *root, int data) {
struct Queue* queue = createQueue(SIZE);
Enqueue(root, queue);
struct node *searchNode = NULL;
while (!isEmpty(queue))
{ struct node* temp = Dequeue(queue);
if(temp->data == data){
searchNode = temp;
return searchNode;

Page No: 86
}
if (temp->left)
Enqueue(temp->left, queue);
if (temp->right)
Enqueue(temp->right, queue);

ID: 2300331530093
}
return searchNode;
}
// Driver program to test above functions
int main()
{ struct node* root = NULL;
struct Queue* queue = createQueue(SIZE);
int i;
int limit;
int data1;
printf("Enter the limit to form binary tree: ");

2023-27-CSE-AIML-B
scanf("%d",&limit);
for(i = 1; i <= limit; ++i) {
printf("Enter node: ");
scanf("%d",&data1);
insert(&root, data1, queue);
}
printf("Level order traversal before deletion\n");
levelOrder(root);
int data;

Raj Kumar Goel Institute Of Technology


printf("\nEnter data to delete: ");
scanf("%d",&data);
struct node *search = searchingNode(root, data);
if(search == NULL)
printf("Node is not present\n");
else{
findDeepestnode(root, search);
printf("Level order traversal after deletion\n");
levelOrder(root);
}
return 0;
}

TreeStructure.c
// A tree node

Page No: 87
struct node
{ int data;
struct node *right,*left;
};

ID: 2300331530093
// A queue node
struct Queue
{ int front, rear;
int size;
struct node* *array;
};

// A utility function to create a new tree node


struct node* newNode(int data)
{ struct node* temp = (struct node*) malloc(sizeof( struct node
));

2023-27-CSE-AIML-B
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}

Execution Results - All test cases have succeeded!

Raj Kumar Goel Institute Of Technology


Test Case - 1

User Output
Enter the limit to form binary tree:
5
Enter node:
45
Enter node:
16
Enter node:
26
Enter node:
36
Enter node:
78
Level order traversal before deletion
45 16 26 36 78
Enter data to delete:
26

Page No: 88
Level order traversal after deletion
45 16 78 36

Test Case - 2

ID: 2300331530093
User Output
Enter the limit to form binary tree:
6
Enter node:
45
Enter node:
15
Enter node:

2023-27-CSE-AIML-B
26
Enter node:
48
Enter node:
26
Enter node:
36

Raj Kumar Goel Institute Of Technology


Level order traversal before deletion
45 15 26 48 26 36
Enter data to delete:
86
Node is not present
Exp. Name: Binary Tree Traversals using Date: 2024-12-
S.No: 17

Page No: 89
Linked List - Recursive 21

Aim:
Write a C program to implement Binary tree traversals using Linked list.

ID: 2300331530093
You have to complete the function inorder , preorder and postorder in
Traversarls.c where parameters passed are the root reference’s of the binary tree T1.

Source Code:

TreeMain.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
// Program for linked implementation of complete binary tree

Page No: 90
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include"TreeStructure.c"

ID: 2300331530093
#include"Traversals.c"

// For Queue Size


#define SIZE 100000

// A utility function to create a new Queue


struct Queue* createQueue(int size)
{

2023-27-CSE-AIML-B
struct Queue* queue = (struct Queue*) malloc(sizeof( struct Queue
));

queue->front = queue->rear = -1;


queue->size = size;

queue->array = (struct node**) malloc


(queue->size * sizeof( struct node* ));

Raj Kumar Goel Institute Of Technology


int i;
for (i = 0; i < size; ++i)
queue->array[i] = NULL;

return queue;
}

// Standard Queue Functions


int isEmpty(struct Queue* queue)
{
return queue->front == -1;
}

int isFull(struct Queue* queue)


{ return queue->rear == queue->size - 1;

int hasOnlyOneItem(struct Queue* queue)


{ return queue->front == queue->rear;
}

Page No: 91
void Enqueue(struct node *root, struct Queue* queue)
{
if (isFull(queue))
return;

ID: 2300331530093
queue->array[++queue->rear] = root;
if (isEmpty(queue))
++queue->front;
}
struct node* Dequeue(struct Queue* queue)
{
if (isEmpty(queue))
return NULL;
struct node* temp = queue->array[queue->front];

if (hasOnlyOneItem(queue))

2023-27-CSE-AIML-B
queue->front = queue->rear = -1;
else
++queue->front;
return temp;
}

struct node* getFront(struct Queue* queue)


{ return queue->array[queue->front]; }

Raj Kumar Goel Institute Of Technology


// A utility function to check if a tree node
// has both left and right children
int hasBothChild(struct node* temp)
{
return temp && temp->left && temp->right;
}

// Function to insert a new node in complete binary tree


void insert(struct node **root, int data, struct Queue* queue)
{
// Create a new node for given data
struct node *temp = newNode(data);
// If the tree is empty, initialize the root with new node.
if (!*root)
*root = temp;
else
{
// get the front node of the queue.
struct node* front = getFront(queue);
// If the left child of this front node doesn’t exist, set the

Page No: 92
// left child as the new node
if (!front->left)
{
front->left = NULL;
front->left = temp;

ID: 2300331530093
}
// If the right child of this front node doesn’t exist, set the
// right child as the new node
else if (!front->right)
{
front->right = NULL;
front->right = temp;
}
// If the front node has both the left child and right child,
// Dequeue() it.
if (hasBothChild(front))

2023-27-CSE-AIML-B
{
Dequeue(queue);
}
}
// Enqueue() the new node for later insertions
Enqueue(temp, queue);
}

// Standard level order traversal to test above function

Raj Kumar Goel Institute Of Technology


void levelOrder(struct node* root)
{
struct Queue* queue = createQueue(SIZE);
Enqueue(root, queue);
while (!isEmpty(queue))
{
struct node* temp = Dequeue(queue);
printf("%d ", temp->data);
if (temp->left)
Enqueue(temp->left, queue);
if (temp->right)
Enqueue(temp->right, queue);
}
}

// Driver program to test above functions


int main()
{
struct node* T1 = NULL;
struct Queue* queue1 = createQueue(SIZE);
int ele;

Page No: 93
while(1){
printf("Enter value : ");
scanf("%d",&ele);
if(ele==-1){

ID: 2300331530093
break;
}
else{
insert(&T1, ele, queue1);
}
}
inorder(T1);
printf("\n");
preorder(T1);
printf("\n");
postorder(T1);

2023-27-CSE-AIML-B
}

TreeStructure.c

// A tree node
struct node
{
int data;

Raj Kumar Goel Institute Of Technology


struct node *right,*left;
struct node *root;
};

// A queue node
struct Queue
{
int front, rear;
int size;
struct node* *array;
};

// A utility function to create a new tree node


struct node* newNode(int data)
{
struct node* temp = (struct node*) malloc(sizeof( struct node ));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
Traversals.c

Page No: 94
void inorder( struct node *root)
{
// write your code here to compute
// and print inorder traversal
if (root != NULL)

ID: 2300331530093
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder( struct node *root){
// write your code here to compute
// and print preorder traversal
if (root != NULL)
{

2023-27-CSE-AIML-B
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
// write your code here to compute

Raj Kumar Goel Institute Of Technology


// and print postorder traversal
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter value :
45
Enter value :
89
Enter value :

Page No: 95
56
Enter value :
12
Enter value :

ID: 2300331530093
45
Enter value :
26
Enter value :
78
Enter value :
45
Enter value :
-1

2023-27-CSE-AIML-B
45 12 89 45 45 26 56 78
45 89 12 45 45 56 26 78
45 12 45 89 26 78 56 45

Test Case - 2

User Output

Raj Kumar Goel Institute Of Technology


Enter value :
15
Enter value :
12
Enter value :
14
Enter value :
18
Enter value :
-1
18 12 15 14
15 12 18 14
18 12 14 15
Exp. Name: Binary Tree Traversal using Date: 2024-12-
S.No: 18

Page No: 96
Linked List - Non Recursive 21

Aim:
You are tasked with implementing a complete binary tree using linked lists. The program

ID: 2300331530093
will support inserting nodes, performing level-order traversal, and executing inorder,
preorder, and postorder traversals. The implementation is divided into several source
files for modularity.

Files Overview:
48. TreeMain.c: Contains the main function, handles user input for inserting nodes
into the binary tree, and initiates tree traversals.
49. TreeStructure.c: Defines the structure for tree nodes and the queue used for level-
order traversal, as well as utility functions for creating nodes.
50. Traversals.c: Contains functions for performing the various tree traversals (inorder,
preorder, postorder).

2023-27-CSE-AIML-B
Task:
• Implement the traversal functions in Traversals.c to perform inorder, preorder,
and postorder traversals iteratively (without recursion).
.
Input Format:
• The program will repeatedly prompt the user to enter an integer value to insert
into the binary tree.

Raj Kumar Goel Institute Of Technology


• Enter -1 to stop inputting values and proceed to the traversals.

Output Format:
After building the binary tree, the program will output the results of the traversals in the
following format:
• Inorder Traversal: x1 x2 x3 ... Where x1, x2, x3, etc., are the values of the nodes in
the order they were visited.
• Preorder Traversal: y1 y2 y3 ... Where y1, y2, y3, etc., represent the values of the
nodes in the order they were visited.
• Postorder Traversal: z1 z2 z3 ... Where z1, z2, z3, etc., are the values of the nodes
in the order they were visited.
Source Code:

TreeMain.c
// Program for linked implementation of complete binary tree

Page No: 97
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include"TreeStructure.c"

ID: 2300331530093
#include"Traversals.c"

// For Queue Size


#define SIZE 100000

// A utility function to create a new Queue


struct Queue* createQueue(int size)
{
struct Queue* queue = (struct Queue*) malloc(sizeof( struct Queue
));
queue->front = queue->rear = -1;

2023-27-CSE-AIML-B
queue->size = size;
queue->array = (struct node**) malloc
(queue->size * sizeof( struct node* ));
int i;
for (i = 0; i < size; ++i)
queue->array[i] = NULL;
return queue;
}

Raj Kumar Goel Institute Of Technology


// Standard Queue Functions
int isEmpty(struct Queue* queue)
{
return queue->front == -1;
}

int isFull(struct Queue* queue)


{
return queue->rear == queue->size - 1;
}

int hasOnlyOneItem(struct Queue* queue)


{
return queue->front == queue->rear;
}

void Enqueue(struct node *root, struct Queue* queue)


{
if (isFull(queue))
return;
queue->array[++queue->rear] = root;

Page No: 98
if (isEmpty(queue))
++queue->front;
}
struct node* Dequeue(struct Queue* queue)
{

ID: 2300331530093
if (isEmpty(queue))
return NULL;
struct node* temp = queue->array[queue->front];

if (hasOnlyOneItem(queue))
queue->front = queue->rear = -1;
else
++queue->front;
return temp;
}

2023-27-CSE-AIML-B
struct node* getFront(struct Queue* queue)
{ return queue->array[queue->front]; }

// A utility function to check if a tree node


// has both left and right children
int hasBothChild(struct node* temp)
{
return temp && temp->left && temp->right;
}

Raj Kumar Goel Institute Of Technology


// Function to insert a new node in complete binary tree
void insert(struct node **root, int data, struct Queue* queue)
{
// Create a new node for given data
struct node *temp = newNode(data);
// If the tree is empty, initialize the root with new node.
if (!*root)
*root = temp;
else
{
// get the front node of the queue.
struct node* front = getFront(queue);

// If the left child of this front node doesn’t exist, set the
// left child as the new node
if (!front->left)
{
front->left = NULL;
front->left = temp;
}

Page No: 99
// If the right child of this front node doesn’t exist, set the
// right child as the new node
else if (!front->right)
{
front->right = NULL;

ID: 2300331530093
front->right = temp;
}
// If the front node has both the left child and right child,
// Dequeue() it.
if (hasBothChild(front))
{
Dequeue(queue);
}
}
// Enqueue() the new node for later insertions
Enqueue(temp, queue);

2023-27-CSE-AIML-B
}

// Standard level order traversal to test above function


void levelOrder(struct node* root)
{
struct Queue* queue = createQueue(SIZE);
Enqueue(root, queue);
while (!isEmpty(queue))
{

Raj Kumar Goel Institute Of Technology


struct node* temp = Dequeue(queue);
printf("%d ", temp->data);
if (temp->left)
Enqueue(temp->left, queue);
if (temp->right)
Enqueue(temp->right, queue);
}
}

// Driver program to test above functions


int main()
{
struct node* T1 = NULL;
struct Queue* queue1 = createQueue(SIZE);
int ele;

while(1){
printf("Enter value : ");
scanf("%d",&ele);
if(ele==-1){
break;

Page No: 100


}
else{
insert(&T1, ele, queue1);
}
}
inorder(T1);

ID: 2300331530093
preorder(T1);
postorder(T1);

TreeStructure.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
// A tree node

Page No: 101


struct node
{
int data;
struct node *right,*left;
struct node *root;
};

ID: 2300331530093
// A queue node
struct Queue
{
int front, rear;
int size;
struct node* *array;
};

// A utility function to create a new tree node

2023-27-CSE-AIML-B
struct node* newNode(int data)
{
struct node* temp = (struct node*) malloc(sizeof( struct node ));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}

Raj Kumar Goel Institute Of Technology


typedef struct node *BTNODE;

struct stacknode {
BTNODE node;
struct stacknode * next;
};

typedef struct stacknode * STKNODE;


STKNODE top = NULL;
int isempty() {
if(top == NULL) {
return 1;
}
return 0;
}

Traversals.c
void push(BTNODE b) {

Page No: 102


STKNODE temp;
temp = (STKNODE)malloc(sizeof(struct stacknode));
if(temp == NULL) {
printf("Stack is overflow.\n");
} else {
temp -> node = b;

ID: 2300331530093
temp -> next = top;
top = temp;
}
}
BTNODE peek() {
if (top == NULL) {
return NULL;
}
return top->node;
}

2023-27-CSE-AIML-B
BTNODE pop() {
STKNODE temp;
BTNODE b;
if(top == NULL) {
printf("Stack is underflow.\n");
return 0;
} else {
temp = top;
top = top -> next;

Raj Kumar Goel Institute Of Technology


b = temp->node;
free(temp);
return b;
}
}

STKNODE newStackNode(BTNODE b) {
STKNODE temp = (STKNODE)malloc(sizeof(struct node));
temp->node = b;
temp->next = NULL;
return temp;
}
BTNODE newNodeInBT(int item) {
BTNODE temp = (BTNODE)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(BTNODE root) {
// Write the inorder() traversal function, and you are free to
write your own utility functions

Page No: 103


// Required to complete the inorder traversal without using
recursion
BTNODE current = root ;
printf("Inorder: ");
while (current != NULL || top!= NULL)
{

ID: 2300331530093
while ( current != NULL)
{
push(current);
current = current->left;
}
current = pop();
printf("%d ", current->data);
current = current->right;
}
printf("\n");

2023-27-CSE-AIML-B
}

void preorder(BTNODE root) {


// Write the preorder() traversal function, and you are free to
write your own utility functions
// Required to complete the preorder traversal without using
recursion
if (root == NULL)
{

Raj Kumar Goel Institute Of Technology


return;
}
push(root);
printf("Preorder: ");
while (top != NULL)
{
BTNODE current = pop();
printf("%d ", current->data);
if (current -> right != NULL )
{
push(current->right);
}
if(current -> left != NULL)
{
push(current->left);
}
}
printf("\n");
}
void postorder(BTNODE root) {

Page No: 104


// Write the postorder() traversal function, and you are free
to write your own utility functions
// Required to complete the postorder traversal without using
recursion
if(root == NULL)
{

ID: 2300331530093
return;
}
push(root);
BTNODE prev =NULL;
printf("Postorder: ");
while(top != NULL) {
BTNODE current = peek();
if(prev == NULL || prev->left == current || prev->right
== current)
{

2023-27-CSE-AIML-B
if(current->left != NULL)
{
push(current->left);
}
else if(current -> right !=NULL){
push(current->right);
}
else{pop();
printf("%d ",current->data);}

Raj Kumar Goel Institute Of Technology


}
else if(current->left==prev){
if(current->right!=NULL){
push(current->right);
}
else{
pop();
printf("%d ",current->data);
}
}
else if(current -> right == prev){
pop();
printf("%d ",current->data);
}
prev=current;
}
printf("\n");
}
Execution Results - All test cases have succeeded!

Page No: 105


Test Case - 1

User Output
Enter value :
4

ID: 2300331530093
Enter value :
8
Enter value :
9
Enter value :
6
Enter value :
7

2023-27-CSE-AIML-B
Enter value :
-1
Inorder: 6 8 7 4 9
Preorder: 4 8 6 7 9
Postorder: 6 7 8 9 4

Test Case - 2

Raj Kumar Goel Institute Of Technology


User Output
Enter value :
9
Enter value :
8
Enter value :
3
Enter value :
4
Enter value :
7
Enter value :
5
Enter value :
1
Enter value :
-1
Inorder: 4 8 7 9 5 3 1
Preorder: 9 8 4 7 3 5 1
Postorder: 4 7 8 5 1 3 9

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 106
Exp. Name: To implement Binary Search Date: 2024-12-
S.No: 19

Page No: 107


Tree using Arrays. 21

Aim:
Write a C program to implement Binary Search Tree using using Array.

ID: 2300331530093
Note: Driver code is been provided for you to run the test cases.
Source Code:

BinarySearchTree.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include <conio.h>

Page No: 108


#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define TREENODES 100
#define FALSE 0
struct tree {

ID: 2300331530093
int info;
int used;
};
struct tree node[TREENODES];
void Createtree();
void Insert(int);
void Display();
void Setleft(int, int);
void Setright(int, int);

2023-27-CSE-AIML-B
void Createtree(int x) {
int i;
node[0].info = x;
node[0].used = TRUE;
for (i = 1; i < TREENODES; i++) node[i].used = FALSE;
}

void Insert(int x) {
// write your code here which inserts element in tha Binary tree.

Raj Kumar Goel Institute Of Technology


int i=0;
while(i<TREENODES) {
if(!node[i].used){
node[i].info=x;
node[i].used=TRUE;
//Printf("%d inserted successfully\n",x);
return;
}
else if(x==node[i].info){
printf("%d is a duplicate number\n");
return;
}
else if(x< node[i].info){
i = 2*i+1;
}
else{
i=2*i+2;
}
}
printf("No more space for new code\n");
}

Page No: 109


void Setleft(int pos, int x) {
int q;
q = 2 * pos + 1;
if (q > TREENODES)
printf("Array overflow.");

ID: 2300331530093
else if (node[q].used == TRUE)
printf("Invalid insertion.");
else {
node[q].info = x;
node[q].used = TRUE;
}
}

void Setright(int pos, int x) {


int q;

2023-27-CSE-AIML-B
q = 2 * pos + 2;
if (q > TREENODES)
printf("Array overflow.");
else if (node[q].used == TRUE)
printf("Invalid insertion.\n");
else {
node[q].info = x;
node[q].used = TRUE;
}

Raj Kumar Goel Institute Of Technology


}

void Display() {
// write your code here to display the elements in the binary tree.
for(int i = 0 ; i < TREENODES ; i++){
if(node[i].used) {
printf("%d ",node[i].info);
}
}
printf("\n");
}
void main() {
int x, y;
char ch = '1';
printf("Enter root node value:");
scanf("%d", &x);
Createtree(x);
while (ch != '3') {
printf("1.Insert\n2.Display\n3.Quit\nEnter your choice: ");
scanf("%d", &y);
switch (y) {

Page No: 110


case 1:
printf("Enter the element to be inserted:");
scanf("%d", &x);
Insert(x);
break;
case 2:

ID: 2300331530093
Display();
break;
case 3:
exit(0);
break;
default:
printf("Out of Range...!\n");
break;
}
}

2023-27-CSE-AIML-B
}

Execution Results - All test cases have succeeded!


Test Case - 1

Raj Kumar Goel Institute Of Technology


User Output
Enter root node value:
10
1.Insert
2.Display
3.Quit
Enter your choice:
1
Enter the element to be inserted:
4
1.Insert
2.Display
3.Quit
Enter your choice:
1
Enter the element to be inserted:
78
1.Insert
2.Display
3.Quit

Page No: 111


Enter your choice:
1
Enter the element to be inserted:
2

ID: 2300331530093
1.Insert
2.Display
3.Quit
Enter your choice:
1
Enter the element to be inserted:
56
1.Insert
2.Display

2023-27-CSE-AIML-B
3.Quit
Enter your choice:
2
10 4 78 2 56
1.Insert
2.Display
3.Quit
Enter your choice:

Raj Kumar Goel Institute Of Technology


3

Test Case - 2

User Output
Enter root node value:
25
1.Insert
2.Display
3.Quit
Enter your choice:
1
Enter the element to be inserted:
10
1.Insert
2.Display
3.Quit
Enter your choice:
1

Page No: 112


Enter the element to be inserted:
36
1.Insert
2.Display

ID: 2300331530093
3.Quit
Enter your choice:
1
Enter the element to be inserted:
24
1.Insert
2.Display
3.Quit
Enter your choice:
1

2023-27-CSE-AIML-B
Enter the element to be inserted:
32
1.Insert
2.Display
3.Quit
Enter your choice:
1

Raj Kumar Goel Institute Of Technology


Enter the element to be inserted:
69
1.Insert
2.Display
3.Quit
Enter your choice:
2
25 10 36 24 32 69
1.Insert
2.Display
3.Quit
Enter your choice:
1
Enter the element to be inserted:
69
69 is a duplicate number
1.Insert
2.Display
3.Quit

Page No: 113


Enter your choice:
1
Enter the element to be inserted:
2

ID: 2300331530093
1.Insert
2.Display
3.Quit
Enter your choice:
2
25 10 36 2 24 32 69
1.Insert
2.Display
3.Quit
Enter your choice:

2023-27-CSE-AIML-B
3

Raj Kumar Goel Institute Of Technology


Exp. Name: Binary Search Tree using Date: 2024-12-
S.No: 20

Page No: 114


Linked List 21

Aim:
Write a program to create a binary search tree of integers and perform the following
operations using linked list.

ID: 2300331530093
51. Insert a node.
52. Delete a node.

Note:
• Driver code is already provided to you, your task is to fill in the remaining code.
• Refer to visible test cases for better understanding.
Source Code:

BinarySearchTree.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include <stdio.h>

Page No: 115


#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};

ID: 2300331530093
// Creation
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// Inorder Traversal using recursion


void inorder(struct node *root) {

2023-27-CSE-AIML-B
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d ", root->key);
// Traverse right
inorder(root->right);
}
}

Raj Kumar Goel Institute Of Technology


void preorder(struct node *root) {
if (root != NULL) {
// Traverse left
printf("%d ", root->key);
preorder(root->left);
// Traverse right
preorder(root->right);
}
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}

// Insert a node in BST


struct node *insert(struct node *node, int key) {

Page No: 116


// write your code here to perform insertion operation
if(node == NULL) {
return newNode(key);
}
if(key < node->key) {
node->left = insert(node->left,key);

ID: 2300331530093
}else if(key > node->key) {
node->right = insert(node->right,key);
}
return node;
}

// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// write your code here to perform deletion operation
if(root == NULL) {

2023-27-CSE-AIML-B
return root;
}
if(key < root->key) {
root->left = deleteNode(root->left,key);
} else if(key > root->key){
root ->right = deleteNode(root->right,key);
} else{
if(root->left == NULL){
struct node *temp=root->right;

Raj Kumar Goel Institute Of Technology


free(root);
return temp;
}else if(root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right,temp->key);
}
return root;

// Driver code
int main() {
struct node *root = NULL;
int n,data;
printf("Enter how many nodes you want to insert in BST :");
scanf("%d",&n);

Page No: 117


for( int i =0 ; i < n ; i++){
printf("Enter the value: ");
scanf("%d", &data);
root = insert(root, data);
}

ID: 2300331530093
printf("Inorder traversal(Always gives ascending order): ");
inorder(root);
printf("\nPreorder traversal: ");
preorder(root);
printf("\nEnter the data to delete: ");
scanf("%d",&data);

printf("After deleting %d\n",data);


root = deleteNode(root, data);
printf("Inorder traversal: ");

2023-27-CSE-AIML-B
inorder(root);
printf("\nPreorder traversal: ");
preorder(root);
}

Execution Results - All test cases have succeeded!

Raj Kumar Goel Institute Of Technology


Test Case - 1

User Output
Enter how many nodes you want to insert in BST :
5
Enter the value:
10
Enter the value:
9
Enter the value:
20
Enter the value:
45
Enter the value:
8
Inorder traversal(Always gives ascending order): 8 9 10 20 45
Preorder traversal: 10 9 8 20 45
Enter the data to delete:
8

Page No: 118


After deleting 8
Inorder traversal: 9 10 20 45
Preorder traversal: 10 9 20 45

ID: 2300331530093
Test Case - 2

User Output
Enter how many nodes you want to insert in BST :
6
Enter the value:
45
Enter the value:
15

2023-27-CSE-AIML-B
Enter the value:
65
Enter the value:
25
Enter the value:
35
Enter the value:

Raj Kumar Goel Institute Of Technology


95
Inorder traversal(Always gives ascending order): 15 25 35 45 65 95
Preorder traversal: 45 15 25 35 65 95
Enter the data to delete:
15
After deleting 15
Inorder traversal: 25 35 45 65 95
Preorder traversal: 45 25 35 65 95
Date: 2024-12-
S.No: 21 Exp. Name: Operations on BST

Page No: 119


20

Aim:
Write a program to create a binary search tree of integers and perform the following
operations using linked list.

ID: 2300331530093
53. Insert a node
54. In-order traversal
55. Pre-order traversal
56. Post-order traversal
57. Search an element
58. Exit
Source Code:

BinarySearchTree.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

Page No: 120


#include<stdlib.h>
#include "InsertAndTraversals.c"

void main() {
int x, op;
BSTNODE root = NULL;

ID: 2300331530093
while(1) {
printf("1.Insert 2.Inorder Traversal 3.Preorder
Traversal 4.Postorder Traversal 5.Search an element 6.Exit\n");
printf("Enter your option: ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element to be inserted
: ");
scanf("%d", &x);
root = insertNodeInBST(root,x);

2023-27-CSE-AIML-B
break;
case 2:
if(root == NULL) {
printf("Binary Search
Tree is empty.\n");
}
else {
printf("Elements of the
BST (in-order traversal): ");

Raj Kumar Goel Institute Of Technology


inorderInBST(root);
printf("\n");
}
break;
case 3:
if(root == NULL) {
printf("Binary Search
Tree is empty.\n");
}
else {
printf("Elements of the
BST (pre-order traversal): ");
preorderInBST(root);
printf("\n");
}
break;
case 4:
if(root == NULL) {
printf("Binary Search
Tree is empty.\n");
}

Page No: 121


else {
printf("Elements of the
BST (post-order traversal): ");
postorderInBST(root);
printf("\n");
}

ID: 2300331530093
break;
case 5:
printf("Enter an element to be
searched : ");
scanf("%d", &x);
if( searchNodeInBST(root,x) ==
NULL)
printf("Element not
found in the binary search tree.\n");
else

2023-27-CSE-AIML-B
printf("Element found
in the binary search tree.\n");
break;
case 6:
exit(0);
}
}
}

Raj Kumar Goel Institute Of Technology


InsertAndTraversals.c
struct node {

Page No: 122


int data;
struct node *left, *right;
};

typedef struct node *BSTNODE;

ID: 2300331530093
BSTNODE newNodeInBST(int item) {
BSTNODE temp = (BSTNODE)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}

void inorderInBST(BSTNODE root) {


//write your code here..
if(root != NULL){

2023-27-CSE-AIML-B
inorderInBST(root->left);
printf("%d ",root->data);
inorderInBST(root->right);
}
}
void preorderInBST(BSTNODE root) {
//write your code here..
if(root!=NULL){
printf("%d ",root->data);

Raj Kumar Goel Institute Of Technology


preorderInBST(root->left);
preorderInBST(root->right);
}
}
void postorderInBST(BSTNODE root) {
//write your code here..
if(root!=NULL){
postorderInBST(root->left);
postorderInBST(root->right);
printf("%d ",root->data);
}

}
BSTNODE insertNodeInBST(BSTNODE node, int ele) {
//write your code here..
if(node == NULL){
printf("Successfully inserted.\n");
return newNodeInBST(ele);
}
if(ele < node->data)
node->left=insertNodeInBST(node->left,ele);

Page No: 123


else if(ele > node->data)
node->right=insertNodeInBST(node->right,ele);
else
printf("Element already existinBST.\n");
return node;

ID: 2300331530093
}

BSTNODE searchNodeInBST(BSTNODE root, int ele) {


//write your code here..
if(root == NULL || root->data ==ele){
return root;
}
if(ele<root->data){
return searchNodeInBST(root->left,ele);
}

2023-27-CSE-AIML-B
else{
return searchNodeInBST(root->right,ele);
}
}

Execution Results - All test cases have succeeded!

Raj Kumar Goel Institute Of Technology


Test Case - 1

User Output
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
2
Binary Search Tree is empty.
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
4
Binary Search Tree is empty.
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
5
Enter an element to be searched :
6
Element not found in the binary search tree.

Page No: 124


1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
3

ID: 2300331530093
Binary Search Tree is empty.
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
1
Enter an element to be inserted :
5
Successfully inserted.
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit

2023-27-CSE-AIML-B
Enter your option:
1
Enter an element to be inserted :
2
Successfully inserted.
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option:
2
Elements of the BST (in-order traversal): 2 5
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
3
Elements of the BST (pre-order traversal): 5 2
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
4
Elements of the BST (post-order traversal): 2 5
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
5
Enter an element to be searched :
2
Element found in the binary search tree.

Page No: 125


1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
5

ID: 2300331530093
Enter an element to be searched :
6
Element not found in the binary search tree.
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
1
Enter an element to be inserted :
1
Successfully inserted.

2023-27-CSE-AIML-B
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit
Enter your option:
2
Elements of the BST (in-order traversal): 1 2 5
1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder
Traversal 5.Search an element 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option:
6
Date: 2024-12-
S.No: 22 Exp. Name: Breadth First Search (BFS)

Page No: 126


20

Aim:
Write a program to implement Breadth First Search (BFS) graph traversal methods.

ID: 2300331530093
Source Code:

GraphsBFS.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include <stdio.h>

Page No: 127


#include <stdlib.h>
#define MAX 99
struct node {
struct node *next;
int vertex;
};

ID: 2300331530093
typedef struct node * GNODE;
GNODE graph[20];
int visited[20];
int queue[MAX], front = -1,rear = -1;
int n;
void insertQueue(int vertex) {
if(rear == MAX-1)
printf("Queue overflow.\n");
else{
if(front==-1)

2023-27-CSE-AIML-B
front=0;
rear=rear+1;
queue[rear]= vertex;
}
}
int isEmptyQueue() {
if(front == -1 || front>rear)
return 1;
else

Raj Kumar Goel Institute Of Technology


return 0;
}
int deleteQueue() {
int deleteItem;
if(front == -1 ||front>rear){
printf("Queue underflow\n");
exit(1);
}
deleteItem = queue[front];
front = front +1;
return deleteItem;
}
void BFS(int v) {
int w;
insertQueue(v);
while(!isEmptyQueue()){
v=deleteQueue( );
printf("\n%d",v);
visited[v]=1;
GNODE g = graph[v];
for(;g!=NULL;g=g->next){

Page No: 128


w=g->vertex;
if(visited[w]==0){
insertQueue(w);
visited[w]=1;
}
}

ID: 2300331530093
}
}

void main() {
int N, E, s, d, i, j, v;
GNODE p, q;
printf("Enter the number of vertices : ");
scanf("%d",&N);
printf("Enter the number of edges : ");
scanf("%d",&E);

2023-27-CSE-AIML-B
for(i=1;i<=E;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
q=(GNODE)malloc(sizeof(struct node));
q->vertex=d;
q->next=NULL;
if(graph[s]==NULL) {

Raj Kumar Goel Institute Of Technology


graph[s]=q;
} else {
p=graph[s];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter Start Vertex for BFS : ");
scanf("%d", &v);
printf("BFS of graph : ");
BFS(v);
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 129


User Output
Enter the number of vertices :
5
Enter the number of edges :

ID: 2300331530093
5
Enter source :
1
Enter destination :
2
Enter source :
1
Enter destination :
4

2023-27-CSE-AIML-B
Enter source :
4
Enter destination :
2
Enter source :
2
Enter destination :

Raj Kumar Goel Institute Of Technology


3
Enter source :
4
Enter destination :
5
Enter Start Vertex for BFS :
1
BFS of graph :
1
2
4
3
5

Test Case - 2

User Output
Enter the number of vertices :
4

Page No: 130


Enter the number of edges :
3
Enter source :
1

ID: 2300331530093
Enter destination :
2
Enter source :
2
Enter destination :
3
Enter source :
3
Enter destination :

2023-27-CSE-AIML-B
4
Enter Start Vertex for BFS :
2
BFS of graph :
2
3
4

Raj Kumar Goel Institute Of Technology


Test Case - 3

User Output
Enter the number of vertices :
9
Enter the number of edges :
12
Enter source :
0
Enter destination :
1
Enter source :
0
Enter destination :
3
Enter source :
0
Enter destination :

Page No: 131


4
Enter source :
1
Enter destination :

ID: 2300331530093
2
Enter source :
1
Enter destination :
3
Enter source :
3
Enter destination :
6

2023-27-CSE-AIML-B
Enter source :
6
Enter destination :
4
Enter source :
6
Enter destination :

Raj Kumar Goel Institute Of Technology


7
Enter source :
7
Enter destination :
8
Enter source :
4
Enter destination :
5
Enter source :
2
Enter destination :
5
Enter source :
7
Enter destination :
5
8
7
5
6
2
4
3
1
0
0
BFS of graph :
Enter Start Vertex for BFS :

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 132
Date: 2024-12-
S.No: 23 Exp. Name: Depth First Search (DFS)

Page No: 133


20

Aim:
Write a program to implement Depth First Search (DFS) graph traversal methods.
Source Code:

ID: 2300331530093
GraphsDFS.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

Page No: 134


#include<stdlib.h>
struct node {
struct node *next;
int vertex;
};
typedef struct node * GNODE;

ID: 2300331530093
GNODE graph[20];
int visited[20];
int v,n;
void DFS(int i) {
GNODE p;
printf("\n%d",i);
p=graph[i];
visited[i]=1;
while(p!=NULL){
i=p->vertex;

2023-27-CSE-AIML-B
if(!visited[i])
DFS(i);
p=p->next;
}
}
void main() {
int N,E,i,s,d;
GNODE q,p;
printf("Enter the number of vertices : ");

Raj Kumar Goel Institute Of Technology


scanf("%d",&N);
printf("Enter the number of edges : ");
scanf("%d",&E);
for(i=1;i<=E;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
q=(GNODE)malloc(sizeof(struct node));
q->vertex=d;
q->next=NULL;
if(graph[s]==NULL)
graph[s]=q;
else {
p=graph[s];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
for(i=0;i<n;i++)

Page No: 135


visited[i]=0;
printf("Enter Start Vertex for DFS : ");
scanf("%d",&v);
printf("DFS of graph : ");
DFS(v);
printf("\n");

ID: 2300331530093
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output

2023-27-CSE-AIML-B
Enter the number of vertices :
6
Enter the number of edges :
7
Enter source :
1
Enter destination :
2

Raj Kumar Goel Institute Of Technology


Enter source :
1
Enter destination :
4
Enter source :
4
Enter destination :
2
Enter source :
2
Enter destination :
3
Enter source :
4
Enter destination :
5
Enter source :
4
Enter destination :

Page No: 136


3
Enter source :
3
Enter destination :

ID: 2300331530093
6
Enter Start Vertex for DFS :
1
DFS of graph :
1
2
3
6
4

2023-27-CSE-AIML-B
5

Test Case - 2

User Output
Enter the number of vertices :
5

Raj Kumar Goel Institute Of Technology


Enter the number of edges :
5
Enter source :
1
Enter destination :
2
Enter source :
1
Enter destination :
4
Enter source :
4
Enter destination :
2
Enter source :
2
Enter destination :
3
Enter source :

Page No: 137


4
Enter destination :
5
Enter Start Vertex for DFS :

ID: 2300331530093
1
DFS of graph :
1
2
3
4
5

Test Case - 3

2023-27-CSE-AIML-B
User Output
Enter the number of vertices :
4
Enter the number of edges :
4
Enter source :

Raj Kumar Goel Institute Of Technology


1
Enter destination :
1
Enter source :
1
Enter destination :
3
Enter source :
2
Enter destination :
2
Enter source :
4
Enter destination :
3
Enter Start Vertex for DFS :
1
3
1
DFS of graph :

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 138
Exp. Name: Minimum spanning tree - Date: 2024-12-
S.No: 24

Page No: 139


Prim's algorithm 20

Aim:
Implement Prim's algorithm in C to find the Minimum Cost Spanning Tree of a connected,
undirected graph. Prompt the user to input the number of vertices (n) and the number of

ID: 2300331530093
edges (e). Then, accept edge information (source, destination, and weight) to build the
adjacency matrix.
Source Code:

PrimsMinimumSpanningTree.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

Page No: 140


#include<conio.h>
int a,b,u,v,n,i,j,ne=1,e,s,d,w;
int visited[10]={0},min,mincost=0,cost[10][10];
void prims(){
visited[1]=1;
while(ne<n){

ID: 2300331530093
min = 999;
for(i=1;i<=n;i++){
if(visited[i]==1){
for(j=1;j<=n;j++){
if(visited[j]==0 && cost[i][j]
< min){
min = cost[i][j];
a=i;
b=j;
}

2023-27-CSE-AIML-B
}
}
}
printf("Edge cost from %d to %d : %d\n",a,b,cost[a]
[b]);
ne++;
mincost+=cost[a][b];
visited[b]=1;
cost[a][b]=cost[b][a]=999;

Raj Kumar Goel Institute Of Technology


}
printf("Minimum cost of spanning tree = %d\n",mincost);
}
void main() {
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter the number of edges : ");
scanf("%d",&e);
for(i=1;i<=e;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
printf("Enter weight : ");
scanf("%d",&w);
if(s<=0 || d<=0 || s> n || d > n || w < 0 ) {
printf("Invalid data.Try again.\n");
i--;
continue;
}
cost[s][d]=w;

Page No: 141


cost[d][s]=w;
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(cost[i][j]==0)
cost[i][j]=999;

ID: 2300331530093
}
}
printf("The edges of Minimum Cost Spanning Tree are : \n");
prims();
}

Execution Results - All test cases have succeeded!

2023-27-CSE-AIML-B
Test Case - 1

User Output
Enter the number of vertices :
7
Enter the number of edges :
11

Raj Kumar Goel Institute Of Technology


Enter source :
1
Enter destination :
2
Enter weight :
7
Enter source :
2
Enter destination :
3
Enter weight :
8
Enter source :
1
Enter destination :
4
Enter weight :
5
Enter source :

Page No: 142


2
Enter destination :
4
Enter weight :

ID: 2300331530093
9
Enter source :
2
Enter destination :
5
Enter weight :
7
Enter source :
3

2023-27-CSE-AIML-B
Enter destination :
5
Enter weight :
5
Enter source :
4
Enter destination :

Raj Kumar Goel Institute Of Technology


5
Enter weight :
15
Enter source :
4
Enter destination :
6
Enter weight :
6
Enter source :
5
Enter destination :
6
Enter weight :
8
Enter source :
5
Enter destination :
7

Page No: 143


Enter weight :
9
Enter source :
6

ID: 2300331530093
Enter destination :
7
Enter weight :
11
The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 4 : 5
Edge cost from 4 to 6 : 6
Edge cost from 1 to 2 : 7
Edge cost from 2 to 5 : 7

2023-27-CSE-AIML-B
Edge cost from 5 to 3 : 5
Edge cost from 5 to 7 : 9
Minimum cost of spanning tree = 39

Test Case - 2

User Output

Raj Kumar Goel Institute Of Technology


Enter the number of vertices :
7
Enter the number of edges :
9
Enter source :
2
Enter destination :
7
Enter weight :
14
Enter source :
1
Enter destination :
2
Enter weight :
28
Enter source :
1
Enter destination :

Page No: 144


6
Enter weight :
10
Enter source :

ID: 2300331530093
5
Enter destination :
6
Enter weight :
25
Enter source :
4
Enter destination :
5

2023-27-CSE-AIML-B
Enter weight :
22
Enter source :
4
Enter destination :
7
Enter weight :

Raj Kumar Goel Institute Of Technology


18
Enter source :
2
Enter destination :
3
Enter weight :
16
Enter source :
3
Enter destination :
4
Enter weight :
12
Enter source :
5
Enter destination :
7
Enter weight :
24

Page No: 145


The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 6 : 10
Edge cost from 6 to 5 : 25
Edge cost from 5 to 4 : 22

ID: 2300331530093
Edge cost from 4 to 3 : 12
Edge cost from 3 to 2 : 16
Edge cost from 2 to 7 : 14
Minimum cost of spanning tree = 99

Test Case - 3

User Output
Enter the number of vertices :

2023-27-CSE-AIML-B
5
Enter the number of edges :
7
Enter source :
1
Enter destination :
2

Raj Kumar Goel Institute Of Technology


Enter weight :
2
Enter source :
1
Enter destination :
3
Enter weight :
5
Enter source :
2
Enter destination :
3
Enter weight :
8
Enter source :
2
Enter destination :
4
Enter weight :

Page No: 146


4
Enter source :
3
Enter destination :

ID: 2300331530093
4
Enter weight :
5
Enter source :
3
Enter destination :
5
Enter weight :
6

2023-27-CSE-AIML-B
Enter source :
4
Enter destination :
5
Enter weight :
4
The edges of Minimum Cost Spanning Tree are :

Raj Kumar Goel Institute Of Technology


Edge cost from 1 to 2 : 2
Edge cost from 2 to 4 : 4
Edge cost from 4 to 5 : 4
Edge cost from 1 to 3 : 5
Minimum cost of spanning tree = 15

Test Case - 4

User Output
Enter the number of vertices :
5
Enter the number of edges :
7
Enter source :
1
Enter destination :
2
Enter weight :
2

Page No: 147


Enter source :
1
Enter destination :
3

ID: 2300331530093
Enter weight :
5
Enter source :
2
Enter destination :
3
Enter weight :
8
Enter source :

2023-27-CSE-AIML-B
2
Enter destination :
4
Enter weight :
4
Enter source :
3

Raj Kumar Goel Institute Of Technology


Enter destination :
5
Enter weight :
6
Enter source :
4
Enter destination :
5
Enter weight :
4
Enter source :
3
Enter destination :
4
Enter weight :
5
The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 2 : 2
Edge cost from 2 to 4 : 4

Page No: 148


Edge cost from 4 to 5 : 4
Edge cost from 1 to 3 : 5
Minimum cost of spanning tree = 15

ID: 2300331530093
2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
Exp. Name: Dijkstra's Shortest path Date: 2024-12-
S.No: 25

Page No: 149


algorithm 28

Aim:
Given a graph G and source vertex S, Dijkstra's shortest path algorithm is used to find
shortest paths from source S to all vertices in the given graph.

ID: 2300331530093
Dijkstra algorithm is also called single source shortest path algorithm. It is based on
greedy technique. Little variation in the algorithm can find the shortest path from the
source nodes to all the other nodes in the graph.

Sample Input and Output:


Enter the number of vertices : 4
Enter the number of edges : 5
Enter source : 1
Enter destination : 2
Enter weight : 4
Enter source : 1

2023-27-CSE-AIML-B
Enter destination : 4
Enter weight : 10
Enter source : 1
Enter destination : 3
Enter weight : 6
Enter source : 2
Enter destination : 4
Enter weight : 5
Enter source : 3

Raj Kumar Goel Institute Of Technology


Enter destination : 4
Enter weight : 2
Enter the source :1
Node Distance Path
2 4 2<-1
3 6 3<-1
4 8 4<-3<-1
Source Code:

Dijkstras.c
#include <limits.h>

Page No: 150


#include <stdio.h>
#define MAX 20
int V,E;
int graph[MAX][MAX];
#define INFINITY 99999
void dijkstra(int G[MAX][MAX],int n,int startnode) {int cost[MAX]

ID: 2300331530093
[MAX],distance[MAX],pred[MAX];

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
int visited[MAX],count,mindistance,nextnode,i,j;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 151
for(i=1;i<=n;i++)

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 152
for(j=1;j<=n;j++)

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 153
if(G[i][j]==0)

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 154
cost[i][j]=INFINITY;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 155
else

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 156
cost[i][j]=G[i][j];

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 157
for(i=1;i<=n;i++){

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 158
distance[i]=cost[startnode][i];

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 159
pred[i]=startnode;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 160
visited[i]=0;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 161
}

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 162
distance[startnode]=0;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 163
visited[startnode]=1;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 164
count=1;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 165
while(count<n-1){

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 166
mindistance=INFINITY;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 167
for(i=1;i<=n;i++)

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 168
<mindistance&&!visited[i]){
if(distance[i]

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 169
mindistance=distance[i];

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 170
nextnode=i;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 171
}visited[nextnode]=1;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 172
for(i=1;i<=n;i++)

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 173
if(!visited[i])

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 174
[i]<distance[i]){
if(mindistance+cost[nextnode]

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 175
distance[i]=mindistance+cost[nextnode][i];

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 176
pred[i]=nextnode;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 177
}

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 178
count++;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 179
}

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 180
printf("Node\tDistance\tPath\n");

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 181
for(i=1;i<=n;i++)

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 182
if(i!=startnode){

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 183
if(distance[i]==INFINITY){

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 184
printf("%4d\t%8s",i,"INF");

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 185
printf("\tNO PATH\n");

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 186
}else{

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 187
printf("%4d\t%8d",i,distance[i]);

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 188
printf("\t%d",i);

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 189
j=i;

Raj Kumar Goel Institute Of Technology 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 190
do{

Page No: 191


j=pred[j];

printf("<-%d",j);

}while(j!=startnode);

ID: 2300331530093
printf("\n");

}
}
int main() {
int s,d,w,i,j;
printf("Enter the number of vertices : ");

2023-27-CSE-AIML-B
scanf("%d",&V);
printf("Enter the number of edges : ");
scanf("%d",&E);
for(i = 1 ; i <= V; i++) {
for(j=1; j <= V; j++ ) {
graph[i][i] = 0;
}
}
for(i=1;i<=E;i++) {

Raj Kumar Goel Institute Of Technology


printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
printf("Enter weight : ");
scanf("%d",&w);
if(s > V || d > V || s<=0 || d<=0) {
printf("Invalid index. Try again.\n");
i--;
continue;
} else {
graph[s][d] = w;
}
}
printf("Enter the source :");
scanf("%d",&s);
dijkstra(graph, V,s);
return 0;
}
Execution Results - All test cases have succeeded!

Page No: 192


Test Case - 1

User Output
Enter the number of vertices :
4

ID: 2300331530093
Enter the number of edges :
5
Enter source :
1
Enter destination :
2
Enter weight :
4

2023-27-CSE-AIML-B
Enter source :
1
Enter destination :
4
Enter weight :
10
Enter source :
1

Raj Kumar Goel Institute Of Technology


Enter destination :
3
Enter weight :
6
Enter source :
2
Enter destination :
4
Enter weight :
5
Enter source :
3
Enter destination :
4
Enter weight :
2
Enter the source :
1
Node Distance Path

Page No: 193


2 4 2<-1
3 6 3<-1
4 8 4<-3<-1

ID: 2300331530093
Test Case - 2

User Output
Enter the number of vertices :
5
Enter the number of edges :
6
Enter source :
1

2023-27-CSE-AIML-B
Enter destination :
2
Enter weight :
2
Enter source :
1
Enter destination :

Raj Kumar Goel Institute Of Technology


5
Enter weight :
3
Enter source :
2
Enter destination :
4
Enter weight :
4
Enter source :
2
Enter destination :
3
Enter weight :
7
Enter source :
4
Enter destination :
3

Page No: 194


Enter weight :
2
Enter source :
5

ID: 2300331530093
Enter destination :
4
Enter weight :
1
Enter the source :
2
Node Distance Path
1 INF NO PATH
3 6 3<-4<-2

2023-27-CSE-AIML-B
4 4 4<-2
5 INF NO PATH

Test Case - 3

User Output
Enter the number of vertices :

Raj Kumar Goel Institute Of Technology


4
Enter the number of edges :
5
Enter source :
1
Enter destination :
2
Enter weight :
4
Enter source :
3
Enter destination :
2
Enter weight :
5
Enter source :
4
Enter destination :
1

Page No: 195


Enter weight :
1
Enter source :
4

ID: 2300331530093
Enter destination :
2
Enter weight :
3
Enter source :
4
Enter destination :
3
Enter weight :

2023-27-CSE-AIML-B
8
Enter the source :
1
Node Distance Path
2 4 2<-1
3 INF NO PATH
4 INF NO PATH

Raj Kumar Goel Institute Of Technology


Exp. Name: Minimum spanning tree - Date: 2024-12-
S.No: 26

Page No: 196


Kruskal’s Algorithm 20

Aim:
Write a C program to implement Kruskal's algorithm to generate a minimum cost

ID: 2300331530093
spanning tree.
Source Code:

KruskalSpanningTree.c

2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

Page No: 197


#include<stdlib.h>

// write your code here..


int i,j,k,a,b,u,v,n,e,s,d,w,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int i){

ID: 2300331530093
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j){
if(i!=j){
parent[j]=i;
return 1;
}
return 0;

2023-27-CSE-AIML-B
}
void kruskal(){
while(ne<n){
for(i=1,min=999;i<=n;i++){
for(j=1;j<=n;j++){
if(cost[i][j]<min){
min=cost[i][j];a=u=i;
b=v=j;
}

Raj Kumar Goel Institute Of Technology


}
}
u=find(u);
v=find(v);
if(uni(u,v)){
printf("Edge cost from %d to %d :
%d\n",a,b,min);
mincost +=min;
ne++;
}
cost[a][b]=cost[b][a]=999;
}
printf("Minimum cost of spanning tree = %d\n",mincost);
}

void main() {
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter the number of edges : ");
scanf("%d",&e);
for(i=1;i<=e;i++) {

Page No: 198


printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
printf("Enter weight : ");
scanf("%d",&w);

ID: 2300331530093
if(s<=0 || d<=0 || s> n || d > n || w < 0 ) {
printf("Invalid data.Try again.\n");
i--;
continue;
}
cost[s][d]=w;
}
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
if(cost[i][j]==0)

2023-27-CSE-AIML-B
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are : \n");
kruskal();
}

Raj Kumar Goel Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter the number of vertices :
7
Enter the number of edges :
9
Enter source :
2
Enter destination :
7
Enter weight :
14
Enter source :
1
Enter destination :
2

Page No: 199


Enter weight :
28
Enter source :
1

ID: 2300331530093
Enter destination :
6
Enter weight :
10
Enter source :
5
Enter destination :
6
Enter weight :

2023-27-CSE-AIML-B
25
Enter source :
4
Enter destination :
5
Enter weight :
22

Raj Kumar Goel Institute Of Technology


Enter source :
4
Enter destination :
7
Enter weight :
18
Enter source :
2
Enter destination :
3
Enter weight :
16
Enter source :
3
Enter destination :
4
Enter weight :
12
Enter source :

Page No: 200


5
Enter destination :
7
Enter weight :

ID: 2300331530093
24
The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 6 : 10
Edge cost from 3 to 4 : 12
Edge cost from 2 to 7 : 14
Edge cost from 2 to 3 : 16
Edge cost from 4 to 5 : 22
Edge cost from 5 to 6 : 25
Minimum cost of spanning tree = 99

2023-27-CSE-AIML-B
Test Case - 2

User Output
Enter the number of vertices :
7
Enter the number of edges :

Raj Kumar Goel Institute Of Technology


11
Enter source :
1
Enter destination :
2
Enter weight :
7
Enter source :
2
Enter destination :
3
Enter weight :
8
Enter source :
1
Enter destination :
4
Enter weight :
5

Page No: 201


Enter source :
2
Enter destination :
4

ID: 2300331530093
Enter weight :
9
Enter source :
2
Enter destination :
5
Enter weight :
7
Enter source :

2023-27-CSE-AIML-B
3
Enter destination :
5
Enter weight :
5
Enter source :
4

Raj Kumar Goel Institute Of Technology


Enter destination :
5
Enter weight :
15
Enter source :
4
Enter destination :
6
Enter weight :
6
Enter source :
5
Enter destination :
6
Enter weight :
8
Enter source :
5
Enter destination :

Page No: 202


7
Enter weight :
9
Enter source :

ID: 2300331530093
6
Enter destination :
7
Enter weight :
11
The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 4 : 5
Edge cost from 3 to 5 : 5
Edge cost from 4 to 6 : 6

2023-27-CSE-AIML-B
Edge cost from 1 to 2 : 7
Edge cost from 2 to 5 : 7
Edge cost from 5 to 7 : 9
Minimum cost of spanning tree = 39

Raj Kumar Goel Institute Of Technology

You might also like