0% found this document useful (0 votes)
9 views

DSA Lab manual final

DSA lab manual ....

Uploaded by

Niranjan B.A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DSA Lab manual final

DSA lab manual ....

Uploaded by

Niranjan B.A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Kodava Education Society®

COORG INSTITUTE OF TECHNOLOGY


(Accredited by NAAC ‘A’ Grade, Approved by the AICTE, New Delhi & Affiliated to VTU, Belagavi)
Ponnampet-571216

DEPARTMENT OF ARTIFICIAL INTELLIGENCE & MACHINE LEARNING

Laboratory Manual
on

DATA STRUCTURES LABORATORY


(BCSL305)

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

To evolve as a Centre of Excellence in technical education by imparting quality education,


focusing on creativity, innovation and entrepreneurial skills.

MISSION OF THE INSTITUTE

➢ Delivering quality education through a conducive learning atmosphere.


➢ Providing professional skillsets through training and value-added courses to meet
industry demand.
➢ Encouraging students to inculcate integrity, responsibility, and social awareness.

VISION OF THE DEPARTMENT

Enabling the students to morally and ethically achieve the academic excellence in artificial
intelligence and machine learning for digital empowerment.

MISSION OF THE DEPARTMENT

➢ To provide knowledge on capabilities of AI & ML systems through effective curriculum


delivery.
➢ Providing technical training in the subject amongst the students to meet industrial
requirements adhering to the existing norms of the industry.
PROGRAM OUTCOMES (PO)

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.

PROGRAM SPECIFIC OUTCOMES (PSO)

A graduate of the Artificial Intelligence and Machine Learning program will


demonstrate:
➢ PSO1: Develop intelligent systems by showcasing comprehension of human cognition,
machine learning, and data engineering.
➢ PSO2: Apply learning algorithms and methodologies to interpret, analyse, design and
implement Artificial Intelligence-driven solutions that benefit.
15.09.2023
14.09.2023

DATA STRUCTURES LABORATORY


SEMESTER – III

Course Code BCSL305 CIE Marks 50


Number of Contact Hours/Week 0:0:2 SEE Marks 50
Total Number of Lab Contact Hours 28 Exam Hours 03
Credits – 1
Course Learning Objectives:
This laboratory course enables students to get practical experience in design, develop, implement, analyze
and evaluation/testing of
● Dynamic memory management
● Linear data structures and their applications such as stacks, queues and lists
● Non-Linear data structures and their applications such as trees and graphs

Descriptions (if any):


● Implement all the programs in “C ” Programming Language and Linux OS.
Programs List:
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent
7 days of a week. Each Element of the array is a structure having three fields. The first
field is the name of the Day (A dynamically allocated String), The second field is the
date of the Day (A integer), the third field is the description of the activity for a
particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.

2. Develop a Program in C for the following operations on Strings.


a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR
Support the program with functions for each of the above operations. Don't use Built-in
functions.
3. Develop a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
15.09.2023
14.09.2023

4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program


should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric
operands.
5. Develop a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,
^
b. Solving Tower of Hanoi problem with n disks

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:

● Analyze various linear and non-linear data structures


● Demonstrate the working nature of different types of data structures and their applications
● Use appropriate searching and sorting algorithms for the give scenario.
● Apply the appropriate data structure for solving real world problems
Conduct of Practical Examination:
● Experiment distribution
o For laboratories having only one part: Students are allowed to pick one experiment from
the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal opportunity.
● Change of experiment is allowed only once and marks allotted for procedure to be made zero of
the changed part only.
● Marks Distribution (Need to change in accordance with university regulations)
c) For laboratories having only one part – Procedure + Execution + Viva-Voce: 15+70+15 =
100 Marks
d) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks
Data Structures Laboratory (BCSL305)
Program 01:
Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to
represent 7 days of a week. Each Element of the array is a structure having
three fields. The first field is the name of the Day (A dynamically allocated
String), The second field is the date of the Day (A integer), the third field is the
description of the activity for a particular day (A dynamically allocated
String).
b) Write functions create(), read() and display(); to create the calendar, to read
the data from the keyboard and to print weeks activity details report on screen.
#include <stdio.h>
#include <stdlib.h>
struct cal {
char *dayName;
int date;
char *activity;
};
struct cal *day[7];
void create(int i) {
day[i] = (struct cal *)malloc(sizeof(struct cal));
day[i]->dayName = (char *)malloc(sizeof(char) * 20);
day[i]->activity = (char *)malloc(sizeof(char) * 100);
}
void read() {
for (int i = 0; i < 7; i++) {
printf("\nEnter details for Day %d:\n", i + 1);
create(i);
printf("Enter day name: ");
scanf("%s", day[i]->dayName);
printf("Enter date: ");
scanf("%d", &(day[i]->date));
printf("Enter activity: ");
scanf("%s", day[i]->activity);
}
}
Department of AI&ML, CIT - Ponnampet Page 1
Data Structures Laboratory (BCSL305)
void display() {
printf("******* CALENDAR DETAILS *******\n");
for (int i = 0; i < 7; i++) {
printf("\nDay %d:\n", i + 1);
printf("Name: %s\n", day[i]->dayName);
printf("Date: %d\n", day[i]->date);
printf("Activity: %s\n", day[i]->activity);
printf("\n");
}
}
void main() {
read();
display();
for (int i = 0; i < 7; i++) {
free(day[i]);
}
}

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

