Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

JSS MAHAVIDYAPEETHA

JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA


Department of CSDS

S. DATE OF NAME OF DATE OF TEACHER’S


NO. PERFORM EXPERINMENT SUBMISSION SIGNATURE
Array handling through
1 functions

To search an array for


2 the given element using
linear and binary search
To sort an array using
3 iterative sorting algorithms

To sort an array using


4 recursive sorting
algorithms
To implement singly
5 linked lists

To implement stack
6 operations using linked
Lists
To implement queues
7 using arrays.

To implement binary
8 search trees.

To implement graphs &


9 traversals

To find minimum spanning


10 tree of given
Graph
JSS Academy of Technical Education, Noida Department of CSDS

Experiment 1
Aim: To use functions for array handling

Programming Assignment: Write a program using functions to find


 the total number of even elements in a given array
 the maximum element in a given array
Pre-Lab Work:
Q1) How are single dimensional arrays used in C programs?

Q2) How is an array passed as an argument to a function in C programs?

Program:
#include <stdio.h>

int traverse(int *a){


int count = 0;
for (int i = 0; i < 10; i++){
if (*(i + a) == 0)
break;
count++;}
return count;};

void insert(int *a){


int b;
int c;
int value;
printf("Enter the index at which you want to insert an element and the value: ");
scanf("%d %d", &b, &value);

c = traverse(a);

for (int i = c - 1; i > b - 1; i--){


*(a + i + 1) = *(a + i);};

*(a + b) = value;

printf("Array after insertion is: ");


for (int i = 0; i <= 10; i++){
if (*(i + a) == 0)
break;
printf(" %d ", *(i + a));
};
};
void delete(int *a){
int c;
int b;
printf("Enter the index at which you want to delete an element: ");
scanf("%d", &b);
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
c = traverse(a);
for (int i = b; i <= c; i++){
*(a + i) = *(a + i + 1);
};
printf("Array after deletion is: ");
for (int i = 0; i <= 10; i++){
if (*(i + a) == 0)
break;
printf(" %d ", *(i + a));
};
};

int main(){
int arr[10] = {23, 24, 63, 78, 53};
char a;
printf("Enter the operation you want t o perform in the array\n");
printf("Enter 'I' for insertion, 'D' for deletion and 'T' for traversal:");
scanf("%c", &a);

printf("Array is: ");


for (int i = 0; i < 10; i++){
if (arr[i] == 0)
break;
printf(" %d ", arr[i]);};
printf("\n");

switch (a){
case 'I':
insert(arr);
break;
case 'D':
delete(arr);
break;
case 'T':
traverse(arr);
break;
};
return 0;
};

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Experiment 2
Aim: To search an array for the given element using linear (or sequential) and binary search

Programming Assignment: Write a menu driven program in C which searches an array for
the input element.
Pre-Lab Work: Write the linear and binary search algorithms and compute their complexity

Program:

Linear search:-

#include <stdio.h>

int main(){
int size,arr[100],key,i;

printf("Enter the size of array: ");


scanf("%d",&size);

printf("Enter the elements of the array: ");


for(i=0;i<size;i++)
scanf("%d",&arr[i]);

printf("Enter the element to be searched: ");


scanf("%d",&key);

for(i=0;i<10;i++){
if((*(arr+i))==key){
printf("Element found at index %d\n",i);};
};
return 0;
};

Binary search:-

#include <stdio.h>

int main(){
int size,arr[100],key,i;
int low=0,high,mid;

printf("Enter the size of array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Enter the element to be searched: ");
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS

scanf("%d", &key);

high=size;

while(low<=high){
mid=(low+high)/2;

if (arr[mid]==key){
printf("Element found at index %d.\n",mid);
break;
}
else if(arr[mid]<key){
low=mid+1;
}
else{
high=mid-1;
}
};
return 0;
};

Output:

Linear search:-

Binary search:-

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Experiment 3
Aim: To sort an array using iterative sorting algorithms

Programming Assignment: Write a menu driven program in C which sorts an array using
selection, bubble and insertion sorting algorithms. The expectations from the program are as
follows:-
 The output of the program MUST be user friendly and self-explanatory.
 The program MUST display the array at the end of each iteration.
 Proper test cases MUST be designed to exhibit the best case, average case and worst-
case behavior of the algorithms.
Pre-Lab Work: Write selection, bubble and insertion sorting algorithms and compute their
complexity in best, worst and average cases.
Program:

