Lab Record Download 2
Lab Record Download 2
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]);
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 :
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] =
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]);
}
2023-27-CSE-AIML-B
printf("a[%d] = %d\n",i,a[i]);
}
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
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
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]);
}
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
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
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]);
}
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 :
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] =
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:
Next, the program should print the following message on the console as:
2023-27-CSE-AIML-B
Enter 5 elements :
Enter 5 elements : 34 67 12 45 22
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
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++;
}
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
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]);
}
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) {
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
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++){
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
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;
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
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
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.
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 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");}}
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;
}
}
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
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.
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
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 :
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.
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:
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);
}
}
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");
}}
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.
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
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
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 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");
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;
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
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
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
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
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
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:
StackOperationsLL.c
struct stack {
Page No: 57
int data;
struct stack *next;
};
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;
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
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
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.
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
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:
QueueOperationsLL.c
struct queue {
Page No: 64
int data;
struct queue *next;
};
ID: 2300331530093
Q front = NULL, rear = NULL;
2023-27-CSE-AIML-B
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Successfully inserted.\n");
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");
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 :
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 :
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
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 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;
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");
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 :
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 :
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
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>
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;
}
Page No: 78
}
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);
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);
2023-27-CSE-AIML-B
deleteNode(tree, delNode, arraySize);
printf("Tree after deletion\n");
displayTree(tree, arraySize);
return 0;
}
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
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
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) {
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);
}
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);
}
}
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;
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;
};
2023-27-CSE-AIML-B
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
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
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"
2023-27-CSE-AIML-B
struct Queue* queue = (struct Queue*) malloc(sizeof( struct Queue
));
return queue;
}
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;
}
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);
}
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;
// A queue node
struct Queue
{
int front, rear;
int size;
struct node* *array;
};
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
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
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.
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"
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;
}
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]; }
// 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
}
while(1){
printf("Enter value : ");
scanf("%d",&ele);
if(ele==-1){
break;
ID: 2300331530093
preorder(T1);
postorder(T1);
TreeStructure.c
2023-27-CSE-AIML-B
Raj Kumar Goel Institute Of Technology
// A tree node
ID: 2300331530093
// A queue node
struct Queue
{
int front, rear;
int size;
struct node* *array;
};
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;
}
struct stacknode {
BTNODE node;
struct stacknode * next;
};
Traversals.c
void push(BTNODE 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;
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
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
}
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);}
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 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 106
Exp. Name: To implement Binary Search Date: 2024-12-
S.No: 19
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>
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.
ID: 2300331530093
else if (node[q].used == TRUE)
printf("Invalid insertion.");
else {
node[q].info = x;
node[q].used = TRUE;
}
}
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;
}
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) {
ID: 2300331530093
Display();
break;
case 3:
exit(0);
break;
default:
printf("Out of Range...!\n");
break;
}
}
2023-27-CSE-AIML-B
}
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:
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
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
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
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>
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;
}
2023-27-CSE-AIML-B
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d ", root->key);
// Traverse right
inorder(root->right);
}
}
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;
// 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);
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);
2023-27-CSE-AIML-B
inorder(root);
printf("\nPreorder traversal: ");
preorder(root);
}
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
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:
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>
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): ");
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);
}
}
}
ID: 2300331530093
BSTNODE newNodeInBST(int item) {
BSTNODE temp = (BSTNODE)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
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);
}
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);
ID: 2300331530093
}
2023-27-CSE-AIML-B
else{
return searchNodeInBST(root->right,ele);
}
}
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.
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
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
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>
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
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) {
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 :
Test Case - 2
User Output
Enter the number of vertices :
4
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
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 :
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 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 132
Date: 2024-12-
S.No: 23 Exp. Name: Depth First Search (DFS)
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>
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 : ");
ID: 2300331530093
}
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
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
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 2023-27-CSE-AIML-B ID: 2300331530093 Page No: 138
Exp. Name: Minimum spanning tree - Date: 2024-12-
S.No: 24
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>
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;
ID: 2300331530093
}
}
printf("The edges of Minimum Cost Spanning Tree are : \n");
prims();
}
2023-27-CSE-AIML-B
Test Case - 1
User Output
Enter the number of vertices :
7
Enter the number of edges :
11
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 :
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
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 :
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
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 :
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
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
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
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.
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
Dijkstras.c
#include <limits.h>
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{
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++) {
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
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 :
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 :
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
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>
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;
}
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++) {
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();
}
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
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
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 :
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
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