[go: up one dir, main page]

0% found this document useful (0 votes)
22 views60 pages

DSA lab final record

The document contains a detailed index of various programming implementations, including dynamic memory management, recursive functions, stack operations, infix to postfix conversion, queue operations, circular queue operations, linked lists, tree traversals, and graph algorithms. Each section includes code snippets, explanations, and sample outputs for the respective implementations. The document serves as a comprehensive guide for understanding and executing these fundamental data structure and algorithm concepts.

Uploaded by

hannibalwilliamj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views60 pages

DSA lab final record

The document contains a detailed index of various programming implementations, including dynamic memory management, recursive functions, stack operations, infix to postfix conversion, queue operations, circular queue operations, linked lists, tree traversals, and graph algorithms. Each section includes code snippets, explanations, and sample outputs for the respective implementations. The document serves as a comprehensive guide for understanding and executing these fundamental data structure and algorithm concepts.

Uploaded by

hannibalwilliamj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

INDEX

Sl.No. Program Name Page No.

01. Implementation of Dynamic 1-3


MemoryManagement functions.

02. Implementation of Recursive Functions. 4-7

03. Implementation of Stack operations. 8-14

04. Implementation of Infix to Postfix conversion. 15-18

05. Implementation of Queue operations. 19-25

06. Implementation of Circular Queue operations. 26-31

07. Implementation of Singly Linked list. 32-41

08. Implementation of Doubly Linked list. 42-52

09. Implementation of Tree Traversals. 53-55

10. Implementation of Graph to find Minimum 56-59

Spanning Tree.

Staff Signature
Lab Program -1
IMPLEMENTATION OF DYNAMIC MEMOREY MANAGEMENT FUNCTION

#include<stdio.h>
#include<stdlib.h>
int main ()
{
int *num=(int*) malloc (sizeof(int));
if(num==NULL)
{
printf ("Memory Allocation Failed:\n");
return 1;
}
*num=42;
printf ("Dynamically Allocated Integer: %d\n" ,*num);
printf ("Dynamically Allocated Array Elements:\n");
int *arr =(int*) calloc (5, sizeof(int));
if(arr==NULL)
{
printf ("Memory Allocation Failed:\n");
return 1;
}
for (int i=0; i<5; i++)
{
arr[i]=i*10;
}
for (int i=0; i<5; i++)
{
printf ("%d\n", arr[i]);
}

1
int *resizedArr=(int*) realloc (arr,8*sizeof(int));
if(resizedArr==NULL)
{
printf ("Memory Allocation Failed:\n");
free(num);
free(arr);
return 1;
}
for (int i=5; i<8; i++)
{
resizedArr[i]=i*10;
}
printf ("Resized Array Elements:\n");
for (int i=0; i<8; i++)
{
printf ("%d\n", resizedArr[i]);
}
}

2
OUTPUT:
Dynamically collocated integers:
0
10
20
30
Resized array elements are:
0
10
20
30
40
50
60
70

3
Lab Program -2
IMPLEMENTATION OF RECURSIVE FUNCTIONS
2.a . GCD of a Number

#include<stdio.h>
#include<stdlib.h>
int gcd (int a, int b)
{
if(b==0)
return a;
else
return gcd (b, a%b);
}
int main()
{
int num1, num2;
printf ("Enter two positive integers:\n");
scanf ("%d %d", &num1, &num2);
if(num1<0||num2<0)
{
Printf ("Please enter positive integer\n");
return 1;
}
printf ("GCD of %d and %d is %d\n", num1, num2, gcd (num1, num2));
return 0;
}

4
OUTPUT:
Enter two positive integers:24 32
GCD of 24 and 32 is :
8

5
2.b. Recursive function using Tower of Hanoi

#include<stdio.h>
#include<stdlib.h>
int toh (int n, char source, char auxiliary, char destination)
{
if(n==1)
{
printf ("move disk 1 from %c to %c\n", source, destination);
return 0;
}
toh (n-1, source, destination, auxilary);
printf ("move disk %d from %c to %c\n", n, source, destination);
toh (n-1, auxilary, source, destination);
}
int main ()
{
int n;
char A, B, C;
printf ("enter the number of disks:\t");
scanf ("%d", &n);
if(n<=0)
{
printf ("number of disks should be a +ve integers:\n");
return 1;
}
toh (n, 'A','B','C');
return 0;
}