Department of AI&ML, CIT - Ponnampet Page 4


Data Structures Laboratory (BCSL305)
Program 02:
Develop a Program in C for the following operations on Strings.
a) Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b) Perform Pattern Matching Operation: Find and Replace all occurrences of
PAT in STR with REP if PAT exists in STR. Report suitable messages in case
PAT does not exist in STR Support the program with functions for each of the
above operations. Don't use Built-in functions.
#include<stdio.h>
char str[50], pat[20], rep[20], res[50];
int c = 0, m = 0, i = 0, j = 0, k, flag = 0;
void stringmatch()
{
while (str[c] != '\0')
{
if (str[m] == pat[i])
{
i++;
m++;
if (pat[i] == '\0')
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++)
{
res[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
res[j] = str[c];
j++;
c++;
m = c;
Department of AI&ML, CIT - Ponnampet Page 5
Data Structures Laboratory (BCSL305)
i = 0;
}
}
res[j] = '\0';
}
void main()
{
printf("Enter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
printf("\nThe string before pattern match is:\n %s", str);
stringmatch();
if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res);
else
printf("\nPattern string not found");
}
Sample Output:
Enter the main string: I love Coffee
Enter the pat string: Coffee
Enter the replace string: Tea
The string before pattern match is:
I love Coffee
The string after pattern match and replace is:
I love Tea

Enter the main string: I love Coffee


Enter the pat string: Juice
Enter the replace string: Tea
The string before pattern match is:
I love Coffee
Pattern string not found
Department of AI&ML, CIT - Ponnampet Page 6
Data Structures Laboratory (BCSL305)
Program 03:
Develop a menu driven Program in C for the following operations on STACK of
Integers (Array Implementation of Stack with maximum size MAX)
a) Push an Element on to Stack
b) Pop an Element from Stack
c) Demonstrate how Stack can be used to check Palindrome
d) Demonstrate Overflow and Underflow situations on Stack
e) Display the status of Stack
f) Exit
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void push();
void pop();
void palindrome();
void display();
int s[MAX], top = -1, i, j, elem, ch;
void main() {
for (;;) {
printf("1: push 2: pop 3: palindrome 4: display 5: exit\n");
printf("Enter your choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: push(); break;
case 2: pop(); break;
case 3: palindrome(); break;
case 4: display(); break;
case 5: exit(0);
default: printf("Wrong choice\n");
}
}
}
void push() {
if (top == MAX - 1)
printf("Stack overflow\n");
Department of AI&ML, CIT - Ponnampet Page 7
Data Structures Laboratory (BCSL305)
else {
printf("Enter element to push\n");
scanf("%d", &elem);
top++;
s[top] = elem;
}
}
void pop() {
if (top == -1)
printf("Stack underflow\n");
else {
printf("Popped element = %d\n", s[top]);
top--;
}
}
void display() {
if (top == -1)
printf("Stack is empty\n");
else {
printf("Contents of stack:\n");
for (i = top; i >= 0; i--)
printf("%d\n", s[i]);
}
}
void palindrome() {
if (top == -1) {
printf("Stack is empty\n");
return;
} else {
for (i = top, j = 0; i >= j; i--, j++) {
if (s[i] != s[j])
break;
}
}

Department of AI&ML, CIT - Ponnampet Page 8


Data Structures Laboratory (BCSL305)
if (i > j)
printf("Not a palindrome\n");
else
printf("It is a palindrome\n");
}

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

