Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 42

Implementation of Queue using Array

Program:

#include<stdio.h>
#include<conio.h>
#define null 0
void main()
{
int a;
void insert();
void deletion();
void disp();
clrscr();
printf("\n\t Implementation of Queue using Array\n");
do
{
printf("\n 1.Insertion");
printf("\n 2.Deletion");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&a);
switch(a)
{
case 1:
insert();
break;

case 2:
deletion();
break;

case 3:
disp();
break;

case 4:
exit(0);
break;

default:
printf("\n Invalid choice........Try again");
break;
}
}
while(a<4);
getch();
}

int q[10],n=5,f=0,r=0;
void insert()
{
int b;
if(r>=n)
{
printf("\n Queue Overflow.........");
return;
}
else
{
printf("\n Enter the element: ");
scanf("%d",&b);
q[r]=b;
r++;
printf("\n The inserted element is %d",b);
f=1;
return;
}
}

void deletion()
{
int b;
if(f==0)
{
printf("\n Queue Underflow....");
return;
}
else
{
b=q[f];
printf("\n The deleted element is %d",b);
f++;
r--;
return;
}
}
void disp()
{
int i;
if(r==0)
{
printf("\n Queue is empty.....");
return;
}
else
{
for(i=0;i<r;i++)
printf("\n The elements present in the queue are........%d",q[i]);}}
Output:

Implementation of Queue using Array

1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 1

Enter the element: 55

The inserted element is 55


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 1

Enter the element: 66

The inserted element is 66


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 3

The elements present in the queue are........55


The elements present in the queue are........66
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 2

The deleted element is 66


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 3

The elements present in the queue are........55

1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 4
Implementation of Queue using Linkedlist

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
void main()
{
int ch;
void insertion();
void deletion();
void display();
clrscr();
printf("\n\t Implementation of Queue using Linkedlist");
do
{
printf("\n1.Insertion\n2.Deletion\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion();
break;

case 2:
deletion();
break;

case 3:
display();
break;
case 4:
exit(0);
break;

default:
printf("\n Invalid choice........Try again");
break;
}
}
while(ch<4);
getch();
}
struct node
{
int elt;
struct node*next;
}*front=null,*rear=null;
void insertion()
{
int x;
struct node*newnode;
newnode=malloc(sizeof(struct node));
printf("\n Enter the element to insert: ");
scanf("%d",&x);
newnode->elt=x;
newnode->next=null;
if(rear==null)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
printf("\n The inserted element is %d",newnode->elt);
}
void deletion()
{
struct node*temp;
if(front==null)
{
printf("\n Queue is empty");
return;
}
temp=front;
if(front==rear)
front=rear=null;
else
front=front->next;
printf("\n Deleted element is %d",temp->elt);
free(temp);
}
void display()
{
struct node*temp;
if(front==null)
{
printf("\n Queue is empty");
return;
}
temp=front;
printf("\n The elements present in the queue are........");
while(temp!=null)
{
printf("%d\t",temp->elt);
temp=temp->next;
}}

Output :

Implementation of Queue using Linkedlist


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 1

Enter the element to insert: 55

The inserted element is 55


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 1

Enter the element to insert: 66

The inserted element is 66


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 2

Deleted element is 55
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 3

The elements present in the queue are........66


1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 4
Balancing Paranthesis using Array implementation

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1;
char stack[20];
void main()
{
char expr[20],ch;
int len,i;
void push(char);
void pop();
clrscr();
printf("\n\t Balancing Paranthesis using Array implementation");
printf("\nEnter the Expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='(')
push(expr[i]);
if(expr[i]==')')
pop();
}
if(top==-1)
printf("\n Balanced Expression");
else
printf("\nImbalanced right paranthesis");
getch();
}
void push(char item)
{
stack[top++]=item;
}
void pop()
{
if(top==-1)
{
printf("\nImbalanced left paranthesis");
getch();
}
else
top--;
}

Output:

Balancing Paranthesis using Array implementation

Enter the Expression: (a+b)

Balanced Expression

Enter the Expression: ((a+b)

Imbalanced left paranthesis

Enter the Expression: (a+b))

Imbalanced right paranthesis


Balancing Paranthesis using Linkedlist implementation

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define null 0
struct node
{
int data,top;
struct node*next;
}
*top=null;
void main()
{
char expr[20],ch;
int len,i;
void push(char);
void pop();
clrscr();
printf("\n\t Balancing Paranthesis using Linkedlist implementation");
printf("\nEnter the Expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='(')
push(expr[i]);
if(expr[i]==')')
pop();
}
if(top==null)
printf("\n Balanced Expression");
else
printf("\nImbalanced right paranthesis");
getch();
}

void push(char item)


{
struct node*newnode;
newnode=malloc(sizeof(struct node));
newnode->data=item;
if(top==null)
{
newnode->next=null;
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}
}
void pop()
{
if(top==null)
{
printf("\nImbalanced left paranthesis");
getch();
}
else
top=top->next;
}

Output:

Balancing Paranthesis using Array implementation

Enter the Expression: (a+b)

Balanced Expression

Enter the Expression: ((a+b)

Imbalanced left paranthesis

Enter the Expression: (a+b))

Imbalanced right paranthesis


Postfix Evaluation Using Array Implementation

Program:

#include<stdio.h>
#include<conio.h>
int top=0,stack[20];
void main()
{
char expr[20];
int x,len,a,b,c,i;
void push(int);
int pop();
clrscr();
printf(“\n Postfix Evaluation Using Array Implementation”);
printf("\n Enter the expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':
c=a+b;
push(c);
break;

case '-':
c=a-b;
push(c);
break;

case '*':
c=a*b;
push(c);
break;

case '/':
c=a/b;
break;
}
}
else
{
printf("\n Enter the value of %c ",expr[i]);
scanf("%d",&x);
push(x);
}
}
printf("\n The Result is: %d",stack[top]);
getch();
}

void push(int item)


{
stack[top++]=item;
}
int pop()
{
int stackval;
top--;
stackval=stack[top];
return stackval;
}

Output:

Postfix Evaluation Using Array Implementation

Enter the expression: a+b*c

Enter the value of a: 1

Enter the value of b: 2

Enter the value of c: 3

The Result is: 7


Postfix Evaluation Using Linkedlist Implementation

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define null 0
void main()
{
char expr[20];
int x,top,len,a,b,c,i;
void push(int);
int pop();
clrscr();
printf(“\n Postfix Evaluation Using Linkedlist Implementation”);
printf("\n Enter the expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':
c=a+b;
push(c);
break;

case '-':
c=a-b;
push(c);
break;

case '*':
c=a*b;
push(c);
break;

case '/':
c=a/b;
break;
}
}
else
{
printf("\n Enter the value of %c ",expr[i]);
scanf("%d",&x);
push(x);
}}
printf("the result is: ");
printf("%d",top->data);
}
struct node
{
int data;
struct node*next;
}*top=null;
void push(int item)
{
struct node*newnode;
newnode=malloc(sizeof(struct node));
newnode->data=item;
if(top==null)
{
newnode->next=null;
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}}
int pop()
{
int staval;
struct node*temp;
temp=top;
staval=temp->data;
top=top->next;
free(temp);
return staval;
}

Output:

Postfix Evaluation Using Linkedlist Implementation


Enter the expression: a*b

Enter the value of a: 10

Enter the value of b: 20

The Result is: 200


QUICK SORT

Program:

#include<stdio.h>
#include<conio.h>
int a[20],n;
void main()
{
int i;
void qsort(int,int);
clrscr();
printf("\n QUICK SORT");
printf("\n Enter the array size: ");
scanf("%d",&n);
printf("\n Enter the elements to be sorted: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(0,n-1);
printf("\n The sorted elements are: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void qsort(int left,int right)
{
int i,j,pivot,temp,k;
if(left<right)
{
i=left+1;
j=right;
pivot=left;
while(i<j)
{
while(a[pivot]>=a[i])
i=i+1;
while(a[pivot]<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
qsort(left,j-1);
qsort(j+1,right);}}
Output:

QUICK SORT

Enter the array size: 3

Enter the elements to be sorted: 2 3 1

The sorted elements are……...: 1 2 3

QUICK SORT

Enter the array size: 5

Enter the elements to be sorted: 2 3 1 5 4

The sorted elements are……..: 1 2 3 4 5


HEAP SORT

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void heapsort(int a[],int n);
void delmax(int a[],int,int);
int a[20];
void main()
{
int n,i;
clrscr();
printf("\n HEAP SORT\n");
printf("\n Enter the array size: ");
scanf("%d",&n);
printf("\n Enter the elements of array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n The sorted elements are.........");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
void heapsort(int a[],int n)
{
int i,temp;
for(i=(n/2)-1;i>=0;i--)
delmax(a,i,n);
for(i=n-1;i>=1;i--)
{
temp=a[0];
a[0]=a[i];
a[i]=temp;
delmax(a,0,i-1);
}
}
void delmax(int a[],int root,int bottom)
{
int maxch,temp;
if(root*2<=bottom)
{
if(root*2==bottom)
maxch=root*2;
else if(a[root*2]>a[root*2+1])
maxch=root*2;
else
maxch=(root*2)+1;

if(a[root]<a[maxch])
{
temp=a[root];
a[root]=a[maxch];
a[maxch]=temp;
root=maxch;
}
}
}

Output:

HEAP SORT

Enter the array size: 5

Enter the elements of array: 6 1 7 2 5

The sorted elements are.........1 2 5 6 7

HEAP SORT

Enter the array size: 3

Enter the elements of array: 3 5 1

The sorted elements are.........1 3 5


Implementation of Stack using Array

Program:

#include<stdio.h>
#include<conio.h>
#define size 5
int stack[size],top=-1;

void main()
{
int choice;
void push():
void pop();
void disp();
int isempty();
int length();
clrscr();

do
{
printf(“\n1.Push\n2. Pop\n3. Display\n4. Length\n5. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
printf(“\n No of elements in stack is %d”,length(1));
break;
case 5:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice!=5);
getch();
}

void push()
{
int no;
if(top==size-1)
{
printf(“\nStack if full”);
}
else
{
printf(“\n Enter the number: ”);
scanf(“%d”,&no);
top++;
stack[top]=no;
}
}

void pop()
{
int no;
if(isempty())
{
printf(“\nStack is empty”);
top=-1;
}
else
{
no=stack[top];
printf(“\n %d is popped from the stack”,no);
--top;
}
}

void disp()
{
int I,temp=top;
if(isempty())
{
printf(“\nStack is empty”);
return;
}
printf(“\nElements in the stack…”);
for(i=temp;i>=0;i--)
{
printf(“%d”,stack[i]);
}}

int isempty()
{
return(top==-1);
}

int length()
{
return(top+1);
}

Output:

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 1


Enter the number: 10

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 1


Enter the number: 20

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 1


Enter the number: 30
1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 1


Enter the number: 40

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 1


Enter the number: 50

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 1


Stack is full

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 3


Elements in the stack 50 40 30 20 10

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 4


No of elements in the stack 5
1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 2


50 is popped from the stack

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 2


40 is popped from the stack

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 2


30 is popped from the stack

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 2


20 is popped from the stack

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 2


10 is popped from the stack
1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 2


Stack is empty

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 3


Stack is empty

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 4


No of elements in the stack 0

1. Push
2. Pop
3. Display
4. Length
5. Exit

Enter your choice: 5


Implementation of Stack using Linked List

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct stack
{
int no;
struct stack *next;
};
struct stack *p,*q,*temp,*head=NULL;
void main()
{
void push();
void pop();
void disp();
int choice;
clrscr();

do
{
printf(“\n1.Push\n2. Pop\n3. Display\n4. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice!=4);
getch();
}

void push()
{
temp=(struct stack*)malloc(sizeof(struct stack));
printf(“\nEnter the no:”);
scanf(“%d”,&temp->no);

if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
(
temp->next=head;
head=temp;
]
printf(“\nElement %d is pushed into the stack”,temp->no);
}

void pop()
{
p=head;
if(head==NULL)
{
printf(“Stack is empty”);
}
else
{
p=head;
head=head->next;
printf(“\n Element %d is popped from the stack”,p->no);
free(p);
}
}
void disp()
{
if(head==NULL)
printf(“Stack is empty”);
else
{
p=head;
printf(“\nElement is present in the stack”);
while(p!=NULL)
{
printf(“%d”,p->no);
}
}
}

Output:

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

Enter your choice: 1


Enter the number: 10
Element 10 is pushed in to stack

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

Enter your choice: 1


Enter the number: 20
Element 20 is pushed in to stack

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

Enter your choice: 1


Enter the number: 30
Element 30 is pushed in to stack
1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 3


Element present in the stack 30 20 10

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

Enter your choice: 2


Element 30 is popped from the stack

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

Enter your choice: 2


Element 20 is popped from the stack

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

Enter your choice: 3


Elements present in the stack 10

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

Enter your choice: 4


Implementation of List using Array

Program:

#include<stdio.h>
#include<conio.h>
struct emp
{
char name[20];
int empno;
int salary;
};
struct emp empinfo[10];
void main()
{
int choice;
void getdetails();
void search();
void disp();
clrscr();
do
{
printf(“\n1.Getdetails\n2. Search\n3. Display\n4. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
getdetails();
break;
case 2:
search();
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice!=4);
getch();}
void getdetails()
{
int i;
printf(“\n Enter the details:”);
for(i=0;i<3;i++)
{
printf(“\n Enter the Employee number:”);
scanf(“%d”,&empinfo[i].empno);
printf(“\n Enter the Employee name:”);
scanf(“%d”,&empinfo[i].name);
printf(“\n Enter the Employee salary:”);
scanf(“%d”,&empinfo[i].salary);
}
}

void disp()
{
int i;
printf(“\n Employee details……”);
for(i=0;i<3;i++)
{
printf(“\nEmployee Name: %s”,empinfo[i].name);
printf(“\nEmployee Number:%d”,empinfo[i].empno);
printf(“\nEmployee Salary: %d”,empinfo[i].salary);
}
}

void search()
{
int i,id,position;
printf(“\n Enter the employee no Who’s detail was to be found?....”);
scanf(“%d”,&id);
for(i=0;i<3;i++)
{
if(id==empinfo[i].empno)
{
printf(“\nEmployee Name: %s”,empinfo[i].name);
printf(“\nEmployee Number:%d”,empinfo[i].empno);
printf(“\nEmployee Salary: %d”,empinfo[i].salary);
}
}
}
Output:

1. Getdetails
2. Search
3. Display
4. Exit

Enter the choice: 1


Enter the details……

Enter the Employee number: 101


Enter the Employee name: AAAA
Enter the Employee salary: 10000

Enter the Employee number: 102


Enter the Employee name: BBBB
Enter the Employee salary: 20000

Enter the Employee number: 103


Enter the Employee name: CCCC
Enter the Employee salary: 30000

1. Getdetails
2. Search
3. Display
4. Exit

Enter the choice: 2


Enter the employee no Who’s detail was to be found?....101

Employee name: AAAA


Employee number: 101
Employee salary: 10000
1. Getdetails
2. Search
3. Display
4. Exit

Enter the choice: 3

Enter the Employee number: 101


Enter the Employee name: AAAA
Enter the Employee salary: 10000

Enter the Employee number: 102


Enter the Employee name: BBBB
Enter the Employee salary: 20000

Enter the Employee number: 103


Enter the Employee name: CCCC
Enter the Employee salary: 30000

1. Getdetails
2. Search
3. Display
4. Exit

Enter the choice: 4


Implementation of List Using Linked List

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
void main()
{
int data,ch;
void insert(int x);
void deletion(int x);
void disp();
clrscr();

do
{
printf(“\n1.Insertion\n2. Deletion\n3. Display\n4. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
printf(“\n Enter the element to insert: ”);
scanf(“%d”,&data);
insert(data);
break;

case 2:
printf(“\n Enter the element to delete: ”);
scanf(“%d”,&data);
deletion(data);
break;

case 3:
disp();
break;

case 4:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice<4);
getch();
}

struct node *find(int);


struct node *findprevious(int);

struct node()
{
int ele;
struct node *next;
}
*list=NULL,*p;

void insert(int x)
{
struct node *newnode;
int pos;
newnode=malloc(sizeof(struct node));
newnode->ele=x;

if(list->next==NULL)
{
list->next=newnode;
newnode->next=NULL;
}
else
{
printf(“\n Enter the value after which the element to be inserted:”);
scanf(“%d”,&pos);
p=find(pos);
newnode->next=p->next;
p->next-newnode;
}
}

struct node *find(int s)


{
p=list->next;
while(p!=NULL&&p->ele!=s)
p=p->next;
return p; }
void deletion(int x)
{
struct node *temp;
temp=malloc(sizeof(struct node));
p=findprevious(x);

if(p->next!=NULL)
{
temp=p->next;
p->next=temp->next;
printf(“\n The deleted element is %d”,temp->ele);
free(temp);
}
else
{
pritnf(“\n Element is not found in the list….”);
}
}

struct node *findprevious(int s)


{
p=list;
while(p->next=NULL&&p->next->ele!=s)
p=p->next;
return p; }

void disp()
{
if(list->next==NULL)
printf(“\n List is empty”);
else
{
p=list->next;
printf(“\n The contents of the list are….”);
while(p!=NULL)
{
printf(“%d->”,p->ele);
p=p->next;
}
}
}
Output:

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 1


Enter the element to insert 10

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 1


Enter the element to insert 20
Enter the value after which the element to be inserted:10

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 3


The contents of the list are….
10->20->

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 1


Enter the element to insert 30
Enter the value after which the element to be inserted:10

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 3


The contents of the list are….
10->30->20->
1. Insertion
2. Deletion
3. Display
4. Exit

Enter your choice: 2


Enter the element to delete 20
The Deleted element is 20

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 3


10->30->

1. Insertion
2. Deletion
3. Display
4. Exit

Enter your choice: 2


Enter the element to delete 10
The deleted element is 10

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 3


30->

1. Insertion
2. Deletion
3. Display
4. Exit

Enter the choice: 4


Binary Search Tree

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int flag=0,a[20],n,pos,i,item,low,mid,high;
void sort(int[],int);
clrscr();

printf(“\n Enter the size of tree:”);


scanf(“%d”,&n);
printf(“\n Enter the elements:”);
for(i=0;i<n;i++)
scanf(“d},&a[i]);
sort(a.n);

printf(“\n Enter the data to be searched:”);


scanf(“%d”,&item);
low=1;
high=n;

while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
flag=1;
break;
}
else
if(item>a[mid])
low=mid+1;
else
high=mid-1;
}
if(flag==0)
printf(“\n The element %d is not present in the tree:”,item);
else
printf(“\n The element %d is present in the tree”,item);
getch();
}
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

Output:

Enter the size of the Tree……..5

Enter the elements: 2 3 5 7 1

Enter the element to be search: 7

The element 7 is present in the tree..

Enter the size of the Tree……..5

Enter the elements: 2 3 5 7 1

Enter the element to be search: 8

The element 8 is present in the tree..

You might also like