6
OUTPUT:
Enter the number of disks: 4
move disk 1 from A to B
move disk 2 from A to C
move disk 1 from B to C
move disk 3 from A to B
move disk 1 from C to A
move disk 2 from C to B
move disk 1 from A to B
move disk 4 from A to C
move disk 1 from B to C
move disk 2 from B to A
move disk 1 from C to A
move disk 3 from B to C
move disk 1 from A to B
move disk 2 from A to C
move disk 1 from B to C

7
Lab Program -3
IMPLEMENTATION OF STACK OPERATIONS

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int s[MAX];
int top=-1;
void push();
int pop();
void display();
int isoverflow();
int underflow();
int main()
{
int ch, e;
do
{
printf ("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf ("Enter your choice:");
scanf ("%d", &ch);
switch(ch)
{
case 1: if(isoverflow())
printf ("Stack overflow");
else
push();
break;
case 2: if(isunderflow())
printf ("Stack underflow");

8
else
{
e=pop();
printf ("Deleted element is: %d", e);
printf("\n");
}
break;
case 3: if(isunderflow())
printf ("Stack underflow");
else
display();
break;
case 4: exit (0);
default: printf ("Invalid choice\n");
}
} while(ch!=4);
return 0;
}
int isoverflow()
{
if(top==MAX-1)
return 1;
else
return 0;
}
int isunderflow()
{
if(top==-1)
return 1;
else

9
return 0;
}
void push()
{
int e;
printf ("Enter the element to be pushed:");
scanf ("%d",&e);
s[++top]=e;
}
int pop()
{
int e;
e=s[top--];
return e;
}
void display()
{
int i;
printf ("Content of the stack are:\n");
for(i=top; i>=0; i--)
{
printf("%d\n",s[i]);
}
}

10
OUTPUT:
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1
Enter the element to be pushed:11
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1
Enter the element to be pushed: 12
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1
Enter the element to be pushed: 13
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1
Enter the element to be pushed: 14
1.Push
2.Pop
3.Display
4.Exit

11
Enter your choice: 1
Enter the element to be pushed: 15
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1
Stack is Overflow
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 3
Content of the stack are:
11
12
13
14
15
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 11
1.Push
2.Pop
3.Display
4.Exit

12
Enter your choice: 2
Deleted element is: 12
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 13
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 14
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 15
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 2
Stack is Underflow
1.Push
2.Pop
3.Display
4.Exit

13
Enter your choice: 1
Enter the element to be pushed: 21
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 3
Content of the stack are:
21
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 4

14
Lab Program -4
IMPLEMENTATION OF INFIX TO POSTFIX CONVERSION

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
char s [20];
int top=-1;
void push(char);
char pop();
int prior(char);
int main()
{
char infix[20], postfix[20];
int i, j=0;
printf ("enter the infix expression:\n");
scanf ("%s", &infix);
push('#');
for(i=0; infix[i]!='\0'; i++)
{
if(isalnum(infix[i]))
postfix[j++]=infix[i];
else if(infix[i]=='(')
push(infix[i]);
else if(infix[i]==')')
{
while (s[top]!='(')
postfix[j++]=pop();
pop();
}

15
else
{
while(prior(s[top])>=prior(infix[i]))
postfix[j++]=pop();
push(infix[i]);
}
}
while(s[top]!='#')
postfix[j++]=pop();
postfix[j]='\0';
printf ("postfix expression is: %s\n", postfix);
return 0;
}
void push(char x)
{
s[++top] =x;
}
char pop()
{
return s[top--];
}
int prior(char x)
{
if(x=='^')
return 3;
if(x=='*'||x=='%'||x=='/')
return 2;

if(x=='+'||x=='-')
return 1;

16
if(x=='('||x=='#')
return 0;
}

17
OUTPUT:
Enter the infix expression:
a*(b+c)*d
postfix expression is:
abc+*d*

18
Lab Program -5
IMPLEMENTATION OF QUEUE OPERATIONS

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int q[MAX], f=-1, r=-1;
void insert();
void rem();
void display();
int main()
{
int ch;
do
{
printf ("\n1.Insert\n2.Remove\n3.Display\n4.Exit\n");
printf ("Enter your choice:");
scanf ("%d", &ch);
switch(ch)
{
case 1: insert();
break;
case 2: rem();
break;
case 3: display();
break;
case 4: exit (0);
default: printf ("Your choice is Invalid choice:");
}
}while(ch!=4);

19
return 0;
}
void insert()
{
int e;
if(r==MAX-1)
{
printf ("Queue is Full");
return;
}
printf ("Enter elements to be inserted:");
scanf ("%d", &e);
r=r+1;
q[r]=e;
if(f==-1)
f=0;
}
void rem()
{
int e;
if(f==-1)
{
printf ("Queue is Empty");
return;
}
e=q[f];
if(f==r)
f=r=-1;
else
f=f+1;

20
printf ("Deleted element is: %d", e);
}
void display()
{
int i;
if(f==-1)
{
printf ("Queue is Empty");
return;
}
printf ("Elements of Queue are:\n");
for(i=f; i<=r; i++)
{
printf ("%d\n", q[i]);
}
}

21
OUTPUT:
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 1
Enter the element to be inserted: 15
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 1
Enter the element to be inserted: 20
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 1
Enter the element to be inserted: 25
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 1
Enter the element to be inserted: 30
1.Insert
2.Remove
3.Display
4.Exit

22
Enter your choice: 1
Enter the element to be inserted: 35
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 1
Queue is Full
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 3
Elements of queue are:
35
30
25
20
15
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 15
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 2

23
Deleted element is: 20
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 25
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 30
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 2
Deleted element is: 35
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 2
Queue is Empty
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 1

24
Enter the element to be pushed: 22
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 3
Content of the stack are: 22
1.Insert
2.Remove
3.Display
4.Exit
Enter your choice: 4

25
Lab Program -6
IMPLEMENTATION OF CIRCULAR QUEUE OPERATIONS

#include<stdio.h>
#include<stdlib.h>
#define MAX 3
char cq[20];
int f=-1, r=-1;
void insert();
void del();
void display();
int main()
{
int ch;
do
{
printf ("\n1.Insert\n2.Delate\n3.Display\n4.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 ("Your choice is Invalid choice");
}

26
} while (ch! =4);
return 0;
}
void insert()
{
char e;
if((r+1) %MAX==f)
{
printf ("Circular queue is Full");
return;
}
printf ("Enter the item:");
scanf (" %c", &e);
r=(r+1) %MAX;
cq[r]=e;
if(f==-1)
f=0;
}
void del()
{
char e;
if(f==-1)
{
printf ("Circular queue is Empty");
return;
}
else
{
e=cq[f];
if(f==r)

27
f=r=-1;
else
f=(f+1) %MAX;
printf ("Deleted item is: %c", e);
}
}
void display()
{
int i;
if(f==-1)
{
printf ("Circular queue is Full");
return;
}
else
{
i=f;
while (i! =r)
{
printf ("%c\n", cq[i]);
i=(i+1) %MAX;
}
printf ("%c\n", cq[r]);
}
}

28
OUTPUT:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the item: b
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the item: i
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the item: t
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Circular queue is Full
1.Insert
2.Delete
3.Display
4.Exit

29
Enter your choice: 3
t
i
b
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 2
Deleted item is: b
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 2
Deleted item is: i
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 2
Enter the item: t
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 2
Circular queue is Empty

30
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the item: g
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 3
g
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 4

31
Lab Program -7
IMPLEMENTATION OF SINGLY LINKED LIST

#include<stdio.h>
#include<stdlib.h>
typedef struct NODE{
char usn [10];
char name [10];
char branch [10];
int sem;
long long int phno;
struct NODE *next;
}node;
node *first=NULL;
node* read_data();
void front_insert();
void front_del();
void end_insert();
void end_del();
void display();
int main()
{
int ch;
do
{

printf("\n1.Front_insert\n2.Front_delete\n3.End_insert\n4.End_delete\n5.Display\n6.Exit\n");
printf ("Enter your choice:");
scanf ("%d", &ch);
switch(ch)

32
{
case 1: front_insert();
break;
case 2: front_del();
break;
case 3: end_insert();
break;
case 4: end_del();
break;
case 5: display();
break;
case 6: exit (0);
default: printf ("Your choice is Invalid");
}
}while(ch!=6);
return 0;
}
node* read_data()
{
node *nn;
nn=(node*) malloc(sizeof(node));
printf ("Enter the usn:");
scanf ("%s", nn->usn);
printf ("Enter the name:");
scanf ("%s", nn->name);
printf ("Enter the branch:");
scanf ("%s", nn->branch);
printf ("Enter the semester:");
scanf ("%d", &nn->sem);
printf ("Enter the phno:");

33
scanf ("%lld", &nn->phno);
nn->next=NULL;
return(nn);
}
void front_insert()
{
node *temp;
temp=(node*) malloc(sizeof(node));
temp=read_data();
if(first==NULL)
first=temp;
else
{
temp->next=first;
first=temp;
}
}
void end_insert()
{
node *temp, *curr;
temp=(node*) malloc(sizeof(node));
temp=read_data();
if(first==NULL)
first=temp;
else
{
curr=first;
while(curr->next!=NULL)
curr=curr->next;
curr->next=temp;

34
}
}
void front_del()
{
node *temp;
if(first==NULL)
printf ("List is empty");
else
{
temp=first;
first=first->next;
free(temp);
}
}
void end_del()
{
node *curr, *prev;
if(first==NULL)
printf ("List is empty");
if(first->next==NULL)
{
curr=first;
first=NULL;
free(curr);
}
else
{
curr=first;
while(curr->next!=NULL)
{

35
prev=curr;
curr=curr->next;
}
prev->next=NULL;
free(curr);
}
}
void display()
{
int count=0;
node *temp;
if(first==NULL)
printf ("List is empty");
else
{
temp=first;
while(temp!=NULL)
{
printf ("\n USN is: %s", temp->usn);
printf ("\n NAME is: %s", temp->name);
printf ("\n BRANCH is: %s", temp->branch);
printf ("\n SEMESTER is: %d", temp->sem);
printf ("\n PHONE NO. is: %lld", temp->phno);
count++;
temp=temp->next;
}
printf ("\n Number of nodes: %d", count);
printf ("\n");
}
}

36
OUTPUT:
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 1
Enter usn: 10
Enter name: ram
Enter branch: mba
Enter semester: 1
Enter phno.:1234567892
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 1
Enter usn: 20
Enter name: madhavi
Enter branch: mca
Enter semester: 2
Enter phno.:9876543219
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display

37
6.Exit
Enter your choice: 1
Enter usn: 30
Enter name: manas
Enter branch: btech
Enter semester: 2
Enter phno.: 92435678124
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 5
USN is: 30
NAME is: manas
BRANCH is: btech
SEMSTER is: 2
PHONE NO. is: 92435678124
USN is: 20
NAME is: madhavi
BRANCH is: mcs
SEMSTER is: 2
PHONE NO. is: 9876543219
USN is: 10
NAME is: ram
BRANCH is: mba
SEMSTER is: 1
PHONE NO. is: 1234567892
Number of nodes: 3

38
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 2
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 5
USN is: 20
NAME is: madhavi
BRANCH is: mca
SEMSTER is: 2
PHONE NO. is: 9876543219
USN is: 10
NAME is: ram
BRANCH is: mba
SEMSTER is: 1
PHONE NO. is: 1234567892
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit

39
Enter your choice: 4
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 5
USN is: 20
NAME is: madhavi
BRANCH is: mca
SEMSTER is: 2
PHONE NO. is: 9876543219
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 3
Enter usn: 40
Enter name: rajesh
Enter branch: mca
Enter semester: 1
Enter phno.: 8675662677
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display

40
6.Exit
Enter your choice: 5
USN is: 40
NAME is: rajesh
BRANCH is: mca
SEMSTER is: 1
PHONE NO. is: 8675662677
USN is: 20
NAME is: madhavi
BRANCH is: mca
SEMSTER is: 2
PHONE NO. is: 9876543219
Numbers of nodes: 2
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 6

41
Lab Program -8
IMPLEMENTATION OF DOUBLY LINKED LIST

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char ssn [10];
char name [10];
char dept [10];
char des [10];
int sal;
long long int phno;
struct node *left;
struct node *right;
}node;
node *first=NULL;
node *read_data();
void front_insert();
void end_insert();
void front_delete();
void end_delete();
void display();
void main()
{
int ch;
do
{
printf("\n1.Front_insert\n2.End_insert\n3.Front_delete\n4.End_delete\n5.Display\n6.Exit\n");
printf ("Enter your choice:");

42
scanf ("%d", &ch);
switch(ch)
{
case 1: front_insert();
break;
case 2: end_insert();
break;
case 3: front_delete();
break;
case 4: end_delete();
break;
case 5: display();
break;
case 6: exit (0);
default: printf ("Your choice is Invalid");
}
}while(ch!=6);
return ;
}
node* read_data()
{
node *nn;
nn=(node*) malloc (sizeof(node));
printf ("Enter SSN of employee:");
scanf ("%s", nn->ssn);
printf ("Enter Name:");
scanf ("%s", nn->name);
printf ("Enter Department:");
scanf ("%s", nn->dept);
printf ("Enter Designation:");

43
scanf ("%s", nn->des);
printf ("Enter Salary:");
scanf ("%d", &nn->sal);
printf ("Enter Phno:");
scanf ("%lld", &nn->phno);
nn->left=NULL;
nn->right=NULL;
return(nn);
}
void front_insert()
{
node *temp;
temp=read_data();
if(first==NULL)
first=temp;
else
{
temp->right=first;
first->left=temp;
first=temp;
}
}
void end_insert()
{
node *temp, *curr;
temp=read_data();
if(first==NULL)
first=temp;
else
{

44
curr=first;
while(curr->right!=NULL)
curr=curr->right;
curr->right=temp;
temp->left=curr;
}
}
void front_delete()
{
node *temp;
if(first==NULL)
printf ("List is Empty");
else if(first->right==NULL)
{
temp=first;
first=NULL;
free(temp);
}
else
{
temp=first;
first=first->right;
first->left=NULL;
free(temp);
}
}
void end_delete()
{
node *temp, *prev, *curr;
if(first==NULL)

45
printf ("List is Empty");
else if(first->right==NULL)
{
temp=first;
first=NULL;
free(temp);
}
else
{
prev=curr=first;
while(curr->right!=NULL)
{
prev=curr;
curr=curr->right;
}
free(curr);
prev->right=NULL;
}
}
void display()
{
node *temp;
int n=0;
if(first==NULL)
printf ("List is Empty");
else
{
temp=first;
while(temp!=NULL)
{

46
printf ("\n SSN is: %s", temp->ssn);
printf ("\n Name is: %s", temp->name);
printf ("\n Dept is: %s", temp->dept);
printf ("\n Des is: %s", temp->des);
printf ("\n Salery is: %d", temp->sal);
printf ("\n Phno. is: %lld\n", temp->phno);
n++;
temp=temp->right;
}
printf ("Number of nodes is: %d", n);
printf ("\n");
}
}