#include <stdio.h>

void InsertionSort(int arr[]);


void SelectionSort(int arr[]);
void BubbleSort(int arr[]);
void PrintArray(int arr[]);

int main(){
int ar[5],ans;

printf("Enter array: ");


scanf("%d %d %d %d %d",&ar[0],&ar[1],&ar[2],&ar[3],&ar[4]);

printf("\nChoose type of sorting\n");


printf("1 -> Insertion Sort\n");
printf("2 -> Selection Sort\n");
printf("3 -> Bubble Sort\n");
scanf("%d",&ans);
switch(ans){
case 1:
InsertionSort(ar);
break;
case 2:
SelectionSort(ar);
break;
case 3:
BubbleSort(ar);
break;
}
return 0;
}

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

void PrintArray(int arr[]){


for(int i=0;i<5;i++)
printf("%d\t",arr[i]);
printf("\n");
}

void InsertionSort(int arr[]){


int i, key, j;
for (i=1;i<5;i++) {
key=arr[i];
j=i-1;
while(j>=0&&arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
PrintArray(arr);
}
printf("\nsorted array is :- ");
PrintArray(arr);
}

void SelectionSort(int arr[]){


int i, j, min_idx;
for (i = 0; i < 4; i++){
min_idx = i;
for (j = i+1; j < 5; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if(min_idx != i){
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
PrintArray(arr);
}
printf("\nsorted array is :- ");
PrintArray(arr);
}

void BubbleSort(int arr[]){


int i, j, swapped;
for (i = 0; i < 4; i++) {
swapped = 0;
for (j = 0; j < 4 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy
arr[jof+Technical Education, Noida
1] = temp; Department of CSDS

swapped = 1;
}
}
PrintArray(arr);
if (swapped == 0)
break;
}
printf("\nsorted array is :- ");
PrintArray(arr);
}

Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Experiment 4
Aim: To sort an array using recursive sorting algorithms

Programming Assignment: Modify the program of Experiment 3 to include Merge Sort


and Quick Sort Algorithms. The expectations from the program are as follows: -
 The output of the program MUST be user friendly and self-explanatory.
 The merged array MUST be displayed after each call to the merge function.
 The array after each partitioning step must be displayed.
 Proper test cases MUST be designed to exhibit the best case, average case and worst-
case behavior of the algorithms.
Pre-Lab Work: Write the Merge Sort and Quick Sort algorithms and compute their
complexity in best, worst and average cases.
Program:

#include <stdio.h>

void PrintArray(int arr[]);


void merge(int arr[], int l, int m, int r);
void mergeSort(int arr[], int l, int r);
int partition(int arr[], int low, int high);
void quickSort(int arr[], int low, int high);

int main(){
int ar[5],ans;

printf("Enter array: ");


scanf("%d %d %d %d %d",&ar[0],&ar[1],&ar[2],&ar[3],&ar[4]);

printf("\nChoose type of sorting\n");


printf("1 -> Merge Sort\n");
printf("2 -> Quick Sort\n");
scanf("%d",&ans);
switch(ans){
case 1:
mergeSort(ar,0,4);
break;
case 2:
quickSort(ar,0,4);
break;
}
printf("\nSorted array:\n");
PrintArray(ar);
return 0;
}

void PrintArray(int arr[]){


for(int i=0;i<5;i++)
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS

printf("%d\t",arr[i]);
printf("\n");
}

void merge(int arr[], int l, int m, int r) {


int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
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++;
}
PrintArray(arr);
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
printf("partition:\n");
PrintArray(arr);
merge(arr, l, m, r);
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS}}Academy of Technical Education, Noida Department of CSDS

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
PrintArray(arr);
}
}
int t = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = t;
PrintArray(arr);
return (i + 1);
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
printf("partition:\n");
PrintArray(arr);
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Experiment 5
Aim: To implement singly linked
lists
Programming Assignment: Write a program to implement singly linked lists using the
following functions
1. Create a linked list
2. Insert before a given item in the linked list
3. Delete an item from the linked list
4. Display the linked lit

Note:-

 The program should be menu driven. The menu should contain options for create,
insert, delete, display and quit.
 Make sure that create function should be called only once.
 Functions should be called on the basis of user’s choice till the user enters the choice
to quit.
Pre-Lab Work: Write the algorithms for 2, 3 and 4 functions and compute their complexity.

