Giter Club home page Giter Club logo

ds_programs_3rd-sem's People

Watchers

 avatar

ds_programs_3rd-sem's Issues

P5_b

//TOWER OF HANOI

#include<stdio.h>
void towers_hanoi(int n)
{
int x;
for(x=1;x<(1<<n);x++)
printf("Move from Pole %d to Pole %d\n",(x&(x-1))%3,(((x |(x-1)) + 1)%3));
}

int main()
{
int m;
printf("\n Enter the Number of Disks : ");
scanf("%d",&m);
towers_hanoi(m);
return 0;
}

P6

// Design,DevelopandImplementamenudrivenPrograminCfor thefollowingoperationsonCircularQUEUEofCharacters(ArrayImplementation ofQueuewithmaximumsizeMAX)
// ⦁ InsertanElementontoCircularQUEUE
// ⦁ DeleteanElementfromCircular QUEUE
// ⦁ DemonstrateOverflowandUnderflowsituationsonCircularQUEUE
// ⦁ DisplaythestatusofCircularQUEUE
// ⦁ Exit
// Supporttheprogramwithappropriatefunctionsforeachoftheaboveoperations

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10

int q[10],front=0,rear=-1; void main()

{

int ch;

void insert(); void delet(); void display();

printf("\nCircular Queue operations\n"); printf("1.insert\n2.delete\n3.display\n4.exit\n"); while(1)
{

printf("Enter your choice:"); scanf("%d",&ch);
switch(ch)
{

case 1: insert(); break;

case 2: delet(); break;

case 3: display(); break;

case 4: exit(1);

default: printf("Invalid option\n");

}
}

}

void insert()

{

int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1)) printf("Queue is overflow\n");

else

{

printf("Enter element to be insert:"); scanf("%d",&x);
if(rear==max-1&&front>0)

{

rear=0;

q[rear]=x;

}

else

{

if((front==0&&rear==-1)||(rear!=front-1)) q[++rear]=x;

}

}

}

void delet()

{

int a; if((front==0)&&(rear==-1))
{

printf("Queue is underflow\n"); exit(1);

}

if(front==rear)

{

a=q[front]; rear=-1; front=0;

}

else if(front==max-1)

{
a=q[front];

front=0;

}

else a=q[front++];

printf("Deleted element is:%d\n",a);

}

void display()

{

int i,j; if(front==0&&rear==-1)
{

printf("Queue is underflow\n"); exit(1);

}

if(front>rear)

{

for(i=0;i<=rear;i++)

printf("\t%d",q[i]); for(j=front;j<=max-1;j++) printf("\t%d",q[j]);

printf("\nrear is at %d\n",rear); printf("\nfront is at %d\n",front);

}

else

{

for(i=front;i<=rear;i++)

{

printf("\t%d",q[i]);

}

printf("\nrear is at %d\n",rear); printf("\nfront is at %d\n",front);

}

printf("\n");

}