47
OUTPUT:
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 1
Enter usn: 10
Enter name: Rama
Enter branch: mca
Enter semester: 1
Enter phno.:1234567892
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 1
Enter usn: 20
Enter name: Sangeetha
Enter branch: mba
Enter semester: 2
Enter phno.:9876543219
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display

48
6.Exit
Enter your choice: 1
Enter usn: 30
Enter name: radha
Enter branch: btech
Enter semester: 2
Enter phno.: 92435678124
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 5
USN is: 30
NAME is: radha
BRANCH is: btech
SEMSTER is: 2
PHONE NO. is: 92435678124
USN is: 20
NAME is: Sangeetha
BRANCH is: mba
SEMSTER is: 2
PHONE NO. is: 9876543219
USN is: 10
NAME is: Rama
BRANCH is: mca
SEMSTER is: 1
PHONE NO. is: 1234567892
Number of nodes: 3

49
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 2
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 5
USN is: 20
NAME is: Sangeetha
BRANCH is: mba
SEMSTER is: 2
PHONE NO. is: 9876543219
USN is: 10
NAME is: Rama
BRANCH is: mca
SEMSTER is: 1
PHONE NO. is: 1234567892
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit

50
Enter your choice: 4
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 5
USN is: 20
NAME is: Sangeetha
BRANCH is: mba
SEMSTER is: 2
PHONE NO. is: 9876543219
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 3
Enter usn: 40
Enter name: Karna
Enter branch: mca
Enter semester: 1
Enter phno.: 8675442699
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display