Program:

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

struct Node{
int data;
struct Node* next;
}*head,*second,*third,*fourth;

void print(struct Node* ptr){


printf("The list is: ");
while(ptr!=NULL){
printf(" %d ",ptr->data);
ptr=ptr->next;
};
printf("\n");
};

void traverse(struct Node* ptr){


while (ptr!=NULL){
ptr=ptr->next;
};
printf("Traverse complete!\n");
};

struct Node* insert_begin(struct Node* head,int i1){


struct Node* new=(struct Node*)malloc(sizeof(struct Node));
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
new->data=i1;
new->next=head;
return new;
};

struct Node* insert_specific(struct Node* head,int i1,int indexI){


struct Node* new=(struct Node*)malloc(sizeof(struct Node));
struct Node* ptr=head;
int i=1;
while(i!=indexI-1){
ptr=ptr->next;
i++;
};
new->data=i1;
new->next=ptr->next;
ptr->next=new;
return head;
};

struct Node* insert_end(struct Node* head,int i1){


struct Node* new=(struct Node*)malloc(sizeof(struct Node));
struct Node* ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
};
new->data=i1;
new->next=ptr->next;
ptr->next=new;
return head;
};

struct Node* delete_begin(struct Node* head){


struct Node* ptr =head;
head=head->next;
free(ptr);
return head;
};
struct Node* delete_specific(struct Node* head, int indexD){
struct Node* ptr =head;
int i=1;
while(i!=indexD-1){
ptr=ptr->next;
i++;
};
struct Node* p=ptr->next;
ptr->next=p->next;
free(p);
return head;};
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
struct Node* delete_end(struct Node* head){
struct Node* ptr=head;
struct Node* p=head->next;
while(p->next!=NULL){
ptr=ptr->next;
p=p->next;
};
ptr->next=p->next;
free(p);
return head;
};

int main(){
head=(struct Node*)malloc(sizeof(struct Node));
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));
fourth=(struct Node*)malloc(sizeof(struct Node));

head->data=29;
head->next=second;
second->data=45;
second->next=third;
third->data=21;
third->next=fourth;
fourth->data=82;
fourth->next=NULL;

char x='y';
while(x=='y'){
printf("The list before any operation is: \n");
print(head);

char a;
printf("Enter the operation to be performed in linklist\n");
printf("Enter T for traversal\n");
printf("Enter I for insertion\n");
printf("Enter D for deletion\n");
scanf("%c",&a);

switch(a){
case'T':
printf("We are initiating traversal.\n");
traverse(head);
break;

case'I':
int b;
printf("We are performing insertion\n");

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
printf("Enter 1 to insert in beginning\n");
printf("Enter 2 to insert in specific location\n");
printf("Enter 3 to insert at last\n");
scanf("%d",&b);
int i1;
printf("Enter the value to be inserted: ");
scanf("%d",&i1);
int indexI;

switch(b){
case 1:
printf("You choose to insert at beginning of list\n");
head=insert_begin(head,i1);
print(head);
break;

case 2:
printf("You choose to insert at specific location of list\n");
printf("Enter the index at which you want to insert the given value\n");
scanf("%d",&indexI);
head=insert_specific(head,i1,indexI);
print(head);
break;

case 3:
printf("You choose to insert at end of list\n");
head=insert_end(head,i1);
print(head);
break;

default: printf("Invalid input insert");


break;
}
break;

case'D':
int e;
printf("We are performing deletion\n");
printf("Enter 1 to delete from beginning\n");
printf("Enter 2 to delete from specific\n");
printf("Enter 3 to delete from last\n");
scanf("%d",&e);
int indexD;

switch(e){
case 1:
printf("You choose to delete from beginning of list\n");
head=delete_begin(head);

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
print(head);
break;

case 2:
printf("You choose to delete at specific location of list\n");
printf("Enter the index at which you want to delete the given value\n");
scanf("%d",&indexD);
head=delete_specific(head,indexD);
print(head);
break;

case 3:
printf("You choose to delete at end of list\n");
head=delete_end(head);
print(head);
break;

default: printf("Invalid input delete");


break;
};
break;
default: printf("Invalid input outer");
};
fflush(stdin);
printf("\nDo you want to repeat any operation?\n");
printf("Enter 'y' for yes and 'n' for no.\n");
scanf("%c",&x);
printf("\n");

fflush(stdin);
};