P9

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<math.h>
typedef struct node
{
int expo,coef;
struct node *next;
}node;
node * insert(node *,int, int);
node * create();
node * add(node *p1,node *p2);
int eval(node *p1);
void display(node *head);
node insert(nodehead,int expo1,int coef1)
{
node *p,*q;
p=(node *)malloc(sizeof(node));
p->expo=expo1;
p->coef=coef1;
p->next=NULL;
if(head==NULL)
{
head=p;
head->next=head;
return(head);
}
if(expo1>head->expo)
{
p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(expo1==head->expo)
{
head->coef=head->coef+coef1;
return(head);
}
q=head;
while(q->next!=head&&expo1>=q->next->expo)
q=q->next;
if(p->expo==q->expo)
q->coef=q->coef+coef1;
else
{
p->next=q->next;
q->next=p;
}
return(head);
}
node *create()
{
int n,i,expo1,coef1;
node *head=NULL;
printf("\n\nEnter no of terms of polynomial==>");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter coef & expo==>");
scanf("%d%d",&coef1,&expo1);
head=insert(head,expo1,coef1);
}
return(head);
}
node *add(node *p1,node *p2)
{
node *p;
node *head=NULL;
printf("\n\n\nAddition of polynomial==>");
p=p1->next;
do
{
head=insert(head,p->expo,p->coef);
p=p->next;
}
while(p!=p1->next);
p=p2->next;
do
{
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p2->next);
return(head);
}
int eval(node *head)
{
node p;
int x,ans=0;
printf("\n\nEnter the value of x=");
scanf("%d",&x);
p=head->next;
do
{
ans=ans+p->coef
pow(x,p->expo);
p=p->next;
}while(p!=head->next);
return(ans);
}
void display(node *head)
{
node *p,*q;
int n=0;
q=head->next;
p=head->next;
do
{
n++; q=q->next;
}while(q!=head->next);
printf("\n\n\tThe polynomial is==>");
do
{
if(n-1)
{
printf("%dx^(%d) + ",p->coef,p->expo);
p=p->next;
}
else
{
printf(" %dx^(%d)",p->coef,p->expo);
p=p->next;
}
n--;
} while(p!=head->next);
}
void main()
{
int a,x,ch;
node *p1,*p2,*p3;
p1=p2=p3=NULL;
while(1)
{
printf("\n\t----------------<< MENU >>---------------");
printf("\n\tPolynomial Operations :");
printf(" 1.Add");
printf("\n\t\t\t\t2.Evaluate");
printf("\n\t\t\t\t3.Exit");
printf("\n\t------------------------------------------- ");
printf("\n\n\n\tEnter your choice==>");
scanf("%d",&ch);
switch(ch)
{
case 1 :
p1=create();
display(p1);
p2=create();
display(p2);
p3=add(p1,p2);
display(p3);
break; case
2 :
p1=create();
display(p1);
a=eval(p1);
printf("\n\nValue of polynomial=%d",a);
break;
case 3 :
exit(0);
break;
default :
printf("\n\n\tinvalid choice");
break;
}
}
}

P1

//array operations

#include<stdio.h>
#include<stdlib.h>
int a[20];
int n, val, i, pos;

/Function Prototype/
void create ();
void display ();
void insert ();
void delete ();

int main ()
{
int choice;
while (1)
{
printf ("\n\n--------MENU \n");
printf ("1.CREATE\n");
printf ("2.DISPLAY\n");
printf ("3.INSERT\n");
printf ("4.DELETE\n");
printf ("5.EXIT\n");
printf (" ");

  printf ("\nENTER YOUR CHOICE:\t");
  scanf ("%d", &choice);
  switch (choice)
{
case 1:create ();
  break;
case 2: display ();
  break;
case 3:insert ();
  break;
case 4:delete ();
  break;
case 5:exit (0);
  break;
default:printf ("\nInvalid choice:\n");
  break;
}
}

return 0;
}

//creating an array
void create ()
{
printf ("\nEnter the size of the array elements:\t");
scanf ("%d", &n);
printf ("\nEnter the elements for the array:\n");
for (i = 0; i < n; i++)
{
scanf ("%d", &a[i]);
}
}

//displaying an array elements
void display ()
{
printf ("\nThe array elements are:\n");
for (i = 0; i < n; i++)
{
printf ("%d\t", a[i]);
}
}

//inserting an element into an array

void insert ()
{
printf ("\nEnter the position for the new element:\t");
scanf ("%d", &pos);
printf ("\nEnter the element to be inserted :\t");
scanf ("%d", &val);
for (i = n - 1; i >= pos; i--)
{
a[i + 1] = a[i];
}
a[pos] = val;
n = n + 1;
}

//deleting an array element
void delete ()
{
printf ("\nEnter the position of the element to be deleted:\t");
scanf ("%d", &pos);
val = a[pos];
for (i = pos; i < n - 1; i++)
{

  a[i] = a[i + 1];
}

n = n - 1;
printf ("\nThe deleted element is =%d", val);
}