51
6.Exit
Enter your choice: 5
USN is: 40
NAME is: Karna
BRANCH is: mca
SEMSTER is: 1
PHONE NO. is: 8675442699
USN is: 20
NAME is:Sangeetha
BRANCH is: mba
SEMSTER is: 2
PHONE NO. is: 9876543219
Numbers of nodes: 2
1.Front_insert
2.Front_delete
3.End_insert
4.End_delete
5.Display
6.Exit
Enter your choice: 6

52
Lab Program -9
IMPLEMENTATION OF TREE TRAVERSAL USING DFS APPROACH.
#include <stdio.h>
int a[10][10], n, v[10], source;
void input();
void dfs (int);
void output();
int main()
{
input();
dfs(source);
output();
return 0;
}
void input()
{
int i, j;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter the source vertex: ");
scanf("%d", &source);
for (i = 0; i < n; i++)
v[i] = 0;
}
void dfs(int s)
{

53
int k;
v[s] = 1;
for (k = 0; k < n; k++)
{
if (a[s][k] == 1 && v[k] == 0)
{
printf("%d -> %d\n", s, k);
dfs(k);
}
}
}
void output()
{
int i;
for (i = 0; i < n; i++)
{
if (v[i] == 0)
printf("%d Not reachable\n", i);
else
printf ("%d Reachable\n", i);
}
}