printf("\nEnd of program\n");
};

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Experiment 6
Aim: To implement stack operations using linked lists

Programming Assignment: Write a program to implement a stack of integers using linked


lists. The program should contain following functions
1. IsEmpty() – to check if the stack is empty
2. Push an item on stack
3. Pop an item from stack
4. Display the items of the stack from top to bottom of the stack.

Note:-

 The program should be menu driven. The menu should contain options for calling
the functions written by you repeatedly till the option to quit is chosen.
Pre-Lab Work: How is a stack implemented using linked lists?

Program:

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

struct node{
int data;
struct node* next;
}*top;

void print(struct node* top){


int a[100],i=0;
struct node* ptr=top;
printf("The stack is: ");
while(ptr!=NULL && i!=99){
a[i]=ptr->data;
i++;
ptr=ptr->next;
};
for(i=i-1;i>=0;i--){
printf(" %d ",a[i]);
};
printf("\n");
};

struct node* pop(struct node* head){


struct node* ptr=head;
head=head->next;
free(ptr);
return head;
};

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
struct node* push(struct node* head, int a){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->data=a;
new->next=head;
return new;
};

void main()
{
top=(struct node*)malloc(sizeof(struct node));
top->data=35;
top->next=NULL;

printf("Initially the stack has only one element\n");

int x=1;
while(x==1){
printf("Choose the operation you want to perform from below:\n");
printf("1.PUSH\n2.POP\n3.Display\n");
printf("Enter here:");
int c;
scanf("%d",&c);
switch(c){
case 3: print(top);
break;
case 2: top=pop(top);
print(top);
break;
case 1: int a;
printf("Enter the number you want to push:");
scanf("%d",&a);
top=push(top,a);
print(top);
break;
default: printf("Invalid Input");
};
fflush(stdin);

printf("\nDo you want to repeat any operation?\n");


printf("Enter '1' for yes and '0' for no.\n");

scanf("%d",&x);
fflush(stdin);
};
printf("End of program");
};

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Experiment 7
Aim: To implement queues using arrays.

Programming Assignment: Write a program to implement a queue of alphabets using


circular arrays. The program should contain following functions
1. IsEmpty() – to check if the queue is empty
2. Add an item to the queue
3. Remove an item from the queue
4. Display the items of the queue from first to last.

Note:-

 The program should be menu driven. The menu should contain options for calling the
functions written by you repeatedly till the option to quit is chosen.
Pre-Lab Work: How are queues implemented using arrays? What are the conditions for
overflow and underflow?
Program:

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

int queue[100]={43},front=0,rear=0;
int c;

void enque(){
if(rear==99){
printf("The queue is full\n");
return;
};
int e;
printf("Enter the element to be inserted: ");
scanf("%d",&e);
if(front==-1){
front=0;
rear=0;
queue[rear]=e;
return;
};
rear=rear+1;
queue[rear]=e;
};

void deque(){
if(front==-1){
printf("The queue is empty\n");
return;
};
front=front+1;
};
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
void display(){
printf("The elements of queue are: ");
for(int i=front;i<=rear;i++){
printf(" %d",queue[i]);
};
printf("\n");
};

int main()
{
printf("The maximum elements the queue can accomodate = 100 \n");
printf("Initiatlly the queue has one element\n");
display();
int x=1;
while(x==1){
printf("The list of operations are given below to be performed\n");
printf("Enter the respective number for below operations\n");
printf("1. Enque\n2. Deque\n\n");
printf("Enter here: ");
scanf("%d",&c);

switch(c){
case 1:
enque();
display();
break;
case 2:
deque();
display();
break;
default:
printf("Invalid input");
break;
};

fflush(stdin);
printf("\nDo you want to repeat any operation?\n");
printf("Type '1' for yes and '0' for no.\n");
scanf("%d",&x);
printf("\n");
fflush(stdin);
};
printf("End of program");
return 0;
};

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Experiment 8

Aim: To implement binary search tree.


Programming Assignment: Write a program to implement a binary search tree containing
numbers as key elements using linked implementation. The program should contain following
functions.
1. In-order traversal
2. Pre-order traversal
3. Post-order traversal
4. Insert a key
5. Delete a key

Note:-
 The program should be menu driven. The menu should contain options for calling the
functions written by you repeatedly till the option to quit is chosen.
Pre-Lab Work: What are the cases encountered during deletion of a key from a BST? How are
they handled?
Program:

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

