DS_Lab_2023-24
DS_Lab_2023-24
DEPARTMENT OF
ARTIFICIAL INTELLIGENCE & MACHINE
LEARNING
LAB MANUAL
(ACADEMIC YEAR 2023-2024)
Vision
To be a centre of excellence to produce engineers in the field of artificial intelligence &
Machine Learning who are committed, innovation driven, self - determined, and capable of
developing applications for the improvement of society
Mission
To expedite the students to various knowledge platforms through expertized academic
guidance and supervision.
To inspire the students to be innovative, skilled engineers by imparting the leadership
qualities, team-spirit, and ethical responsibilities.
To encourage faculty and students to get involved in lifelong learning, research oriented
and significantly contributing to the advancement of the civilization.
Demonstrate technical Competence in Artificial intelligence & Machine learning and their
applications
Design solution for problems in different domains like medical, data science by applying
Artificial intelligence & Machine learning Concepts.
Take up Masters and Research Programs in the area of Artificial intelligence & Machine
learning.
Apply knowledge of Artificial intelligence & Machine learning and formulate solutions for
interdisciplinary problems
Develop models in Data Science,Machine Learning and Big Data technologies and
integrate into real time applications
Q No Description
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
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
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.
// Function prototypes
void create(struct Day *calendar, int size);
void read(struct Day *calendar, int size);
void display(struct Day *calendar, int size);
int main() {
int size = 7; // Number of days in a week
struct Day *calendar = (struct Day *)malloc(size * sizeof(struct Day));
if (calendar == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
create(calendar, size);
read(calendar, size);
display(calendar, size);
return 0;
}
printf("Date: ");
scanf("%d", &calendar[i].date);
printf("Activity: ");
scanf(" %[^\n]s", calendar[i].activity);
}
}
// Function to display the calendar
void display(struct Day *calendar, int size) {
printf("\nWeek's Activity Details:\n");
for (int i = 0; i < size; ++i) {
printf("Day %d - %s, Date %d: %s\n", i + 1, calendar[i].dayName, calendar[i].date,
calendar[i].activity);
}
}
OUTPUT:
#include<stdio.h>
#include<stdlib.h>
char STR[100],PAT[100],REP[100];
void ReadStrings(char STR[100], char PAT[100], char REP[100])
{
printf("Enter the main string STR : ");
gets(STR);
printf("Enter the pattern string PAT : ");
gets(PAT);
printf("Enter the replace string REP : ");
gets(REP);
}
void PatternMatching(char STR[100], char PAT[100], char REP[100])
{
int i,j,k,m,c,flag,count;
i=m=c=j=count=flag=0;
char RESULT[100];
while(STR[c] != '\0')
{
if(STR[m] == PAT[i])
{
i++;
m++;
if(PAT[i] =='\0')
{
for(k=0;REP[k] !='\0';k++,j++)
RESULT[j]=REP[k];
i=0;
c=m;
count++;
flag =1;
}
}
else
{
RESULT[j]=STR[c];
j++;
c++;
m=c;
i=0;
}
}
if(flag == 0)
printf("Pattern does not found\n");
else
{
RESULT[j]='\0';
printf("Pattern found %d times\n",count);
printf("The resultant string is\n%s\n",RESULT);
}
}
int main()
{
ReadStrings(STR,PAT,REP);
PatternMatching(STR, PAT, REP);
}
OUTPUT 1:
Enter the main string STR : YIT Moodbidri is the best college in Moodbidri
Enter the pattern string PAT : Moodbidri
Enter the replace string REP : Mangalore
Pattern found 2 times
The resultant string is
YIT Mangalore is the best college in Mangalore
OUTPUT 2:
Enter the main string STR : YIT Moodbidri
Enter the pattern string PAT : Yenepoya
Enter the replace string REP : YIT
Pattern does not found
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
int stack[MAX];
int top = -1;
char a[10];
int isFull()
{
return (top == MAX - 1);
}
int isEmpty()
{
return (top == -1);
}
int pop()
{
if (isEmpty())
{
printf("Stack Underflow. Cannot pop element.\n");
return -1;
}
else
{
int element = stack[top--];
return element;
}
}
int isPalindrome()
{
int i;
for(i=0;i<=top;i++)
{
if (a[i] != pop())
return 0;
}
return 1;
}
void displayStack() {
if (isEmpty()) {
printf("Stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = 0; i <= top; i++)
{
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main()
{
int choice, element,i;
while(1)
{
printf("\n------ Menu ------");
printf("Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit\n ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
element = pop();
if (element != -1)
printf("Popped element: %d", element);
break;
case 3:
top=-1;
printf("Enter the string:");
scanf("%s",a);
for (i=0;i<strlen(a);i++)
push(a[i]);
if (isPalindrome())
printf("The string is a palindrome.\n");
else
printf("The string not palindrome.\n");
break;
case 4:
displayStack();
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please enter a valid choice.\n");
}
}
OUTPUT:
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
3
Enter the string:MALAYALAM
Element 77 pushed successfully.
Element 65 pushed successfully.
Element 76 pushed successfully.
Element 65 pushed successfully.
Element 89 pushed successfully.
Element 65 pushed successfully.
Element 76 pushed successfully.
Element 65 pushed successfully.
Element 77 pushed successfully.
The string is a palindrome.
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
4
Stack elements: 77 65 76 65
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
1
Enter the element to push: 10
Element 10 pushed successfully.
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
2
Popped element: 10
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
4
Stack elements: 77 65 76 65
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
2
Popped element: 65
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
2
Popped element: 76
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
22
Invalid choice. Please enter a valid choice.
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
2
Popped element: 65
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
2
Popped element: 77
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
2
Stack Underflow. Cannot pop element.
------ Menu ------Enter 1. Push, 2 for pop, 3 for Palindrome, 4 Display stack, 5 Exit
5
#include<stdio.h>
#include<stdlib.h>
int stack[20];
int top = -1;
void push(char);
char pop();
int incmprc(char);
int instprc(char);
void IntoPost(char[], char[]);
int main()
{
char infix[20], postfix[20];
printf("Enter the valid infix expression : ");
scanf("%s",infix);
push('#');
IntoPost(infix,postfix);
printf("The corresponding postfix expression is :
\n%s\n",postfix);
}
char pop()
{
return stack[top--];
}
OUTPUT:
Enter the valid infix expression : (3+5)*(3-5)
The corresponding postfix expression is :
35+35-*
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int stack[50];
int top = -1;
void push(int);
int pop();
int pop()
{
return stack[top--];
int main()
{
char suffix[50],ch;
int p1,p2,p3,i;
printf("Enter the valid suffix expression : ");
gets(suffix);
for(i=0;suffix[i]!='\0';i++)
{
ch = suffix[i];
//puts(ch);
if(isdigit(ch))
push(ch-'0');
else
{
p2 = pop();
p1 = pop();
switch(ch)
{
case '+': push(p1+p2);
//printf("%d",pop());
break;
case '-': push(p1-p2);
break;
OUTPUT 1:
OUTPUT 2:
#include <stdio.h>
int main() {
int n;
return 0;
}
OUTPUT:
Enter the number of disks: 3
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
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
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int main()
{
int choice,rear = 0,front = 0;
char cirqueue[MAX], ele;
while(1)
{
printf("\n\n**********************MENU*****************");
printf("\n1. Insert an Element on to Circular Queue\n2. Delete an
Element from Circular Queue\n3. Display the status of Circular Queue\n4. Exit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: rear = insert(cirqueue,rear,front);
break;
case 2: front = delete(cirqueue,rear,front);
break;
case 3: display(cirqueue,rear,front);
break;
case 4: exit(0);
}
}
}
i=front;
do
{
i=((i+1)%MAX);
printf("%c ",cirqueue[i]);
}
while(i!=rear);
}
}
OUTPUT:
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 1
Enter the element to be inserted: c
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 1
Enter the element to be inserted: h
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 1
Enter the element to be inserted: o
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 3
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 1
Circular Queue Overflow
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 3
The elements of circular queue are
holi
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 2
The element deleted is : h
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 2
The element deleted is : o
**********************MENU*****************
1. Insert an Element on to Circular Queue
2. Delete an Element from Circular Queue
3. Display the status of Circular Queue
4. Exit
Enter your choice : 2
The element deleted is : l
**********************MENU*****************
1. Insert an Element on to Circular Queue
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count = 0;
struct node
{
int sem, phno;
char usn[50], name[50],branch[50];
struct node *link;
};
struct node *first = NULL, *last = NULL, *temp = NULL, *temp1 = NULL;
void CreateNode()
{
int sem,phno;
char usn[50],name[50],branch[50];
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter usn,name, branch, sem, phno of student \n");
scanf("%s %s %s %d %d", usn, name,branch, &sem,&phno);
strcpy(temp->usn,usn);
strcpy(temp->name,name);
strcpy(temp->branch,branch);
temp->sem = sem;
temp->phno = phno;
temp->link=NULL;
count++;
}
void InsertAtFront()
{
if (first == NULL)
{
CreateNode();
first = temp;
last = first;
}
else
{
CreateNode();
temp->link = first;
first = temp;
}
}
void InsertAtEnd()
{
if(first==NULL)
{
CreateNode();
first = temp;
last = first;
}
else
{
CreateNode();
last->link = temp;
last = temp;
}
}
void Display()
{
temp1=first;
if(temp1 == NULL)
{
printf("\nList is empty \n");
}
else
{
printf("\nThe elements of SLL are :\n");
while (temp1!= NULL)
{
printf("%s %s %s %d %d\n", temp1->usn, temp1-
>name,temp1->branch,temp1->sem,temp1->phno );
temp1 = temp1->link;
}
}
printf("Number of students = %d\n", count);
}
void DeleteAtEnd()
{
struct node *temp;
temp=first;
if(first == NULL)
printf("\nList is empty\n");
else if(temp->link==NULL)
{
printf("The student record deleted is\n%s %s %s %d %d\n",
temp->usn, temp->name, temp->branch, temp->sem, temp->phno );
free(temp);
first=NULL;
count--;
}
else
{
while(temp->link!=last)
temp=temp->link;
printf("The student record deleted is\n%s %s %s %d %d\n",
last->usn, last->name,last->branch,last->sem, last->phno );
free(last);
temp->link=NULL;
last=temp;
count--;
}
}
void DeleteAtFront()
{
struct node *temp;
temp=first;
if(first == NULL)
printf("\nList is empty\n");
else if(temp->link==NULL)
{
printf("The student record deleted is\n%s %s %s %d %d",
temp->usn, temp->name,temp->branch,temp->sem, temp->phno );
free(temp);
first=NULL;
count--;
}
else
{
first=temp->link;
printf("The student record deleted is\n%s %s %s %d %d",
temp->usn, temp->name,temp->branch,temp->sem, temp->phno );
free(temp);
count--;
}
}
int main()
{
while(1)
{
int choice, n, i, choice2, flag1, flag2;
printf("\n\n**********************MENU*****************");
printf("\n1. Create a SLL of N students Data by using front
insertion\n2. Display the status of SLL and count the number of nodes
in it\n3. Perform Insertion/Deletion at End of SLL\n4. Perform
Insertion/Deletion at Front of SLL (Demonstaration of SLL as
STACK)\n5. Exit\n");
case 5 : exit(0);
break;
}
}
}
OUTPUT
**********************MENU*****************
1. Create a SLL of N students Data by using front insertion
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion/Deletion at End of SLL
4. Perform Insertion/Deletion at Front of SLL (Demonstaration of SLL as STACK)
5. Exit
Enter your choice : 1
Enter the number of students : 2
Enter usn,name, branch, sem, phno of student
4PA15CS001 JOHN CS 1 1111
Enter usn,name, branch, sem, phno of student
4PA15CS002 JACK CS 3 3333
**********************MENU*****************
1. Create a SLL of N students Data by using front insertion
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion/Deletion at End of SLL
4. Perform Insertion/Deletion at Front of SLL (Demonstaration of SLL as STACK)
5. Exit
Enter your choice : 2
The elements of SLL are :
4PA15CS002 JACK CS 3 3333
4PA15CS001 JOHN CS 1 1111
Number of students = 2
**********************MENU*****************
1. Create a SLL of N students Data by using front insertion
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion/Deletion at End of SLL
4. Perform Insertion/Deletion at Front of SLL (Demonstaration of SLL as STACK)
5. Exit
Enter your choice : 3
1. Insertion
2. Deletion
3. Exit
Enter your option
1
1. Insertion
2. Deletion
3. Exit
Enter your option
2
The student record deleted is
4PA15CS003 RAJ CS 5 5555
1. Insertion
2. Deletion
3. Exit
Enter your option
3
**********************MENU*****************
1. Create a SLL of N students Data by using front insertion
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion/Deletion at End of SLL
4. Perform Insertion/Deletion at Front of SLL (Demonstaration of SLL as STACK)
5. Exit
Enter your choice : 4
Demonstration of STACK
1. Insertion
2. Deletion
3. Exit
Enter your option
1
Enter usn,name, branch, sem, phno of student
4PA15CS004 RAHUL CS 7 7777
1. Insertion
2. Deletion
3. Exit
1. Insertion
2. Deletion
3. Exit
1. Insertion
2. Deletion
3. 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{
int ssn, phno;
float sal;
char name[50],dept[50],desig[50];
struct node *llink;
struct node *rlink;
};
struct node *first = NULL,*temp = NULL,*temp1=NULL,*temp2=NULL;
void CreateNode()
{
int ssn,phno;
float sal;
char name[50],dept[50],desig[50];
temp =(struct node *)malloc(sizeof(struct node));
temp->llink = NULL;
temp->rlink = NULL;
printf("\nEnter ssn,name,department, designation, salary and phno
of employee\n");
scanf("%d %s %s %s %f %d", &ssn, name,dept,desig,&sal, &phno);
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desig,desig);
temp->sal = sal;
temp->phno = phno;
count++;
}
void InsertAtFront()
{
if (first == NULL)
{
CreateNode();
first = temp;
temp1 = first;
}
else
{
CreateNode();
temp->rlink = first;
first->llink = temp;
first = temp;
}
}
void InsertAtEnd()
{
if(first==NULL)
{
CreateNode();
first = temp;
temp1 = first;
}
else
{
CreateNode();
temp1->rlink = temp;
temp->llink = temp1;
temp1 = temp;
}
}
void Display()
{
temp2 =first;
if(temp2 == NULL)
{
printf("\nList is empty \n");
}
else
{
printf("\n\nThe elements of DLL are :\n");
while (temp2!= NULL)
{
printf("%d %s %s %s %f %d\n", temp2-
>ssn, temp2->name,temp2->dept,temp2->desig,temp2->sal, temp2->phno );
temp2 = temp2->rlink;
}
}
printf("Number of employees = %d ", count);
}
void DeleteAtEnd()
{
struct node *temp;
temp=first;
if(first == NULL)
printf("\nList is empty\n");
else if(temp->rlink==NULL)
{
printf("The employee record deleted is\n%d %s %s %s %f
%d\n", temp->ssn, temp->name,temp->dept,
temp->desig,temp->sal, temp->phno );
free(temp);
first=NULL;
count--;
}
else
{
temp2=temp1->llink;
temp2->rlink=NULL;
printf("The employee record deleted is\n%d %s %s %s %f
%d\n", temp1->ssn, temp1->name,temp1->dept,
temp1->desig,temp1->sal, temp1->phno );
free(temp1);
temp1 = temp2;
count--;
}
}
void DeleteAtFront()
{
struct node *temp;
temp=first;
if(first == NULL)
printf("\nList is empty\n");
else if(temp->rlink==NULL)
{
printf("The employee record deleted is\n%d %s %s %s %f %d",
temp->ssn, temp->name,temp->dept, temp->desig,temp->sal, temp->phno );
free(temp);
first=NULL;
count--;
}
else
{
first=first->rlink;
printf("The employee record deleted is\n%d %s %s %s %f %d",
temp->ssn, temp->name,temp->dept, temp->desig,temp->sal, temp->phno );
free(temp); \
count--;
}
}
int main()
{
Display();
break;
case 2 : DeleteAtFront();
Display();
break;
case 3 : flag2 = 1;
break;
}
}
break;
OUTPUT:
**********************MENU*****************
1. Create a DLL of N employees Data by using end insertion
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
Department of AI&ML, YIT Moodbidri 34
Data Structure Lab (BCSL305)
**********************MENU*****************
1. Create a DLL of N employees Data by using end insertion
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstaration of DLL as DOUBLE ENDED QUEUE
6. Exit
Enter your choice : 2
**********************MENU*****************
1. Create a DLL of N employees Data by using end insertion
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstaration of DLL as DOUBLE ENDED QUEUE
6. Exit
Enter your choice : 3
1. Insertion
2. Deletion
3. Exit
1. Insertion
2. Deletion
3. Exit
1. Insertion
2. Deletion
3. Exit
**********************MENU*****************
1. Create a DLL of N employees Data by using end insertion
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstaration of DLL as DOUBLE ENDED QUEUE
6. Exit
Enter your choice : 4
1. Insertion
2. Deletion
3. Exit
1. Insertion
2. Deletion
3. Exit
1. Insertion
2. Deletion
3. Exit
**********************MENU*****************
1. Create a DLL of N employees Data by using end insertion
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstaration of DLL as DOUBLE ENDED QUEUE
6. Exit
Enter your choice : 5
Demonstration of DLL as DOUBLE ENDED QUEUE
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
List is empty
Number of employees = 0
1. Insert at end
2. Delete at end
3. Insert at front
4. Delete at front
5. Exit
**********************MENU*****************
1. Create a DLL of N employees Data by using end insertion
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstaration of DLL as DOUBLE ENDED QUEUE
6. Exit
Enter your choice : 6
9. Design, Develop and Implement 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)
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int coef;
int xexp;
int yexp;
int zexp;
struct node *link;
};
typedef struct node *NODE;
NODE first = NULL, last = NULL, temp = NULL, poly1 = NULL, poly2 =
NULL;
void CreateTerm()
{
int coef, xexp, yexp, zexp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the coefficient, x power, y power and z power : ");
scanf("%d %d %d %d",&coef, &xexp, &yexp, &zexp);
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
temp->link=NULL;
}
temp1 = temp1->link;
}
}
void CreatePoly()
{
int n, i;
NODE header = NULL;
header = (struct node *)malloc(sizeof(struct node));
header->coef = header->xexp = header->yexp = header->zexp = -1;
header->link = header;
printf("\nEnter the number of terms in the polynomial : ");
scanf("%d",&n);
for(i = 1;i <= n; i++)
{
if(header->link == header)
{
CreateTerm();
header->link = temp;
temp->link = header;
first = header;
last = temp;
}
else
{
CreateTerm();
last->link = temp;
temp->link = header;
last = temp;
}
}
DisplayPoly(first);
}
void EvaluatePoly()
{
int x, y, z, sum = 0;
NODE temp2;
printf("\n\nEnter the value for x, y and z : ");
scanf("%d %d %d", &x, &y, &z);
temp2 = first->link;
while(temp2 != first)
{
sum = sum + temp2->coef * pow(x,temp2->xexp) * pow(y,
temp2->yexp) * pow(z,temp2->zexp);
temp2 = temp2->link;
}
printf("The value of polynomial is : %d\n", sum);
}
void attach(int coef, int xexp, int yexp, int zexp, NODE *rear)
{
NODE temp3;
temp3=(struct node*)malloc(sizeof(struct node));
temp3->coef = coef;
temp3->xexp = xexp;
temp3->yexp = yexp;
temp3->zexp = zexp;
temp3->link = NULL;
(*rear)->link = temp3;
*rear = temp3;
}
flag1 = 0;
ptr1 = ptr1->link;
}
ptr2 = poly2->link;
while(ptr2 != poly2)
{
temp2 = poly4->link;
while(temp2 != poly4)
{
if(ptr2->xexp == temp2->xexp && ptr2->yexp == temp2-
>yexp && ptr2->zexp == temp2->zexp)
{
flag2 = 1;
break;
}
else
temp2 = temp2->link;
}
if(flag2 == 0)
{
attach(ptr2->coef, ptr2->xexp, ptr2->yexp, ptr2->zexp,
&header1);
header1->link = poly3;
}
flag2 = 0;
ptr2 = ptr2->link;
}
printf("\n\nResultant Polynomial:");
DisplayPoly(poly3);
}
int main()
{
while(1)
{
int choice;
printf("\n\n**********************MENU*****************");
printf("\n1. Represent and Evaluate the Polynomial\n2. Add
two Polynomials\n3. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: first = last = NULL;
CreatePoly();
EvaluatePoly();
break;
case 2: printf("\nPolynomial 1 :\n");
first = last = NULL;
CreatePoly();
poly1=first;
printf("\n\nPolynomial 2 :\n");
first = last = NULL;
CreatePoly();
poly2=first;
Add(poly1,poly2);
break;
case 3: exit(0);
break;
}
}
}
OUTPUT
**********************MENU*****************
1. Represent and Evaluate the Polynomial
2. Add two Polynomials
3. Exit
Enter your choice : 1
The Polynomial is :
6x^(2)y^(2)z^(1)-4x^(0)y^(1)z^(5) + 3x^(3)y^(1)z^(1) + 2x^(1)y^(5)z^(1)-2x^(1)y^(1)z^(3)
**********************MENU*****************
1. Represent and Evaluate the Polynomial
2. Add two Polynomials
3. Exit
Enter your choice : 2
Polynomial 1 :
The Polynomial is :
2x^(2)y^(2)z^(2) + 3x^(3)y^(1)z^(1) + 4x^(1)y^(2)z^(3)
Polynomial 2 :
The Polynomial is :
5x^(3)y^(3)z^(3) + 2x^(1)y^(2)z^(3) + 7x^(1)y^(1)z^(1) + 6x^(2)y^(2)z^(2)
Resultant Polynomial:
The Polynomial is :
8x^(2)y^(2)z^(2) + 3x^(3)y^(1)z^(1) + 6x^(1)y^(2)z^(3) + 5x^(3)y^(3)z^(3) + 7x^(1)y^(1)z^(1)
**********************MENU*****************
1. Represent and Evaluate the Polynomial
2. Add two Polynomials
3. Exit
Enter your choice : 3
10. Design, Develop and Implement 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
e. Exit
#include<stdio.h>
#include <stdlib.h>
#define MAX 20
struct node
{
int data;
struct node *lchild, *rchild;
};
typedef struct node *NODE;
NODE tree = NULL;
void CreateBST (int a[MAX], int n)
{
NODE temp, p, q;
int i;
for(i=0;i<n;i++)
{
temp =(struct node *)malloc(sizeof(struct node*));
temp->data = a[i];
temp->lchild = temp->rchild = NULL;
if(tree == NULL)
tree = temp;
else
{
p = q = tree;
while(q != NULL)
{
p=q;
if(a[i] < p->data)
q = p->lchild;
else if(a[i] > p->data)
q = p->rchild;
else
{
free(temp);
break;
}
}
if( q == NULL)
{
if(a[i] < p->data)
p->lchild = temp;
else
p->rchild = temp;
}
}
}
printf("Binary Seacrh Tree created\n\n");
}
void SearchBST()
{
NODE temp1;
int key;
printf("Enter the key to be searched : ");
scanf("%d", &key);
temp1= tree;
while(temp1 != NULL)
{
if(key == temp1->data)
{
printf("The key %d found in the BST", key);
return;
}
else if(key < temp1->data)
temp1 = temp1->lchild;
else
temp1 = temp1->rchild;
}
printf("Key %d not found in the BST", key);
}
int main()
{
int a[MAX], n, i, choice;
while(1)
{
printf("\n\n**********************MENU*****************");
printf("\n1. Create a BST of n integers\n2. Traverse the
BST in Inorder\n3. Traverse the BST in Preorder\n4. Traverse the BST
in Postorder\n5. Search the BST for a given element (KEY)\n6.
Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Enter the number of integers : ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
CreateBST(a, n);
break;
case 2 : printf("Inorder Traversal :\n");
Inorder(tree);
break;
case 3 : printf("Preorder Traversal :\n");
Preorder(tree);
break;
case 4 : printf("Postoder Traversal :\n");
Postorder(tree);
break;
case 5 : SearchBST(tree);
break;
case 6 : exit(0);
break;
}
}
}
OUTPUT
5 9
2 8 15
7 14 24
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 1
Enter the number of integers : 12
Enter the elements
6 9 5 2 8 15 24 14 7 8 5 2
Binary Seacrh Tree created
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 2
Inorder Traversal :
2 5 6 7 8 9 14 15 24
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 3
Preorder Traversal :
6 5 2 9 8 7 15 14 24
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 4
Postoder Traversal :
2 5 7 8 14 24 15 9 6
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 5
Enter the key to be searched : 8
The key 8 found in the BST
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 5
Enter the key to be searched : 10
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Search the BST for a given element (KEY)
6. Exit
Enter your choice : 6
11. Design, Develop and Implement 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>
#define MAX 10
int source;
int main()
{
int a[MAX][MAX], visited[MAX], n, choice, i, j, x, y;
printf("Enter the number of vertices in the graph : ");
scanf("%d", &n);
printf("Enter the adjacency matrix for the graph\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
printf("Enter the starting node of the graph : ");
scanf("%d",&source);
while(1)
{
printf("\n\n**********************MENU*****************");
printf("\n1. DFS\n2. BFS\n3. Exit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1 : printf("Nodes reachable from %d using DFS
method\n", source);
for(x=1; x<=n; x++)
visited[x] = 0;
DFS(a, visited, source, n);
break;
case 2 : printf("Nodes reachable from %d using BFS
method\n", source);
for(y=1; y<=n; y++)
visited[y] = 0;
BFS(a, visited, source, n);
break;
case 3: exit(0);
break;
}
}
}
OUTPUT
2 3
4 5 6 7
**********************MENU*****************
1. DFS
2. BFS
3. Exit
Enter your choice : 1
Nodes reachable from 1 using DFS method
2 4 5 3 6 7
**********************MENU*****************
1. DFS
2. BFS
3. Exit
Enter your choice : 2
Nodes reachable from 1 using BFS method
2 3 4 5 6 7
**********************MENU*****************
1. DFS
2. BFS
3. Exit
Enter your choice : 3
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. Design and 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>
#define MAX 100
int main()
{
int n,m,ht[MAX],i, j, k, rec, address, homebucket, currentbucket,
count = 0, choice;
printf("Enter the number of employee records : ");
scanf("%d", &n);
for(i = 0; i < MAX; i++)
ht[i] = -1;
for(k = 0; k <n; k++)
{
printf("\nEnter the record %d\n", k+1);
scanf("%d", &rec);
address = rec % MAX;
homebucket = address;
currentbucket = homebucket;
while(ht[currentbucket] != -1)
{
currentbucket = (currentbucket + 1) % MAX;
if(currentbucket == homebucket)
{
printf("Hash Table Overflow");
exit(0);
}
count++;
}
if(count != 0)
printf("Collision occured %d times and solved using
Linear Probing\n", count);
count = 0;
ht[currentbucket] = rec;
printf("Record : %d\nHome Address : %d\nCurrent Adrress :
%d\n",rec,homebucket,currentbucket);
}
printf("\nHASH TABLE DISPLAY\n");
while(1)
{
printf("\n\n**********************MENU*****************");
OUTPUT
Home Address : 31
Current Adrress : 32
**********************MENU*****************
1. Complete Hash table contents
2. Hash Table showing only record entries
3. Exit
27 -1
28 -1
29 -1
30 -1
31 1231
32 2231
33 -1
34 -1
35 -1
36 -1
37 -1
38 -1
39 -1
40 -1
41 -1
42 -1
43 -1
44 -1
45 -1
46 -1
47 -1
48 -1
49 -1
50 -1
51 -1
52 -1
53 -1
54 -1
55 -1
56 -1
57 -1
58 -1
59 -1
60 -1
61 -1
62 -1
63 -1
64 -1
65 1265
66 -1
67 -1
68 -1
69 -1
70 -1
71 -1
72 -1
73 -1
74 -1
75 -1
76 -1
77 -1
78 -1
79 -1
80 -1
81 -1
82 -1
83 -1
84 -1
85 -1
86 -1
87 -1
88 -1
89 -1
90 -1
91 -1
92 -1
93 -1
94 -1
95 -1
96 -1
97 -1
98 -1
99 1299
**********************MENU*****************
1. Complete Hash table contents
2. Hash Table showing only record entries
3. Exit
**********************MENU*****************
1. Complete Hash table contents
2. Hash Table showing only record entries
3. Exit