P12

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int create(int);
void linear_prob(int[], int, int);
void display (int[]);
void main()
{
int a[MAX],num,key,i;
int ans=1;
printf(" collision handling by linear probing : \n");
for (i=0;i<MAX;i++)
{
a[i] = -1;
}
do
{
printf("\n Enter the data");
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) ");
scanf("%d",&ans);
}
while(ans);
display(a);
}
int create(int num)
{
int key;
key=num%100;
return key;
}
void linear_prob(int a[MAX], int key, int num)
{
int flag, i,
count=0; flag=0;
if(a[key]== -1)
{
a[key] = num;
}
else
{
printf("\nCollision Detected...!!!\n");
i=0;
while(i<MAX)
{
if (a[i]!=-1)
count++;
i++;
}
printf("Collision avoided successfully using LINEAR PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
display(a);
exit(1);
}
for(i=key+1; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag =1;
break;
}
//for(i=0;i<key;i++)
i=0;
while((i<key) && (flag==0))
{
if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
}
i++;
}
}
}
void display(int a[MAX])
{
int i,choice;
printf("1.Display ALL\n 2.Filtered Display\n");
scanf("%d",&choice);
if(choice==1)
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
printf("\n %d %d ", i, a[i]);
}
else
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
if(a[i]!=-1)
{
printf("\n %d %d ", i, a[i]);
continue;
}
}
}

P7

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct stud
{
long long int ph;
int sem;
char name[15],usn[15],brnch[8];
struct stud next;
}
head=NULL,tail=NULL,temp=NULL,temp1;
void create(long long int n,int s,char na[20],char u[15],char b[5])
{
if(head==NULL)
{
head=(struct stud
)malloc(1
sizeof(struct stud));
head->ph=n;
head->sem=s;
strcpy(head->name,na);
strcpy(head->usn,u);
strcpy(head->brnch,b);
head->next=NULL;
tail=head;
count++;
}
else
{
temp=(struct stud
)malloc(1
sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=NULL;
tail->next=temp;
tail=temp;
count++;
}
}
void display()
{
temp1=head;
if(temp1==NULL)
{
printf("\nlist is empty\n");
}
else
{
printf("student details are as follows:\n");
while(temp1!=NULL)
{
printf(" \n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1->name,temp1->usn,temp1->brnch,temp1->sem,temp1->ph);
printf(" \n");
temp1=temp1->next;
}
printf("no. of nodes=%d\n",count);
}
}
void insert_head(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud
)malloc(1sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=head;
head=temp;
count++;
}
void insert_tail(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud
)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
tail->next=temp;
temp->next=NULL;
tail=temp;
count++;
}
void delete_head()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
else
{
head=head->next;
printf("deleted node is:\n");
printf(" \n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1->name,
temp1->usn,temp1->brnch,temp1->sem,temp1->ph);
printf(" \n");
free(temp1);
count--;
}
}
void delete_tail()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
while(temp1->next!=tail)
{
temp1=temp1->next;
}
printf("deleted node is:\n"); printf(" \n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",tail->name,tail->usn,tail->brnch,tail->sem,tail->ph); printf(" \n");
free(tail);
tail=temp1;
tail->next=NULL;
count--;
}
void main()
{
int choice;
long long int ph; int sem;
char name[20],usn[15],brnch[5]; printf("--------MENU \n");
printf("1.create\n2.Insert from head\n3.Insert from tail\n4.Delete from head\5.Delete fromtail\n6.display\n7.exit\n");
printf(" \n");
while(1)
{
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
create(ph,sem,name,usn,brnch);
break;
case 2:
printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_head(ph,sem,name,usn,brnch);
break;
case 3:
printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_tail(ph,sem,name,usn,brnch);
break;
case 4:
case 5:
delete_head();
break;
delete_tail();
break;
case 6:
display();
break;
case 7:exit(0);
default:printf("invalid option\n");
}
}
}