54
OUTPUT:
Enter the number of nodes: 5
Enter the adjacency matrix:
01100
10001
10010
00101
01010
Enter the source vertex: 1
1 -> 0
0 -> 2
2 -> 3
3 -> 4
0 Reachable
1 Reachable
2 Reachable
3 Reachable
4 Reachable

55
Lab Program -10
IMPLEMENTATION OF GRAPH TO TO FIND MINIMUM SPANING TREE USING
KRUSKALS ALGORITHM.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];
int find(int);
int uni(int, int);
void main()
{
printf("\n\t Implementation of Kruskal's Algorithm\n");
printf("\n Enter the no. of vertices:");
scanf("%d", & n);
printf("\ n Enter the cost adjacency matrix:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", & cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while (ne < n)
{
for (i = 1, min = 999; i <= n; i++)
{

56
for (j = 1; j <= n; j++)
{
if (cost[i][j] < min)
{
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = find(u);
v = find(v);
if (uni(u, v))
{
printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999;
}
printf("\n\t Minimum cost = %d\n", mincost);
getch();
}

int find(int i)
{
while (parent[i])
i = parent[i];
return i;
}

57
int uni(int i, int j)
{
if (i != j)
{
parent[j] = i;
return 1;
}
return 0;
}

58
OUTPUT:

Implementation of Kruskal's Algorithm

Enter the no. of vertices:7

Enter the cost adjacency matrix:


0 28 999 999 999 10 999
28 0 16 999 999 999 14
999 16 0 12 999 999 999
999 999 12 0 22 999 18
999 999 999 22 0 25 24
10 999 999 999 25 0 999
999 14 999 18 24 999 0
The edges of Minimum Cost Spanning Tree are
1 edge (1,6) =10
2 edge (3,4) =12
3 edge (2,7) =14
4 edge (2,3) =16
5 edge (4,5) =22
6 edge (5,6) =25

Minimum cost = 99

59

You might also like