struct Node {
int data;
struct Node *left;
struct Node *right;
};

struct Node* createNode(int data)


struct Node* insert(struct Node* root, int data)
void inorder(struct Node* root)
void preorder(struct Node* root)
void postorder(struct Node* root)
struct Node* minValueNode(struct Node* node)
struct Node* deleteNode(struct Node* root, int key)

int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Inorder Traversal\n");
printf("4. Preorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 4:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 5:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");}}
return 0;}

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;}
struct Node* insert(struct Node* root, int data) {
if (root == NULL)
return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;}
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}}
void preorder(struct Node* root) {
if (root != NULL) {
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS

printf("%d ", root->data);


preorder(root->left);
preorder(root->right);
}}
void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}}
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;}
struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL)
return root;
if (key < root->data)
root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;}
struct Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);}
return root;}

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Experiment 9

Aim: To implement graphs &


traversal.
Programming Assignment: Write a program to implement a graphs containing numbers as key
elements using adjacency lists and adjacency matrix. Write functions for breadth first and depth
first graph traversal.
Pre-Lab Work:
1) Compare the two graph representation methods
2) Differentiate between BFS and DFS of graphs

Program:

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

#define MAX_VERTICES 100

struct Node {
int data;
struct Node* next;
};

struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};

struct Node* createNode(int data);


struct Graph* createGraph(int vertices);
void addEdge(struct Graph* graph, int src, int dest);
void printGraph(struct Graph* graph);
void DFS(struct Graph* graph, int vertex);
void BFS(struct Graph* graph, int startVertex);

int main() {
struct Graph* graph = createGraph(5);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);

printf("Graph:\n");
printGraph(graph);
printf("\nDFS Traversal:\n");
DFS(graph, 0);
// Resetting visited array for BFS
int i;
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSSfor
Academy
(i = 0; i <ofgraph->numVertices;
Technical Education,i++)
Noida
{ Department of CSDS

graph->visited[i] = 0;}
printf("\nBFS Traversal:\n");
BFS(graph, 0);
return 0;
}
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists = (struct Node**)malloc(vertices * sizeof(struct Node*));
graph->visited = (int*)malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;}
return graph;
}
void addEdge(struct Graph* graph, int src, int dest) {
struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;}
void printGraph(struct Graph* graph) {
int v;
for (v = 0; v < graph->numVertices; v++) {
struct Node* temp = graph->adjLists[v];
printf("Vertex %d: ", v);
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;}
printf("NULL\n");}}

void DFS(struct Graph* graph, int vertex) {


struct Node* adjList = graph->adjLists[vertex];
struct Node* temp = adjList;
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
while (temp != NULL) {
int connectedVertex = temp->data;
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);}
temp = temp->next;}}

void BFS(struct Graph* graph, int startVertex) {


Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSSstruct
Academy
Node*ofqueue[MAX_VERTICES];
Technical Education, Noida Department of CSDS
int front = 0, rear = 0;

graph->visited[startVertex] = 1;
queue[rear++] = graph->adjLists[startVertex];
while (front < rear) {
struct Node* currentNode = queue[front++];
printf("Visited %d \n", currentNode->data);
while (currentNode) {
int adjVertex = currentNode->data;
if (graph->visited[adjVertex] == 0) {
queue[rear++] = graph->adjLists[adjVertex];
graph->visited[adjVertex] = 1;}
currentNode = currentNode->next;
}}}

Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092


JSS Academy of Technical Education, Noida Department of CSDS
Experiment 10
Aim: To find minimum spanning tree of given graph

Programming Assignment: Write a program to find MST of a given graph.

Pre-Lab Work:
1) What is minimum spanning tree?
2) Differentiate between Prim’s and Kruskal’s algorithms

Program:

#include <stdio.h>
#include <limits.h>

#define V 5

int minKey(int key[], int mstSet[]);


void printMST(int parent[], int graph[V][V]);
void primMST(int graph[V][V]);

int main() {
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primMST(graph);
return 0;
}
int minKey(int key[], int mstSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;
return min_index;}

void printMST(int parent[], int graph[V][V]) {


printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);}

void primMST(int graph[V][V]) {


int parent[V];
int key[V];
int mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
mstSet[u] = 1;
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];}
printMST(parent, graph);}

Output:

Data Structures Lab (BCS 351) Sajal Singhal 2200911540092

You might also like