Department of AI&ML, CIT - Ponnampet Page 10


Data Structures Laboratory (BCSL305)
Program 04:
Develop a Program in C for converting an Infix Expression to Postfix Expression.
Program should support for both parenthesized and free parenthesized expressions
with the operators: +, -, *, /, (Remainder), ^ (Power) and alphanumeric operands.

#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) {

Department of AI&ML, CIT - Ponnampet Page 11


Data Structures Laboratory (BCSL305)
case '^': return 3;
case '*':
case '/':
case '%': return 2;
case '+':
case '-': return 1;
case '(': return 0;
case '#': return -1;
default:
printf("Invalid operator\n");
return -1;
}
}
void convert() {
push('#');
for (i = 0; infix[i] != '\0'; i++) {
switch (infix[i]) {
case '(':
push('(');
break;
case '^':
case '*':
case '/':
case '%':
case '+':
case '-':
while (priority(s[top]) >= priority(infix[i])) {
operator = pop();
postfix[j++] = operator;
}
push(infix[i]);
break;
case ')':
while ((operator = pop()) != '(') {
postfix[j++] = operator;
Department of AI&ML, CIT - Ponnampet Page 12
Data Structures Laboratory (BCSL305)
}
break;
default:
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
} else {
printf("Invalid operand\n");
}
}
}
while ((operator = pop()) != '#') {
postfix[j++] = operator;
}
postfix[j] = '\0';
printf("Postfix expression = %s\n", postfix);
}

Sample Output:
Enter infix expression
A+(B*C-(D/E^F)*G)*H
Postfix expression = ABC*DEF^/G*-H*+

Department of AI&ML, CIT - Ponnampet Page 13


Data Structures Laboratory (BCSL305)
Program 05:
Develop a Program in C for the following Stack Applications.
a) Evaluation of Suffix expression with single digit operands and operators: +, -,
*, /, %,^.
b) Solving Tower of Hanoi problem with n disks.

a) Evaluation of Suffix expression with single digit operands and operators: +, -, *, /,


%,^.
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#define SIZE 50
char postfix[SIZE], element;
float s[SIZE], op1, op2, operand, res;
int top = -1, i;
void push(float operand);
float pop();
void eval();
void main() {
printf("Enter postfix expression\n");
scanf("%s", postfix);
eval();
}
void push(float operand) {
if (top == SIZE - 1)
printf("STACK OVERFLOW\n");
else {
top++;
s[top] = operand;
}
}
float pop() {
if (top == -1)
printf("STACK UNDERFLOW\n");
else
Department of AI&ML, CIT - Ponnampet Page 14
Data Structures Laboratory (BCSL305)
return s[top--];
}
void eval() {
for (i = 0; postfix[i] != '\0'; i++) {
if (isdigit(postfix[i])) {
operand = postfix[i] - '0';
push(operand);
} else {
op2 = pop();
op1 = pop();
switch (postfix[i]) {
case '+':
res = op1 + op2;
push(res);
break;
case '-':
res = op1 - op2;
push(res);
break;
case '*':
res = op1 * op2;
push(res);
break;
case '^':
res = powf(op1, op2);
push(res);
break;
case '/':
if (op2 == 0)
printf("Division error\n");
else {
res = op1 / op2;
push(res);
}
break;
Department of AI&ML, CIT - Ponnampet Page 15
Data Structures Laboratory (BCSL305)
case '%':
if (op2 == 0)
printf("Division error\n");
else {
res = (int)op1 % (int)op2;
push(res);
}
break;
default:
printf("Invalid operation\n");
}
}
}
printf("Result = %f\n", pop());
}

Sample Output:
Enter postfix expression
23*54*+
Result = 23.000000

b) Solving Tower of Hanoi problem with n disks.


#include <stdio.h>
void tower(int n, int source, int temp, int destination)
{
if (n == 0)
return;
tower(n - 1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n - 1, temp, source, destination);
}
void main()
{
int n;
printf("\nEnter the number of discs: \n");
Department of AI&ML, CIT - Ponnampet Page 16
Data Structures Laboratory (BCSL305)
scanf("%d", & n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int) pow(2, n) - 1);
}

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