P2

#include <stdio.h>
void main ()
{
char STR[50], PAT[50], REP[50], ans[50];
int i, j, c, m, k, flag = 0;
printf ("\nEnter the MAIN string: \n");
gets(STR);
printf ("\nEnter a PATTERN string: \n");
gets(PAT);
printf ("\nEnter a REPLACE string: \n");
gets(REP);
i = m = c = j = 0;
while (STR[c] != '\0')
{
if (STR[m] == PAT[i])
{
i++;
m++;
flag = 1;
if (PAT[i] == '\0')
{
for (k = 0; REP[k] != '\0'; k++, j++)
ans[j] = REP[k];
i = 0;
c = m;
}
}

     else	

    {
        ans[j] = STR[c];
        j++;
        c++;
        m = c;
        i = 0;
    }
}
 if (flag == 0)

{
printf ("Pattern doesn't found!!!");
}
else
{
ans[j] = '\0';
printf ("\nThe RESULTANT string is:%s\n", ans);
}
}

P3

// .Design,DevelopandImplementamenudrivenprograminCforthefollowingoperationsonSTACKofintegers(Arrayimplementation ofstack with maximum sizeMAX)
// Pushan element on to stack
// Popanelementfromstack.
// Demonstratehow stack can beused to check palindrome.
// DemonstrateOverflowandUnderflowsituationsonstack.
// Displaythestatus ofstack.
// Exit.
// Supporttheprogram withappropriatefunctionsforeachoftheaboveoperations.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size], top = -1, flag = 1;
int i, temp, item, rev[max_size], num[max_size];
void push ();
void pop ();
void display ();
void pali ();
int main ()
{
int choice;
printf ("\n\n--------STACK OPERATIONS------\n\n ");
printf ("\n1.Push\n");
printf ("2.Pop\n");
printf ("3.Palindrome\n");
printf ("4.Display\n");
printf ("5.Exit\n");
printf (" ");
while (1)
{
printf ("\nEnter your choice:\t");
scanf ("%d", &choice);
switch (choice)
{
case 1:
push ();
break;
case 2:
pop ();
if (flag)
printf ("\nThe poped element: %d\t", item);
temp = top;
break;
case 3:
pali ();
top = temp;
break;
case 4:
display ();
break;
case 5:
exit (0);
break;
default:
printf ("\nInvalid choice:\n");
break;
}

}

//return 0;
}
void push () //Inserting element into the stack
{
if (top == (max_size - 1))
{
printf ("\nStack Overflow:");
}
else
{
printf ("Enter the element to be inserted:\t");
scanf ("%d", &item);
top = top + 1;
stack[top] = item;
}
temp = top;
}
void pop () //deleting an element from the stack
{
if (top == -1)
{
printf ("Stack Underflow:");
flag = 0;
}
else
{
item = stack[top];
top = top - 1;
}
}
void pali ()
{
i = 0;
if (top == -1)
{
printf ("Push some elements into the stack first\n");
}
else
{
while (top != -1)
{
rev[top] = stack[top];
pop ();
}
top = temp;
for (i = 0; i <= temp; i++)
{
if (stack[top--] == rev[i])
{
if (i == temp)

	        {
                printf ("Palindrome\n");
	            return;
            } 
        }	
    }
    printf ("Not Palindrome\n");

}

}
void display ()
{
int i;
top = temp;
if (top == -1)
{
printf ("\nStack is Empty:");
}
else
{
printf ("\nThe stack elements are:\n");
for (i = top; i >= 0; i--)
{
printf ("%d\n", stack[i]);
}
}
}

