0% found this document useful (0 votes)
65 views88 pages

Adil Dsa

The document contains C code written by Adil Inamdar to check for matching parentheses in an expression. The code takes in an expression as a string, iterates through each character and increments a counter when it sees an opening parenthesis and decrements the counter when it sees a closing parenthesis. If the counter is ever less than 0 during iteration, it means there are more closing parentheses than opening ones, so the expression does not have matching parentheses.

Uploaded by

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

Adil Dsa

The document contains C code written by Adil Inamdar to check for matching parentheses in an expression. The code takes in an expression as a string, iterates through each character and increments a counter when it sees an opening parenthesis and decrements the counter when it sees a closing parenthesis. If the counter is ever less than 0 during iteration, it means there are more closing parentheses than opening ones, so the expression does not have matching parentheses.

Uploaded by

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

/*Program to find gcd of 2 numbers

Name-Adil Inamdar
USN-2KE21MCA47-T
18/03/22*/
#include <stdio.h>
int gcd(int a, int b);
int main() {
int a, b;
printf("program executed by Adil\n");
printf("Enter two positive integers:\n");
scanf("%d %d", &a, &b);
printf("G.C.D of %d and %d is %d.", a, b, gcd(a, b));
return 0;
}

int gcd(int a, int b) {


if (b != 0)
return gcd(b, a % b);
else
return a;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
Enter two positive integers:
10
5
G.C.D of 10 and 5 is 5.

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to find nth fibonacci number
Name-Adil Inamdar
USN-2KE21MCA47-T
18/03/22*/
#include <stdio.h>
int fib(int);

int main()
{
int num;
int result;
printf("program executed by Adil\n");
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fib(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fib(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fib(num - 1) + fib(num - 2));
}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
Enter the nth number in fibonacci series: 6
The 6 number in fibonacci series is 8

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to solve Tower OF Hanoi problem
Name-Adil Inamdar
USN-2KE21MCA47-T
18/03/22*/
#include<stdio.h>
void towers(int, char, char, char);

int main()
{
int num;
printf("program executed by Adil\n");
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char src, char dst, char aux)
{
if (num == 1)
{
printf("\n Move disk 1 from %c to %c",src, dst);
return;
}
towers(num - 1,src, aux, dst);
printf("\n Move disk %d from %c to %c", num, src, dst);
towers(num - 1, aux, src, dst);
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from A to C


Move disk 2 from A to B
Move disk 1 from C to A
Move disk 3 from A to C
Move disk 1 from B to C
Move disk 2 from B to A
Move disk 1 from C to B

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to find factorial of a number
Name-Adil Inamdar
USN-2KE21MCA47-T
18/03/22*/
#include<stdio.h>
int fact(int);
int main()
{
int num, f;
printf("program executed by Adil\n");
printf("Enter any integer number:");
scanf("%d",&num);
f =fact(num);
printf("factorial of %d is: %d",num, f);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
Enter any integer number:5
factorial of 5 is: 120

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to check whether giver string is palindrome
Name-Adil Inamdar
USN-2KE21MCA47-T
18/03/22*/
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#define MAX 50
int top=-1,front=0;
int stack[MAX];
void push(char);
void pop();
void main()
{
int i,choice;
char s[MAX],b;
printf("program executed by Adil\n");
printf("Enter a word to check if it is a palindrome\n");
scanf("%s",s);
for(i=0;s[i]!='\0';i++)
{
b=s[i];
push(b);
}
for(i=0;i<(strlen(s)/2);i++)
{
if(stack[top]==stack[front])
{
pop();
front++;
}
else
{
printf("The entered word is not a palindrome");
break;
}
}
if((strlen(s)/2)==front)
printf("The entered word is a palindrome");
front=0;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
top=-1;
}
void push(char a)
{
top++;
stack[top]=a;
}
void pop()
{
top--;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
Enter a word to check if it is a palindrome
madam
The entered word is a palindrome

program executed by Adil


Enter a word to check if it is a palindrome
college
The entered word is not a palindrome

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to perform operations of stack
Name-Adil Inamdar
USN-2KE21MCA47-T
18/03/22*/
#include<stdio.h>
#include<conio.h>
#define MAX 5
struct stack
{
int items[MAX];
int temp;

};
typedef struct stack *s1;
void push(s1 p, int x);
int pop(s1 p);
int empty(s1 p);
int full(s1 p);
void main()
{
struct stack s;
int i,elem,ch,done=1;
int temp;
s.temp=-1;
while (done)
{
printf("\nProgram executed by Adil\n");
printf(" 1.insert\n 2.Delete\n 3.Display\n 4.EXIT\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter Element\n");
scanf("%d",&elem);
push(&s,elem);
break;

case 2: temp=pop(&s);
printf("item deleted is %d",temp);
break;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
case 3: if (s.temp==-1)
printf("Empty");
else
{
for(i=s.temp;i>=0;i--)
printf("%d\n",s.items[i]);
}
break;
case 4:done=0;
break;
default : printf("invalid choice\n");
}

}
}

void push(s1 p, int x)


{

if(full(p))
{
printf("ERROR OVERFLOW\n");
return ;
}
++(p->temp);
p->items[p->temp]=x;
}

int full(s1 p)
{
if(p->temp==MAX-1)
return 1;
else
return 0;
}

int pop (s1 p)


{
int temp;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
if(empty(p))
return -1;
temp=p->items[p->temp];
--(p->temp);
return temp;
}
int empty(s1 p)
{
if (p->temp==-1)
return 1;
else
return 0;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:1
Enter Element
22

Program executed by Adil


1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:1
Enter Element
33

Program executed by Adil


1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:1
Enter Element
44

Program executed by Adil


1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:1
Enter Element
55

Program executed by Adil


1.insert
2.Delete
3.Display

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
4.EXIT
Enter your choice:1
Enter Element
66

Program executed by Adil


1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:1
Enter Element
77
ERROR OVERFLOW

Program executed by Adil


1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:3
66
55
44
33
22

Program executed by Adil


1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:2
item deleted is 66
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:2

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
item deleted is 55
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:2
item deleted is 44
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:2
item deleted is 33
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:2
item deleted is 22
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:2
item deleted is -1
Program executed by Adil
1.insert
2.Delete
3.Display
4.EXIT
Enter your choice:5
invalid choice

Program executed by Adil


1.insert
2.Delete

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
3.Display
4.EXIT
Enter your choice:4

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to convert infix expression to postfix expression
Name-Adil Inamdar
USN-2KE21MCA47-T
28/03/22*/
#include<stdio.h>

#include<conio.h>

char stack[100];
int top = -1;
void push(char x) {
stack[++top] = x;

}
char pop() {
if (top == -1)
return -1;
else
return stack[top--];

}
int priority(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
if (x == '^')
return 3;
return 0;

}
int main() {
printf("Executed by Adil\n");
char exp[100];
char * e, x;
printf("enter the expression::");
scanf("%s", exp);
printf("\n");

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
e = exp;
while ( * e != '\0') {
if (isalnum( * e))
printf("%c", * e);
else if ( * e == '(')
push( * e);
else if ( * e == ')') {
while (x = pop() != '(')
printf("%c", x);

} else {
while (priority(stack[top]) >= priority( * e))
printf("%c", pop());
push( * e);

}
e++;
}
while (top != -1) {
printf("%c", pop());

}
return (0);

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Executed by Adil
enter the expression::a+b-c

ab+c-

Executed by Adil
enter the expression::a+b-c/d

ab+cd/-

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program for paranthesis matching
Name-Adil Inamdar
USN-2KE21MCA47-T
28/03/22*/
#include<stdio.h>
int main()
{
char exp[50];
int x=0, i=0;
printf("program executed by Adil\n");
printf("enter expression:");
scanf("%s",exp);
while(exp[i]!='\0')
{
if(exp[i]=='(')
{
x++;
}
else if(exp[i]==')')
{
x--;
if(x<0)
break;
}
i++;
}
if(x==0)
{
printf("expression is matching");
}
else
{
printf("expression is not matching");
}
return 0;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
enter expression:a(b+c)(c+d)
expression is matching

program executed by Adil


enter expression:(a-(b*c)
expression is not matching

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to perform postfix evaluation
Name-Adil Inamdar
USN-2KE21MCA47-T
04/04/22*/
#include<stdio.h>
int stack[20];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("program executed by Adil\n");
printf("enter the expression::");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isdigit(*e))
{
num=*e-48;
push(num);
}
else
{
n1=pop();
n2=pop();
switch(*e)
{
case '+':n3=n1+n2;
break;
case '-':n3=n2-n1;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
break;
case '*':n3=n2*n1;
break;
case '/':n3=n2/n1;
break;

}
push(n3);
}
e++;

printf("\n the result of expression %s=%d\n",exp,pop());


return 0;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
program executed by Adil
enter the expression::32+4-

the result of expression 32+4-=1

program executed by Adil


enter the expression::43*6-

the result of expression 43*6-=6

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to convert infix expression to prefix expression
Name-Adil Inamdar
USN-2KE21MCA47-T
04/04/22*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>
#define MAX 20
void push(int);
char pop();
void infix_to_prefix();
int precedence (char);
char stack[20],infix[20],prefix[20];
int top = -1;

int main()
{
printf("Prgram Executed by Adil\n");
printf("INPUT THE INFIX EXPRESSION : ");
scanf("%s",infix);
infix_to_prefix();
return 0;
}
void push(int pos)
{
if(top == MAX-1)
{
printf("\nSTACK OVERFLOW\n");
}
else {
top++;
stack[top] = infix[pos];
}}
char pop()
{
char ch;
if(top < 0)
{
printf("\nSTACK UNDERFLOW\n");

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
exit(0);
}
else
{
ch = stack[top];
stack[top] = '\0';
top--;
return(ch);
}
return 0;
}
void infix_to_prefix()
{
int i = 0,j = 0;
strrev(infix);
while(infix[i] != '\0')
{
if(infix[i] >= 'a' && infix[i] <= 'z')
{
prefix[j] = infix[i];
j++;
i++;
}
else if(infix[i] == ')' || infix[i] == '}' || infix[i] == ']')
{
push(i);
i++;
}
else if(infix[i] == '(' || infix[i] == '{' || infix[i] == '[')
{
if(infix[i] == '(')
{
while(stack[top] != ')')
{
prefix[j] = pop();
j++;
}
pop();
i++;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
else if(infix[i] == '[')
{
while(stack[top] != ']')
{
prefix[j] = pop();
j++;
}
pop();
i++;
}
else if(infix[i] == '{')
{
while(stack[top] != '}')
{
prefix[j] = pop();
j++;
}
pop();
i++;
}}
else
{
if(top == -1)
{
push(i);
i++;
}
else if( precedence(infix[i]) < precedence(stack[top]))
{
prefix[j] = pop();
j++;
while(precedence(stack[top]) > precedence(infix[i])){
prefix[j] = pop();
j++;
if(top < 0) {
break;
}}
push(i);
i++;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
else if(precedence(infix[i]) >= precedence(stack[top]))
{
push(i);
i++;
}}}
while(top != -1)
{
prefix[j] = pop();
j++;
}
strrev(prefix);
prefix[j] = '\0';
printf("EQUIVALENT PREFIX NOTATION : %s ",prefix);
}
int precedence(char alpha)
{
if(alpha == '+' || alpha =='-')
{
return(1);
}
if(alpha == '*' || alpha =='/')
{
return(2);
}
return 0;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Prgram Executed by Adil
INPUT THE INFIX EXPRESSION : a+c-d
EQUIVALENT PREFIX NOTATION : -+acd

Prgram Executed by Adil


INPUT THE INFIX EXPRESSION : a/b-c
EQUIVALENT PREFIX NOTATION : -/abc

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to perform operations on simple queue
Name-Adil Inamdar
USN-2KE21MCA47-T
04/04/22*/
#include<stdio.h>
#define max 5
int q[max];
int front=-1;
int rear=-1;
void insert(int item)
{
if(rear==max-1)
{
printf("Queue is overflow\n");
return;
}
if(front==-1)
front++;
q[++rear]=item;
}
void delete()
{
int item;
if(front==-1)
{
printf("Queue is underflow\n");
return;
}
item=q[front];
if(front==rear)
{
front=rear=-1;
printf("empty\n");
}
else
front++;
printf("deleted item is %d\n",item);
}

void display()

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
{
int temp;
if(front==-1)
{
printf("Queue is empty\n");
}
for(temp=front;temp<=rear;temp++)
printf("%d\n",q[temp]);
}
void main()
{

int choice,item;
while(1)
{
printf("Program is Executed by Adil\n");
printf("Queue implementation\n");
printf("1.insert\t2.Delete\t3.dislpay\t4.exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the element to be inserted:\n");
scanf("%d",&item);
insert(item);
break;
case 2:delete();
break;
case 3:printf("the contents of the queue are:\n");
display();
break;
case 4:exit(0);
default:printf("invalid choice\n");
break;
}
}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
1
Enter the element to be inserted:
11
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
1
Enter the element to be inserted:
22
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
1
Enter the element to be inserted:
33
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
1
Enter the element to be inserted:
44
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
1
Enter the element to be inserted:
55
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
1

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Enter the element to be inserted:
66
Queue is overflow
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
3
the contents of the queue are:
11
22
33
44
55
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
2
deleted item is 11
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
2
deleted item is 22
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
2
deleted item is 33
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
2
deleted item is 44
Program is Executed by Adil
Queue implementation

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
2
empty
deleted item is 55
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
2
Queue is underflow
Program is Executed by Adil
Queue implementation
1.insert 2.Delete 3.dislpay 4.exit
Enter your choice
4

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to perform operations on circular queue
Name-Adil Inamdar
USN-2KE21MCA47-T
04/04/22*/
#include<stdio.h>
#include<conio.h>

#define max 2
int CQ[max];
int front=0,rear=-1,count=0;

void insert(int item)


{
if(count==max)
{
printf("Q is overflow!!!\n");
return;
}
count++;
rear=(rear+1)%max;
CQ[rear]=item;
printf("Element %d Inserted\n",item);
}
void delete()
{
int item;
if(count==0)
{
printf("Queue is Underflow!!!\n");
return;
}
count--;
item=CQ[front];
front=(front+1)%max;
printf("Deleted Element is %d\n",item);
}
void display()
{
int i,f;
if(count==0)

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
{
printf("Q is empty!!!\n");
return;
}
f=front;
rear=(rear+1)%max;
printf("CQ Element are\n");
for(i=1;i<=count;i++)
{
printf("%d\n",CQ[f]);
f=(f+1)%max;
}

}
void main()
{
printf("Program Executed by Adil\n\n");

int choice,item;
while(1)
{
printf("1.Insert\n2.Delete\n3.Dislpay\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the element to be inserted:\n");
scanf("%d",&item);
insert(item);
break;
case 2:delete();
break;
case 3:
display();
break;
case 4: exit(0);
default:printf("invalid choice");
break;
}}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program Executed by Adil

1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
1
Enter the element to be inserted:
11
Element 11 Inserted
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
1
Enter the element to be inserted:
22
Element 22 Inserted
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
1
Enter the element to be inserted:
33
Element 33 Inserted
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
1
Enter the element to be inserted:
44
Element 44 Inserted
1.Insert
2.Delete

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
3.Dislpay
4.Exit
Enter your choice
1
Enter the element to be inserted:
55
Element 55 Inserted
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
1
Enter the element to be inserted:
66
Q is overflow!!!
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
3
CQ Element are
11
22
33
44
55
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
2
Deleted Element is 11
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
1
Enter the element to be inserted:
66
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
3
CQ Element are
66
22
33
44
55
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
2
Deleted Element is 66
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
2
Deleted Element is 22
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
2
Deleted Element is 33
1.Insert
2.Delete
3.Dislpay
4.Exit

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Enter your choice
2
Deleted Element is 44
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
2
Deleted Element is 55
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
2
Queue is Underflow!!!
1.Insert
2.Delete
3.Dislpay
4.Exit
Enter your choice
4

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to perform operations on double ended queue
Name-Adil Inamdar
USN-2KE21MCA47-T
11/04/22*/
#include<stdio.h>
#include<process.h>
#include<conio.h>
#define MAX 30
typedef struct dequeue
{
int data[MAX];
int rear,front;
}dequeue;
void initialize(dequeue *p);
int empty(dequeue *p);
int full(dequeue *p);
void enqueueR(dequeue *p,int x);
void enqueueF(dequeue *p,int x);
int dequeueF(dequeue *p);
int dequeueR(dequeue *p);
void print(dequeue *p);
void main()
{
int i,x,op,n;
dequeue q;
initialize(&q);
do
{
printf("Program executed by Adil\n");
printf("\n1.Create\n2.Insert(rear)\n3.Insert(front)\n4.Delete(rear)");
printf("\n5.Delete(front)\n6.Display\n7.Exit\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter number of elements:");
scanf("%d",&n);
initialize(&q);
printf("\nEnter the data:\n");
for(i=0;i<n;i++)
{

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
}
break;
case 2: printf("\nEnter element to be inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
break;
case 3: printf("\nEnter the element to be inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueF(&q,x);
break;
case 4: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueR(&q);
printf("\nElement deleted is %d\n",x);
break;
case 5: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
x=dequeueF(&q);
printf("\nElement deleted is %d\n",x);
break;
case 6: print(&q);
break;
default: break;
}
}while(op!=7);
getch();
}
void initialize(dequeue *P)
{
P->rear=-1;
P->front=-1;
}
int empty(dequeue *P)
{
if(P->rear==-1)
return(1);
return(0);
}
int full(dequeue *P)
{
if((P->rear+1)%MAX==P->front)
return(1);
return(0);
}
void enqueueR(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->rear=(P->rear+1)%MAX;
P->data[P->rear]=x;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
}
void enqueueF(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->front=(P->front-1+MAX)%MAX;
P->data[P->front]=x;
}
}
int dequeueF(dequeue *P)
{
int x;
x=P->data[P->front];
if(P->rear==P->front)
initialize(P);
else
P->front=(P->front+1)%MAX;
return(x);
}
int dequeueR(dequeue *P)
{
int x;
x=P->data[P->rear];
if(P->rear==P->front)
initialize(P);
else
P->rear=(P->rear-1+MAX)%MAX;
return(x);
}
void print(dequeue *P)
{
if(empty(P))
{
printf("\nQueue is empty!!");

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
exit(0);
}
int i;
i=P->front;
while(i!=P->rear)
{
printf("\n%d",P->data[i]);
i=(i+1)%MAX;
}
printf("\n%d\n",P->data[P->rear]);
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:1

Enter number of elements:3

Enter the data:


11
22
33
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:3

Enter the element to be inserted:


Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:6

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
44
11
22
33
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:4

Element deleted is 33
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:5

Element deleted is 44
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:4

Element deleted is 22

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:5

Element deleted is 11
Program executed by Adil

1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Display
7.Exit
Enter your choice:4

Queue is empty!!

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to perform operations on priority queue
Name-Adil Inamdar
USN-2KE21MCA47-T
11/04/22*/
#include<stdio.h>
#define max 5
struct queue{
int a[max];
int f;
int r;
};
typedef struct queue *que;
void insert(que q,int x);
int del(que q);
void display(que q);
void main()
{
struct queue q;
q.f=0;
q.r=-1;
int ch,i,tmp,x,done=1;
printf("Program executed by Adil\n");
while(done)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the number\n");
scanf("%d",&x);
insert(&q,x);
break;
case 2:tmp=del(&q);
if(tmp!=0)
{
}
else
printf("Empty queue\n");
break;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
case 3:display(&q);
break;
case 4:done=0;
break;
default:printf("Invalid option\n");
break;
}
}
}
void insert(que q,int x)
{
int j;
if(q->r==max-1)
{
printf("Queue is full!!\n");
return;
}
j=q->r;
while(j>=0&&x<q->a[j])
{
q->a[j+1]=q->a[j];
j--;
}
q->a[j+1]=x;
q->r++;
}
int del(que q)
{
int temp;
if(q->r==-1)
{
printf("Queue is empty!!\n");
return;
}
printf("The deleted element is %d\n",q->a[q->f++]);
if(q->f>q->r)
{
q->f=0;
q->r=-1;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
}
void display(que q)
{
int i;
if(q->f==-1)
{
printf("Queue underflow!!\n");
return;
}
else
{
printf("Elements of queue are\n");
for(i=q->f;i<=q->r;i++)
printf("%d\n",q->a[i]);
}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program executed by Adil

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the number
44

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the number
11

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the number
33

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the number
77

1.Insert
2.Delete

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
3.Display
4.Exit
Enter your choice
1
Enter the number
22

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the number
55
Queue is full!!

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
3
Elements of queue are
11
22
33
44
77

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
2
The deleted element is 11

1.Insert
2.Delete

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
3.Display
4.Exit
Enter your choice
2
The deleted element is 22

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
2
The deleted element is 33

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
2
The deleted element is 44

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
2
The deleted element is 77

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
2
Queue is empty!!
Empty queue

1.Insert

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
2.Delete
3.Display
4.Exit
Enter your choice
4

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*implementation of stack using linked list
Name-Adil Inamdar
USN-2KE21MCA47-T
18/04/22*/
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
printf("\nProgram executed Adil\n");
printf("----------------------------------------------");
while(choice != 4)
{
printf("\nChose one from the below options...");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\nEnter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice\n");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element\n");
}
else
{
printf("Enter the value\n");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
}
printf("Item pushed\n");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow\n");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped\n");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Name-Adil Inamdar 2021-22
USN-2KE21MCA47-T
Program executed Adil
----------------------------------------------
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice
1
Enter the value
11
Item pushed

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
1
Enter the value
22
Item pushed

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
1
Enter the value
33
Item pushed

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Enter your choice
1
Enter the value
55
Item pushed

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
1
Enter the value
66
Item pushed

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
3
Printing Stack elements
66
55
33
22
11

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
2
Item popped

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice
2
Item popped

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
2
Item popped

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
2
Item popped

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
2
Item popped

Chose one from the below options...


1.Push
2.Pop
3.Show

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
4.Exit
Enter your choice
2
Underflow

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
5
Please Enter valid choice

Chose one from the below options...


1.Push
2.Pop
3.Show
4.Exit
Enter your choice
4
Exiting....
--------------------------------

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*implementation of queue using linked list
Name-Adil Inamdar
USN-2KE21MCA47-T
18/04/22*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
printf("\nProgram executed by Adil\n");
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
printf("\nEnter valid choice\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("Enter value\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values....\n");
while(ptr != NULL)
{
printf("%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program executed by Adil

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
1
Enter value
5

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
1
Enter value
10

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
1
Enter value
20

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
1
Enter value
30

1.insert an element

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
1
Enter value
40

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
3

printing values....
5
10
20
30
40

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
2

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
2

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Enter your choice
2

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
2

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
2

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
2

UNDERFLOW

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice
4

--------------------------------

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to use mergesort
Name-Adil Inamdar
USN-2KE21MCA47-T
16/05/22*/
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int i2,int j1,int j2);
void main()
{
int a[30],n,i;
printf("Program executed by Adil\n");
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("sorted array is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);

}
}
void merge(int a[],int i1,int i2,int j1,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=j1;
k=0;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
while(i<=i2 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=i2)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program executed by Adil
enter the number of elements
5
enter the elements
11
99
33
14
10
sorted array is
10
11
14
33
99

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Dijkstra's algorithm - finding shortest path from given vertex in a weighted graph
Name-Adil Inamdar
USN-2KE21MCA47-T
16/05/22*/
#include<stdio.h>
#define INFINITY 999
void dij(int n,int v,int cost[10][10],int dist[]);
void main()
{
int n,v,i,j,cost[10][10],dist[10];
printf("Program Executed by Adil\n");
printf("Enter the number of nodes\n");
scanf("%d",&n);
printf("enter the cost 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]=INFINITY;
}

printf("enter the source vertex\n");


scanf("%d",&v);
dij(n,v,cost,dist);
printf("Shortest path from %d to all vertices is\n",v);
for(i=1;i<n;i++)
{
if(i!=v)
{
printf("%d -> %d,its cost=%d\n",v,i,dist[i]);
}
}
}

void dij(int n,int v,int cost[10][10],int dist[])


{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w])&& !flag[w])
dist[w]=dist[u]+cost[u][w];

}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program Executed by Adil
Enter the number of nodes
5
enter the cost matrix
04080
40530
05067
83605
00750
enter the source vertex
2
Shortest path from 2 to all vertices is
2 -> 1,its cost=4
2 -> 3,its cost=5
2 -> 4,its cost=3

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*topological vertices graph
Name-Adil Inamdar
USN-2KE21MCA47-T
16/05/22*/
#include<stdio.h>
void main()
{
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
printf("Programe Executed by Adil\n");
printf("Enter number of vertices : ");
scanf("%d",&n);
printf("Enter adjcency matrix \n");
for(i=0;i<n;i++)
{
printf("Enter row %d\n",i+1);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
{
indeg[i]=0;
flag[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
indeg[i]=indeg[i]+a[j][i];
}
printf("Topological ordering is \n");
while(count<n)
{
for(k=0;k<n;k++)
{
if((indeg[k]==0)&&(flag[k]==0))
{
printf("%d\t",k+1);
flag[k]=1;
}
for(i=0;i<n;i++)
{

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
if(a[i][k]==1)
indeg[k]--;
}
}
count++;
}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Programe Executed by Adil
Enter number of vertices : 5
Enter adjcency matrix
Enter row 1
10100
Enter row 2
01110
Enter row 3
11010
Enter row 4
00110
Enter row 5
11001
Topological ordering is
1 2 3 4 5

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Program to use quicksort
Name-Adil Inamdar
USN-2KE21MCA47-T
16/05/22*/
#include<stdio.h>
void quicksort(int a[],int l,int r);
int partition(int a[],int l,int r);
void main()
{
int a[30],n,i;
printf("Program Executed by Adil\n");
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("soretd array is\n");
for(i=n-1;i>=0;i--)
printf("%d\n", a[i]);
}
void quicksort(int a[], int l, int r)
{
int s;
if(l<r)
{
s=partition(a,l,r);
quicksort(a,l,s-1);
quicksort(a,s+1,r);
}
}
int partition(int a[], int l, int r)
{
int p,i,j,temp;
p=a[l];
i=l;
j=r+1;
do
{
do

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
i++;
while(a[i]<p);
do
j--;
while(a[j]>p);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;

}
}
while(i<j);
a[l]=a[j];
a[j]=p;
return j;
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program Executed by Adil
enter the number of elements
5
enter the elements
22
66
44
15
2
soretd array is
66
44
22
15
2

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*kruskal's Algorithm
Name-Adil Inamdar
USN-2KE21MCA47-T
16/05/22*/
#include<stdio.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("Program Executed by Adil\n");
printf("Enter the number of vertices\n");
scanf("%d",&n);
printf("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++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
v=find(v);
if(uni(u,v))
{
printf("%d edge between (%d and %d)= %d\n",ne++,a,b,min);
mincost+=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost=%d",mincost);
}

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

int uni(int i,int j)


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

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
Program Executed by Adil
Enter the number of vertices
6
Enter the cost adjacency matrix
040076
402005
020705
007096
700903
655630
The Edges of minimum cost spanning tree are
1 edge between (2 and 3)= 2
2 edge between (5 and 6)= 3
3 edge between (1 and 2)= 4
4 edge between (2 and 6)= 5
5 edge between (4 and 6)= 6

Minimum cost=20

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
/*Depth First Search
Name-Adil Inamdar
USN-2KE21MCA47-T
16/05/22*/
#include<stdio.h>
int a[20][20],reach[20],n;
void dfs(int v);

void main()
{
printf("Program Executed by Adil\n\n");
int i,j,count=0;
printf("Enter the number of vertices\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
reach[i]=0;
a[i][j]=0;
}
printf("Enter adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
for(i=1;i<=n;i++)
if(reach[i])
count++;
if(count==n)

printf("\nGraph is connected\n");
else
printf("Graph is not connected");

void dfs(int v)
{
int i;
reach[v]=1;

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T
for(i=1;i<=n;i++)
if(a[v][i] &&!reach[i])
{
printf("\n %d ->%d\n",v,i);
dfs(i);

}
}

Name-Adil Inamdar 2021-22


USN-2KE21MCA47-T

You might also like