Department of AI&ML, CIT - Ponnampet Page 17


Data Structures Laboratory (BCSL305)
Program 06:
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
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
char cq[MAX], elem;
int i, j, ch, count = 0, front = 0, rear = -1;
void insert();
void del();
void display();
void main() {
for(;;) {
printf("1:insert 2:delete 3:display 4:exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1: insert(); break;
case 2: del(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Wrong choice\n");
}
}
}
void insert() {
if(count == MAX)
printf("Circular queue is full\n");
else {
printf("Enter the character: ");
Department of AI&ML, CIT - Ponnampet Page 18
Data Structures Laboratory (BCSL305)
scanf(" %c", &elem); // Note the space before %c to consume any leftover newline character
rear = (rear + 1) % MAX;
cq[rear] = elem;
count++;
}
}
void del() {
if(count == 0)
printf("Circular queue is empty\n");
else {
printf("Deleted element = %c\n", cq[front]);
front = (front + 1) % MAX;
count--;
}
}
void display() {
if(count == 0)
printf("Circular queue is empty\n");
else {
printf("Contents of circular queue:\n");
i = front;
for(j = 0; j < count; j++) {
printf("%c\t", cq[i]);
i = (i + 1) % MAX;
}
printf("\n");
}
}

Sample Output:
1:insert 2:delete 3:display 4:exit

Enter your choice: 1

Enter the character: A

1:insert 2:delete 3:display 4:exit

Department of AI&ML, CIT - Ponnampet Page 19


Data Structures Laboratory (BCSL305)
Enter your choice: 1

Enter the character: B

1:insert 2:delete 3:display 4:exit

Enter your choice: 1

Enter the character: C

1:insert 2:delete 3:display 4:exit

Enter your choice: 1

Circular queue is full

1:insert 2:delete 3:display 4:exit

Enter your choice: 3

Contents of circular queue:

A B C

1:insert 2:delete 3:display 4:exit

Enter your choice: 2

Deleted element = A

1:insert 2:delete 3:display 4:exit

Enter your choice: 3

Contents of circular queue:

B C

1:insert 2:delete 3:display 4:exit

Enter your choice: 2

Deleted element = B

1:insert 2:delete 3:display 4:exit

Enter your choice: 3

Contents of circular queue:

1:insert 2:delete 3:display 4:exit

Enter your choice: 2

Department of AI&ML, CIT - Ponnampet Page 20


Data Structures Laboratory (BCSL305)
Deleted element = C

1:insert 2:delete 3:display 4:exit

Enter your choice: 3

Circular queue is empty

Department of AI&ML, CIT - Ponnampet Page 21


Data Structures Laboratory (BCSL305)
Program 07:
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

#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

Department of AI&ML, CIT - Ponnampet Page 26


Data Structures Laboratory (BCSL305)
1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit
Enter your choice: 4
Enter USN: 11223
Enter name: Bob
Enter branch: ECE
Enter semester: 1
Enter phone number: 5555555555
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
11223 Bob ECE 1 5555555555
Total number of nodes = 3
1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit
Enter your choice: 5
One info deleted
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
12345 John CSE 3 9876543210
11223 Bob ECE 1 5555555555
Total number of nodes = 2
1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit
Enter your choice: 6
One node deleted
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
12345 John CSE 3 9876543210
Total number of nodes = 1
1: create 2: display 3: ins_front 4: ins_rear 5: del_front 6: del_rear 7: exit
Enter your choice: 7

Department of AI&ML, CIT - Ponnampet Page 27


Data Structures Laboratory (BCSL305)
Program 08:
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

#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;
}
}
}
}

Department of AI&ML, CIT - Ponnampet Page 30