P5

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int i,top=-1;
int op1, op2, res,s[20];
char postfix[90],symb;
void push(int item)
{
top = top+1;
s[top]= item;
}
int pop()
{
int item;
item=s[top];
top = top-1;
return item;
}
void main()
{
printf("\nEnter a valid postfix expression:\n");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
symb= postfix[i];
if(isdigit(symb))
{
push(symb-'0');
}
else
{
op2 = pop();
op1 = pop();
switch(symb)
{
case'+':push(op1+op2);
break;
case'-':push(op1-op2);
break;
case'':push(op1op2);
break;
case'/':push(op1/op2);
break;
case'%':push(op1%op2);
break;
case'$':
case'^':push(pow(op1,op2));
break;
default:push(0);
break;
}
}
}
res= pop();
printf("\nResult= %d",res);
}

P4

// 4.Design, Develop andImplement a Program in C for converting an Infix Expression to PostfixExpression.
// Program should support for both parenthesized and free parenthesized expressions withtheoperators:+, -,*, /,%(Remainder),^(Power)and alphanumericoperands.

#define SIZE 50 /* Size of Stack /
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
char s[SIZE];
int top = -1; /
Global declarations /
push(char elem) /
Function for PUSH operation /
{
s[++top] = elem;
}
char pop() /
Function for POP operation /
{
return (s[top--]);
}
int pr(char elem) /
Function for precedence /
{
switch (elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '
':
case '/':
case '%': return 3;
case '^': return 4;
}
}
void main() /* Main Program /
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nRead the Infix Expression ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /
Remove ( /
}
else /
Operator /
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /
Pop from stack till empty /
pofx[k++] = pop();
pofx[k] = '\0'; /
Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s\n Postfix Expn: %s\n", infx, pofx);
}

P8

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Enode
{
char ssn[15];
char name[20];
char dept[5];
char designation[10];
int salary;
long long int phno;
struct Enode *left;
struct Enode *right;
}*head=NULL;
struct Enode *tail,*temp1,*temp2;
void create(char [],char [],char [],char [],int ,long long int);
void ins_beg(char [],char [],char [],char [],int ,long long int);
void ins_end(char [],char [],char [],char [],int ,long long int);
void del_beg();
void del_end();
void display();
int count=0;
void main()
{
int choice;
char s[15],n[20],dpt[5],des[10];
int sal;
long long int p;
printf("1.Create\n2.Display\n3.Insert at beginning\n4.Insert at End\n5.Delete at beginning\n6.Deleteat End\n7.Exit\n");
while(1)
{
printf("\nEnter your choice\n");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("Enter the required data(Emp no,Name,Dept,Desig,sal,phone\n");
scanf("%s%s%s%s%d%lld",s,n,dpt,des,&sal,&p);
create(s,n,dpt,des,sal,p);
break;
case 2:
display();
break;
case 3:
printf("Enter the required data (Emp no,Name,Dept,Desig,sal,phone\n");
scanf("%s%s%s%s%d%lld",s,n,dpt,des,&sal,&p);
ins_beg(s,n,dpt,des,sal,p);
break;
case 4:
printf("Enter the required data(Emp no,Name,Dept,Desig,sal,phone\n");
scanf("%s%s%s%s%d%lld",s,n,dpt,des,&sal,&p);
ins_end(s,n,dpt,des,sal,p);
break;
case 5:
del_beg();
break;
case 6:
del_end();
break;
case 7:
exit(0);
}
}
}
void create(char s[15],char n[20],char dpt[5],char des[10],int sal,long long int p)
{
if(head==NULL)
{
head=(struct Enode )malloc(1sizeof(struct Enode));
strcpy(head->ssn,s);
strcpy(head->name,n);
strcpy(head->dept,dpt);
strcpy(head->designation,des);
head->salary=sal;
head->phno=p;
head->left=NULL;
head->right=NULL;
tail=head;
}
else
{
temp1=(struct Enode )malloc(1sizeof(struct Enode));
strcpy(temp1->ssn,s);
strcpy(temp1->name,n);
strcpy(temp1->dept,dpt);
strcpy(temp1->designation,des);
temp1->salary=sal;
temp1->phno=p;
tail->right=temp1;
temp1->right=NULL;
temp1->left=tail;
tail=temp1;
}
}
void display()
{
temp1=head;
printf("Employee Details \n");
while(temp1!=NULL)
{
printf(" \n");
printf("%s\n%s\n%s\n%s\n%d\n%lld\n",temp1->ssn,temp1->name,temp1->dept,temp1->designation,temp1->salary,temp1->phno);
printf(" ");
temp1=temp1->right;
}
}
void ins_beg(char s[15],char n[20],char dpt[5],char des[10],int sal,long long int p)
{
temp1=(struct Enode )malloc(1sizeof(struct Enode));
strcpy(temp1->ssn,s);
strcpy(temp1->name,n);
strcpy(temp1->dept,dpt);
strcpy(temp1->designation,des);
temp1->salary=sal;
temp1->phno=p;
temp1->right=head;
head->left=temp1;
head=temp1;
temp1->left=NULL;
}
void ins_end(char s[15],char n[20],char dpt[5],char des[10],int sal,long long int p)
{
temp1=(struct Enode )malloc(1sizeof(struct Enode));
strcpy(temp1->ssn,s);
strcpy(temp1->name,n);
strcpy(temp1->dept,dpt);
strcpy(temp1->designation,des);
temp1->salary=sal;
temp1->phno=p;
tail->right=temp1;
temp1->left=tail;
temp1->right=NULL;
tail=temp1;
}
void del_beg()
{
temp1=head->right;
free(head);
head=temp1;
head->left=NULL;
}
void del_end()
{
temp1=tail->left;
free(tail);
tail=temp1;
tail->right=NULL;
}

P10

include <stdio.h>

include <stdlib.h>

int flag=0;
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
void main() {
int choice;
int ans =1;
int key;
node *new_node, *root, *tmp, *parent;
node get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.RecursiveTraversals");
printf("\n4.Exit");
printf("\n Enter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /
Tree is not Created /
root = new_node;
else
insert(root, new_node);
printf("\n Want To enter More Elements?(1/0)");
scanf("%d",&ans);
} while (ans);
break;
case 2:
printf("\n Enter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
if(flag==1)
{
printf("\nParent of node %d is %d", tmp->data, parent->data);
}
else
{
printf("\n The %d Element is not Present",key);
}
flag=0;
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
} while (choice!= 4);
}
/
Get new Node */
node *get_node()
{ node *temp;
temp = (node ) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/

This function is for creating a binary search
tree */
void insert(node *root, node new_node)
{
if (new_node->data < root->data)
{
if(root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data)
{
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/

This function is for searching the node from binary Search Tree */
node *search(node *root, int key, node **parent)
{
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf("\n The %d Element is Present", temp->data);
flag=1;
return temp;
}
parent = temp;
if(temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/

This function displays the tree in inorder fashion
*/
void inorder(node temp)
{
if (temp != NULL)
{
inorder(temp->lchild);
printf("%d\t", temp->data);
inorder(temp->rchild);
}
}
/

This function displays the tree in preorder
fashion */
void preorder(node *temp)
{
if (temp != NULL) {
printf("%d\t", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/This function displays the tree in postorder fashion/
void postorder(node *temp)
{
if (temp != NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d\t", temp->data);
}
}

P11

#include <stdio.h>
#include <stdlib.h>
int a[20][20],q[20],visited[20],reach[10],n,i,j,f=0,r=-1,count=0;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}}
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
{
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
count++;
dfs(i);
}
}
}
void main()
{
int v, choice;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
for(i=1;i<=n-1;i++)
reach[i]=0;
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n\n1.BFS\n\n 2.DFS\n\n 3.Exit\n\n Enter Your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the starting vertex:");
scanf("%d",&v);
bfs(v);
if((v<1)||(v>n))
{
printf("\nBfs is not possible");
}
else
{
printf("\nThe nodes which are reachable from %d:\n",v);
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
}
break;
case 2:dfs(1);
if(count==n-1)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
break;
case 3: exit(0);
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.