DSA Lab manual final
DSA Lab manual final
Laboratory Manual
on
Semester – III
BACHELOR OF ENGINEERING
IN
ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING
Prepared by
Mr. VIGNESH T V
Assistant Professor
Department of AI&ML,
CIT, Ponnampet
VISION OF THE INSTITUTE
Enabling the students to morally and ethically achieve the academic excellence in artificial
intelligence and machine learning for digital empowerment.
At the end of the B.E program, students are expected to have developed the following outcomes.
1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialisation to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, research literature, and analyse complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex engineering activities
with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal, and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
7. Environment and Sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of need for
sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognise the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
PROGRAM EDUCATIONAL OBJECTIVES (PEO)
➢ PEO1: Display the exploratory and analytical skills by developing practical solutions
for real-world challenges encountered in the field of Artificial Intelligence and Machine
Learning.
➢ PEO2: Involve in research and construct intelligent systems in various domains,
including machine learning, deep learning, computer vision, and natural language
processing. Contribute to advancing, knowledge in the field of Artificial Intelligence and
Machine Learning.
➢ PEO3: Demonstrate a professional and ethical mind set while addressing challenges
with social awareness. Showcase strong abilities in cooperation, communication, and
leadership.
6. Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
7. Develop a menu driven Program in C for the following operations on Singly Linked List
(SLL) of Student Data with the fields: USN, Name, Programme, Sem,
PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
8. Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,
Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
9. Develop a Program in C for the following operationson Singly Circular Linked List (SCLL)
with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
10. Develop a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
11. Develop a Program in C for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method
15.09.2023
14.09.2023
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function H:
K →L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.
Laboratory Outcomes: The student should be able to:
Sample Output:
Enter details for Day 1:
Enter day name: Monday
Enter date: 25
Enter activity: Meeting with the team
Enter details for Day 2:
Enter day name: Tuesday
Enter date: 26
Enter activity: Project planning
Enter details for Day 3:
Enter day name: Wednesday
Enter date: 27
Enter activity: Coding session
Enter details for Day 4:
Enter day name: Thursday
Enter date: 28
Enter activity: Code review
Department of AI&ML, CIT - Ponnampet Page 2
Data Structures Laboratory (BCSL305)
Enter details for Day 5:
Enter day name: Friday
Enter date: 29
Enter activity: Team discussion
Enter details for Day 6:
Enter day name: Saturday
Enter date: 30
Enter activity: Weekend planning
Enter details for Day 7:
Enter day name: Sunday
Enter date: 31
Enter activity: Family time
******* CALENDAR DETAILS *******
Day 1:
Name: Monday
Date: 25
Activity: Meeting with the team
Day 2:
Name: Tuesday
Date: 26
Activity: Project planning
Day 3:
Name: Wednesday
Date: 27
Activity: Coding session
Day 4:
Name: Thursday
Date: 28
Activity: Code review
Day 5:
Name: Friday
Date: 29
Activity: Team discussion
Day 6:
Name: Saturday
Department of AI&ML, CIT - Ponnampet Page 3
Data Structures Laboratory (BCSL305)
Date: 30
Activity: Weekend planning
Day 7:
Name: Sunday
Date: 31
Activity: Family time
Sample Output:
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
1
Enter element to push
10
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
1
Enter element to push
20
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
1
Enter element to push
10
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
4
Contents of stack:
10
20
10
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
3
It is a palindrome
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
Department of AI&ML, CIT - Ponnampet Page 9
Data Structures Laboratory (BCSL305)
2
Popped element = 10
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
4
Contents of stack:
20
10
1: push 2: pop 3: palindrome 4: display 5: exit
Enter your choice
3
Not a palindrome
#include<stdio.h>
#include<ctype.h>
#define MAX 50
char infix[MAX], postfix[MAX], s[MAX], operator;
int i, j = 0, top = -1;
void push(char operator);
char pop();
void convert();
int priority(char operator);
void main() {
printf("Enter infix expression\n");
scanf("%s", infix);
convert();
}
void push(char operator) {
if (top == MAX - 1)
printf("Stack overflow\n");
else {
top++;
s[top] = operator;
}
}
char pop() {
if (top == -1)
printf("Stack underflow\n");
else
return s[top--];
}
int priority(char operator) {
switch (operator) {
Sample Output:
Enter infix expression
A+(B*C-(D/E^F)*G)*H
Postfix expression = ABC*DEF^/G*-H*+
Sample Output:
Enter postfix expression
23*54*+
Result = 23.000000
Sample Output:
Enter the number of discs: 3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are: 7
Sample Output:
1:insert 2:delete 3:display 4:exit
A B C
Deleted element = A
B C
Deleted element = B
#include<stdio.h>
#include<stdlib.h>
struct list {
char usn[20], name[20], branch[20], phonenumber[20];
int sem;
struct list* link;
};
typedef struct list* NODE;
NODE first = NULL;
int n, i, c = 0, ch;
void create();
void read(NODE temp);
void ins_front();
void ins_rear();
void del_front();
void del_rear();
void display();
int main() {
for(;;) {
printf("1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("Enter number of student info: ");
scanf("%d", &n);
Department of AI&ML, CIT - Ponnampet Page 22
Data Structures Laboratory (BCSL305)
create();
break;
case 2:
display();
printf("Total number of nodes = %d\n", c);
break;
case 3:
n = 1;
create();
break;
case 4:
ins_rear();
break;
case 5:
del_front();
break;
case 6:
del_rear();
break;
case 7:
exit(0);
default:
printf("Wrong choice\n");
}
}
return 0;
}
void read(NODE temp) {
printf("Enter USN: ");
scanf("%s", temp->usn);
printf("Enter name: ");
scanf("%s", temp->name);
printf("Enter branch: ");
scanf("%s", temp->branch);
printf("Enter semester: ");
Department of AI&ML, CIT - Ponnampet Page 23
Data Structures Laboratory (BCSL305)
scanf("%d", &temp->sem);
printf("Enter phone number: ");
scanf("%s", temp->phonenumber);
c++;
}
void create() {
NODE temp;
for(i = 0; i < n; i++) {
if((temp = (NODE)malloc(sizeof(struct list))) == NULL) {
printf("Insufficient memory\n");
exit(0);
}
read(temp);
temp->link = first;
first = temp;
}
}
void display() {
NODE cur;
if(first == NULL) {
printf("List is empty\n");
} else {
printf("USN\tName\tBranch\tSemester\tPhone Number\n");
cur = first;
while(cur != NULL) {
printf("%s\t%s\t%s\t%d\t%s\n", cur->usn, cur->name, cur->branch, cur->sem, cur-
>phonenumber);
cur = cur->link;
}
}
}
void ins_rear() {
NODE cur, temp;
if((temp = (NODE)malloc(sizeof(struct list))) == NULL) {
printf("Insufficient memory\n");
Department of AI&ML, CIT - Ponnampet Page 24
Data Structures Laboratory (BCSL305)
exit(0);
}
read(temp);
temp->link = NULL;
if(first == NULL) {
first = temp;
} else {
cur = first;
while(cur->link != NULL) {
cur = cur->link;
}
cur->link = temp;
}
}
void del_front() {
NODE cur;
if(first == NULL) {
printf("List is empty\n");
} else {
cur = first;
first = first->link;
printf("One info deleted\n");
c--;
free(cur);
}
}
void del_rear() {
NODE cur, prev;
if(first == NULL) {
printf("List is empty\n");
} else if(first->link == NULL) {
cur = first;
first = first->link;
printf("Info deleted\n");
c--;
Department of AI&ML, CIT - Ponnampet Page 25
Data Structures Laboratory (BCSL305)
free(cur);
} else {
cur = first;
while(cur->link != NULL) {
prev = cur;
cur = cur->link;
}
prev->link = NULL;
printf("One node deleted\n");
c--;
free(cur);
}
}
Sample Output:
1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit
Enter your choice: 1
Enter number of student info: 2
Enter USN: 12345
Enter name: John
Enter branch: CSE
Enter semester: 3
Enter phone number: 9876543210
Enter USN: 67890
Enter name: Alice
Enter branch: IT
Enter semester: 2
Enter phone number: 1234567890
1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit
Enter your choice: 2
USN Name Branch Semester Phone Number
67890 Alice IT 2 1234567890
12345 John CSE 3 9876543210
Total number of nodes = 2
#include<stdio.h>
#include<stdlib.h>
struct list {
char ssn[11];
char name[20];
char dept[20];
char dsg[20];
float sal;
char phno[11];
struct list *llink, *rlink;
};
typedef struct list* NODE;
NODE first = NULL;
void read(NODE temp);
void ins_r();
void ins_f();
void del_f();
void del_r();
void display();
int i, n, ch, c = 0;
int main() {
for (;;) {
printf("1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit\n");
printf("enter your choice\n");
Department of AI&ML, CIT - Ponnampet Page 28
Data Structures Laboratory (BCSL305)
scanf("%d", &ch);
switch (ch) {
case 1:
printf("enter n\n");
scanf("%d", &n);
ins_r();
break;
case 2:
display();
printf("total no. of nodes = %d\n", c);
break;
case 3:
ins_f();
break;
case 4:
n = 1;
ins_r();
break;
case 5:
del_f();
break;
case 6:
del_r();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
}
}
return 0;
}
void read(NODE temp) {
printf("enter ssn\n");
scanf("%s", temp->ssn);
Department of AI&ML, CIT - Ponnampet Page 29
Data Structures Laboratory (BCSL305)
printf("enter name\n");
scanf("%s", temp->name);
printf("enter department\n");
scanf("%s", temp->dept);
printf("enter designation\n");
scanf("%s", temp->dsg);
printf("enter salary\n");
scanf("%f", &temp->sal);
printf("enter phonenumber\n");
scanf("%s", temp->phno);
c++;
}
void ins_r() {
NODE temp, cur;
for (i = 0; i < n; i++) {
if ((temp = (NODE)malloc(sizeof(struct list))) == NULL) {
printf("insufficient memory\n");
exit(0);
} else {
read(temp);
temp->llink = temp->rlink = NULL;
if (first == NULL) {
first = temp;
} else {
cur = first;
while (cur->rlink != NULL) {
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
}
}
}
}
Sample Output:
1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit
Enter your choice: 1
Enter n: 2
Enter SSN: 1234567890
Enter Name: John
Department of AI&ML, CIT - Ponnampet Page 32
Data Structures Laboratory (BCSL305)
Enter Department: IT
Enter Designation: Manager
Enter Salary: 50000
Enter Phone Number: 9876543210
Enter SSN: 9876543210
Enter Name: Alice
Enter Department: HR
Enter Designation: Assistant
Enter Salary: 30000
Enter Phone Number: 1234567890
1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit
Enter your choice: 2
SSN Name Dept Designation Salary Phone
1234567890 John IT Manager 50000 9876543210
9876543210 Alice HR Assistant 30000 1234567890
Total number of nodes = 2
1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit
Enter your choice: 3
Enter SSN: 5678901234
Enter Name: Bob
Enter Department: Finance
Enter Designation: Clerk
Enter Salary: 25000
Enter Phone Number: 1122334455
1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit
Enter your choice: 2
SSN Name Dept Designation Salary Phone
5678901234 Bob Finance Clerk 25000 1122334455
1234567890 John IT Manager 50000 9876543210
9876543210 Alice HR Assistant 30000 1234567890
Total number of nodes = 3
1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit
Enter your choice: 5
One info del
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct list {
int coef, expx, expy, expz;
struct list *link;
};
typedef struct list *poly;
poly p1, p2, p3;
poly add();
void disp(poly p);
poly read();
void eval();
void copy(poly s, poly d);
int main() {
int ch;
for (;;) {
printf("1: evaluation 2: addition 3: exit\n");
printf("enter your choice\n");
scanf("%d", &ch);
switch(ch) {
case 1:
p1 = read();
eval();
break;
case 2:
printf("enter first polynomial in decreasing order of exponent\n");
Department of AI&ML, CIT - Ponnampet Page 35
Data Structures Laboratory (BCSL305)
p1 = read();
printf("enter second polynomial in decreasing order of exponent\n");
p2 = read();
printf("first polynomial is\n");
disp(p1);
printf("second polynomial is\n");
disp(p2);
p3 = add();
printf("sum of polynomials is\n");
disp(p3);
break;
case 3:
exit(0);
default:
printf("wrong choice\n");
}
}
return 0;
}
poly read() {
poly h, temp, last;
int n, i;
printf("enter number of terms in the polynomial\n");
scanf("%d", &n);
h = (poly)malloc(sizeof(struct list));
h->coef = 0;
h->expx = h->expy = h->expz = -1;
last = h;
for(i = 1; i <= n; i++) {
temp = (poly)malloc(sizeof(struct list));
printf("enter the coefficient of term %d: ", i);
scanf("%d", &temp->coef);
printf("enter the exponent value of x: ");
scanf("%d", &temp->expx);
printf("enter the exponent value of y: ");
Department of AI&ML, CIT - Ponnampet Page 36
Data Structures Laboratory (BCSL305)
scanf("%d", &temp->expy);
printf("enter the exponent value of z: ");
scanf("%d", &temp->expz);
last->link = temp;
last = temp;
}
last->link = h;
return h;
}
void disp(poly p) {
poly temp;
temp = p->link;
while (temp != p) {
printf("%dx^%dy^%dz^%d", temp->coef, temp->expx, temp->expy, temp->expz);
temp = temp->link;
if (temp != p) {
printf(" + ");
}
}
printf("\n");
}
void eval() {
poly temp;
int res = 0, x, y, z;
temp = p1->link;
printf("enter the values for x, y, and z\n");
scanf("%d%d%d", &x, &y, &z);
while (temp != p1) {
res += temp->coef * pow(x, temp->expx) * pow(y, temp->expy) * pow(z, temp-
>expz);
temp = temp->link;
}
printf("result = %d\n", res);
}
void copy(poly s, poly d) {
Department of AI&ML, CIT - Ponnampet Page 37
Data Structures Laboratory (BCSL305)
d->coef = s->coef;
d->expx = s->expx;
d->expy = s->expy;
d->expz = s->expz;
}
poly add() {
poly temp, c, last;
c = (poly)malloc(sizeof(struct list));
c->coef = 0;
c->expx = c->expy = c->expz = -1;
last = c;
p1 = p1->link;
p2 = p2->link;
while(1) {
temp = (poly)malloc(sizeof(struct list));
if (p1->expx == p2->expx) {
if (p1->expy == p2->expy) {
if (p1->expz == p2->expz) {
if ((p1->expx == -1) && (p2->expx == -1) && (p1->expy == -1) && (p2-
>expy == -1) && (p1->expz == -1) && (p2->expz == -1)) {
free(temp);
break;
} else {
temp->coef = p1->coef + p2->coef;
temp->expx = p1->expx;
temp->expy = p1->expy;
temp->expz = p1->expz;
p1 = p1->link;
p2 = p2->link;
}
} else if (p1->expz > p2->expz) {
copy(p1, temp);
p1 = p1->link;
} else {
copy(p2, temp);
Department of AI&ML, CIT - Ponnampet Page 38
Data Structures Laboratory (BCSL305)
p2 = p2->link;
}
} else if (p1->expy > p2->expy) {
copy(p1, temp);
p1 = p1->link;
} else {
copy(p2, temp);
p2 = p2->link;
}
} else if (p1->expx > p2->expx) {
copy(p1, temp);
p1 = p1->link;
} else {
copy(p2, temp);
p2 = p2->link;
}
last->link = temp;
last = temp;
}
last->link = c;
return c;
}
Sample Output:
1: evaluation 2: addition 3: exit
enter your choice
1
enter number of terms in the polynomial
2
enter the coefficient of term 1: 3
enter the exponent value of x: 2
enter the exponent value of y: 1
enter the exponent value of z: 0
enter the coefficient of term 2: 5
enter the exponent value of x: 1
enter the exponent value of y: 2
Department of AI&ML, CIT - Ponnampet Page 39
Data Structures Laboratory (BCSL305)
enter the exponent value of z: 1
enter the values for x, y, and z
111
result = 18
1: evaluation 2: addition 3: exit
enter your choice
2
enter first polynomial in decreasing order of exponent
enter number of terms in the polynomial
2
enter the coefficient of term 1: 4
enter the exponent value of x: 2
enter the exponent value of y: 0
enter the exponent value of z: 1
enter the coefficient of term 2: 2
enter the exponent value of x: 1
enter the exponent value of y: 2
enter the exponent value of z: 0
enter second polynomial in decreasing order of exponent
enter number of terms in the polynomial
2
enter the coefficient of term 1: 3
enter the exponent value of x: 2
enter the exponent value of y: 1
enter the exponent value of z: 0
enter the coefficient of term 2: 1
enter the exponent value of x: 1
enter the exponent value of y: 2
enter the exponent value of z: 1
first polynomial is
4x^2y^0z^1 + 2x^1y^2z^0
second polynomial is
3x^2y^1z^0 + 1x^1y^2z^1
sum of polynomials is
7x^2y^0z^1 + 3x^2y^1z^0 + 3x^1y^2z^1
Department of AI&ML, CIT - Ponnampet Page 40
Data Structures Laboratory (BCSL305)
Program 10:
Develop a menu driven Program in C for the following operations on Binary Search
Tree (BST) of Integers.
a) Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2.
b) Traverse the BST in Inorder, Preorder and Post Order.
c) Search the BST for a given element (KEY) and report the appropriate
message.
d) Exit.
#include<stdio.h>
#include<stdlib.h>
struct list {
int info;
struct list *llink, *rlink;
};
typedef struct list* NODE;
NODE root = NULL;
void create();
void search();
void inorder(NODE temp);
void preorder(NODE temp);
void postorder(NODE temp);
int main() {
int ch;
for (;;) {
printf("1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit\n");
printf("enter your choice\n");
scanf("%d", &ch);
switch(ch) {
case 1:
create();
break;
case 2:
inorder(root);
Sample Output:
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
6
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
9
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
Department of AI&ML, CIT - Ponnampet Page 44
Data Structures Laboratory (BCSL305)
5
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
2
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
8
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
15
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
24
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
14
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
7
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
Department of AI&ML, CIT - Ponnampet Page 45
Data Structures Laboratory (BCSL305)
8
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
5
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
1
enter the data
2
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
2
2 2 5 5 6 7 8 8 9 14 15 24
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
3
6 5 2 2 5 9 8 7 8 15 14 24
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
4
2 2 5 5 7 8 8 9 14 24 15 6
1: create 2: inorder 3: preorder 4: postorder 5: search 6: exit
enter your choice
5
enter the key to search
8
key found
#include<stdio.h>
#include<stdlib.h>
int a[50][50], n, visited[50];
int q[20], front = -1, rear = -1;
int s[20], top = -1, count = 0;
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while (front != rear)
{
cur = q[++front];
for (i = 1; i <= n; i++)
{
if ((a[cur][i] == 1) && (visited[i] == 0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}
void dfs(int v)
{
int i;
visited[v] = 1;
s[++top] = v;
Department of AI&ML, CIT - Ponnampet Page 47
Data Structures Laboratory (BCSL305)
for (i = 1; i <= n; i++)
{
if (a[v][i] == 1 && visited[i] == 0)
{
printf("%d ", i);
dfs(i);
}
}
}
int main()
{
int ch, start, i, j;
printf("\nEnter the number of vertices in graph:");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}
for (i = 1; i <= n; i++)
visited[i] = 0;
printf("\nEnter the starting vertex: ");
scanf("%d", &start);
printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for (i = 1; i <= n; i++)
Department of AI&ML, CIT - Ponnampet Page 48
Data Structures Laboratory (BCSL305)
{
if (visited[i] == 0)
printf("\nThe vertex that is not reachable is %d", i);
}
break;
case 2:
printf("\nNodes reachable from starting vertex %d are:\n", start);
dfs(start);
break;
case 3:
exit(0);
default:
printf("\nPlease enter valid choice:");
}
return 0;
}
Sample Output:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0101
1010
0101
1010
Enter the starting vertex: 1
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Nodes reachable from starting vertex 1 are: 2 4 3
The vertex that is not reachable is 1
#include<stdio.h>
#include<stdlib.h>
void insert();
void display();
void init();
int a[100], hk, key, choice, i, flag;
int main() {
init();
for (;;) {
printf("1:insert 2:display 3:exit\n");
printf("enter your choice\n");
scanf("%d", &choice);
switch(choice) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("invalid choice\n");
}
}
return 0;
Department of AI&ML, CIT - Ponnampet Page 51
Data Structures Laboratory (BCSL305)
}
void init() {
for(i = 0; i < 100; i++)
a[i] = -1;
}
void insert() {
printf("enter the key value\n");
scanf("%d", &key);
hk = key % 10;
if(a[hk] == -1)
a[hk] = key;
else {
for(i = hk + 1; i < 100; i++) {
if(a[i] == -1) {
a[i] = key;
return;
}
}
for(i = 0; i < hk; i++) {
if(a[i] == -1) {
a[i] = key;
return;
}
}
}
}
void display() {
printf("index: key\n");
for(i = 0; i < 100; i++)
printf("%d:%d\n", i, a[i]);
}