Data Structures Laboratory (BCSL305)
void display() {
NODE cur;
cur = first;
if (cur == NULL) {
printf("list is empty\n");
} else {
printf("ssn\t name\t dept\t desg\t sal\t phno\n");
while (cur != NULL) {
printf("%s\t %s\t %s\t %s\t %f\t %s\n", cur->ssn, cur->name, cur->dept, cur->dsg,
cur->sal, cur->phno);
cur = cur->rlink;
}
}
}
void ins_f() {
NODE temp;
if ((temp = (NODE)malloc(sizeof(struct list))) == NULL) {
printf("insufficient memory\n");
} else {
read(temp);
temp->rlink = temp->llink = NULL;
if (first != NULL) {
temp->rlink = first;
first->llink = temp;
}
first = temp;
}
}
void del_f() {
NODE cur;
if (first == NULL) {
printf("list is empty\n");
} else {
cur = first;
first = first->rlink;
Department of AI&ML, CIT - Ponnampet Page 31
Data Structures Laboratory (BCSL305)
if (first != NULL) {
first->llink = NULL;
}
free(cur);
printf("one info del\n");
c--;
}
}
void del_r() {
NODE cur, prev;
if (first == NULL) {
printf("list is empty\n");
} else {
cur = first;
while (cur->rlink != NULL) {
cur = cur->rlink;
}
if (cur == first) {
first = NULL;
} else {
prev = cur->llink;
prev->rlink = NULL;
}
printf("one info del\n");
c--;
free(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

Department of AI&ML, CIT - Ponnampet Page 33


Data Structures Laboratory (BCSL305)
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: 6
One info del
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
Total number of nodes = 1
1: create 2: display 3: ins_f 4: ins_r 5: del_f 6: del_r 7: exit
Enter your choice: 7

Department of AI&ML, CIT - Ponnampet Page 34


Data Structures Laboratory (BCSL305)
Program 09:
Develop a Program in C for the following operations on 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).

#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);

Department of AI&ML, CIT - Ponnampet Page 41


Data Structures Laboratory (BCSL305)
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
search();
break;
case 6:
exit(0);
default:
printf("wrong choice\n");
}
}
return 0;
}
void create() {
int elem;
NODE cur, prev, temp;
temp = (NODE)malloc(sizeof(struct list));
printf("enter the data\n");
scanf("%d", &elem);
temp->info = elem;
temp->llink = temp->rlink = NULL;
if (root == NULL) {
root = temp;
} else {
prev = NULL;
cur = root;
while (cur != NULL) {
prev = cur;
if (temp->info < cur->info) {
cur = cur->llink;
Department of AI&ML, CIT - Ponnampet Page 42
Data Structures Laboratory (BCSL305)
} else {
cur = cur->rlink;
}
}
if (temp->info < prev->info) {
prev->llink = temp;
} else {
prev->rlink = temp;
}
}
}
void search() {
int key;
NODE cur;
printf("enter the key to search\n");
scanf("%d", &key);
if (root == NULL) {
printf("tree is empty\n");
} else {
cur = root;
while (cur != NULL) {
if (key == cur->info) {
printf("key found\n");
return;
} else if (key < cur->info) {
cur = cur->llink;
} else {
cur = cur->rlink;
}
}
printf("key not found\n");
}
}
void inorder(NODE temp) {
if (temp != NULL) {
Department of AI&ML, CIT - Ponnampet Page 43
Data Structures Laboratory (BCSL305)
inorder(temp->llink);
printf("%d\t", temp->info);
inorder(temp->rlink);
}
}
void preorder(NODE temp) {
if (temp != NULL) {
printf("%d\t", temp->info);
preorder(temp->llink);
preorder(temp->rlink);
}
}
void postorder(NODE temp) {
if (temp != NULL) {
postorder(temp->llink);
postorder(temp->rlink);
printf("%d\t", temp->info);
}
}

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

Department of AI&ML, CIT - Ponnampet Page 46


Data Structures Laboratory (BCSL305)
Program 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.

#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

Department of AI&ML, CIT - Ponnampet Page 49


Data Structures Laboratory (BCSL305)
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: 2
Nodes reachable from starting vertex 1 are:
243

Department of AI&ML, CIT - Ponnampet Page 50


Data Structures Laboratory (BCSL305)
Program 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 (2digit) 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.

#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]);
}

Department of AI&ML, CIT - Ponnampet Page 52


Data Structures Laboratory (BCSL305)
Sample Output:
1:insert 2:display 3:exit
enter your choice
1
enter the key value
25
1:insert 2:display 3:exit
enter your choice
1
enter the key value
35
1:insert 2:display 3:exit
enter your choice
2
index: key
0:-1
1:-1
2:-1
3:-1
4:-1
5:25
6:35
7:-1
8:-1
9:-1
10:-1
……
……
99:-1

Department of AI&ML, CIT - Ponnampet Page 53

You might also like