Data Structures in C Programs
Data Structures in C Programs
#include<stdio.h>
int insert(int ar[],int n, int num);
void display(int ar[],int n);
void sorting(int ar[],int n);
void max(int ar[],int n);
void min(int ar[],int n);
int uar[1000];
int f=0;
int n;
int main()
{ int u;
int ar[1000];
char ch='y';
printf("Pls enter the array size u want\n");
scanf("%d",&n);
while(ch=='y'){
printf("option one is compulsory\n");
printf("MENU\n1.ARRAY
CREATION\n2.INSERTION\n3.SORTING\n4.MAX\n5.MIN\n6.DISPLAY\n");
scanf("%d",&u);
switch (u) {
case 1:
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
}
break;
case 2:{int num;
printf("Enter the value to be inserted\n");
scanf("%d",&num);
n=insert(ar,n,num);
}break;
case 3:{
sorting(ar, n);
}break;
case 4:{max(ar, n);}break;
case 5:{min(ar,n);}break;
ar[position-1] = num;
n++;
printf("Resultant array is\n");
display(ar, n);}
break;
case 2:{
printf("Enter the location where you wish to insert an
element\n");
scanf("%d", &position);
uar[position-1] = num;
n++;
printf("Resultant array is\n");
display(uar, n);
}break;
default:{printf("pls choose from one or two\n");
}break;
}
}
else{
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
ar[position-1] = num;
n++;
printf("Resultant array is\n");
display(ar, n);
}
return n;
}
OUTPUT :
PROGRAM : 1(B)
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
OUTPUT:
PROGRAM : 1(C)
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
OUTPUT :
PROGRAM : 2
#include <stdio.h>
#define max 10
int stack[max],top=-1,item;
void push(void);
void pop(void);
void display(void);
int main(void)
{
int choice;
printf("menu");
printf("\n1-push");
printf("\n2-pop");
printf("\n3-display");
do{
printf("\nenter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("exit");
break;
}while(choice!=4);
return 0;
}
void push()
{
if(top>max)
{
printf("overflow");
}
else
{
printf("enter the no you want to be pushed to stack : ");
scanf("%d",&item);
top++;
stack[top]=item;
void pop()
{
if(top<=-1)
{
printf("\nstack underflow");
}
else
{
printf("\nthe popped elemnt is %d ",stack[top]);
top--;
void display()
{
for(int i=top;i>=0;i--)
printf("%d ",stack[i]);
}
OUTPUT :
PROGRAM : 3(A)
#include <stdio.h>
void toh(int ,char ,char ,char );
int main(void)
{
int n;
char a='A';
char b='B';
char c='C';
printf("Enter the number of hoops in one tower\n");
scanf("%d",&n);
toh(n,a,b,c);
return 0;
}
void toh(int n, char a, char b, char c)
{
if(n==1)
{
printf("%c->%c\n",a,c);
return;
}
toh(n-1,a,c,b);//transfer n-1 elements to last
toh(1,a,b,c);
toh(n-1,b,a,c);
}
OUTPUT :
PROGRAM : 3(B)
push(char elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}
char pop()
{ /* Function for POP operation */
return(s[top--]);
}
int 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 Postfix Expn: %s\n",infx,pofx);
return 0;
}
OUTPUT :
PROGRAM : 3(C)
AIM : Program to evaluate postfix expression.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define max 100
char ex[max];
int stk[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int top=-1;
int n;
void push(int);
int pop(void);
int main() {int i,num;
int result=0;
int a,b;
printf("Enter the expression length\n");
scanf("%d",&n);
printf("Enter the exp you want to convert\n");
for (i=0; i<n; i++) {
scanf("%c",&ex[i]);
}
strcat(ex, ")");
i=0;
while (ex[i]!=')') {
if (isdigit(ex[i])) {
num = ex[i] -'0';
push(num);
}
else if (ex[i]=='+'||ex[i]=='-
'||ex[i]=='/'||ex[i]=='*'||ex[i]=='^')
{
a=pop();
b=pop();
if (ex[i]=='+') {
result= b+a;
printf("result= %d\n",result); }
else if (ex[i]=='-') {
result=b-a;
printf("result= %d\n",result); }
else if (ex[i]=='/')
{
result =b/a;
printf("result= %d\n",result); }
else if (ex[i]=='*')
{
result= b*a;
printf("result= %d\n",result); }
if(ex[i]=='^')
{
result= pow(b,a);
printf("pow");}
push(result);
}
i+=1;
}
printf("result= %d\n",result);
return 0;
}
void push(int num)
{ if(n==max)
{printf("overflow\n");}
top+=1;
stk[top]=num;
printf("push ok\n");
}
int pop(void)
{
int p=0;
p=stk[top];
printf("pop%d\n",stk[top]);
top-=1;
return p;
}
OUTPUT :
PROGRAM : 4(A)
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
OUTPUT :
PROGRAM : 4(B)
int deque[MAX];
int left=-1, right=-1;
void insert_right(void);
void insert_left(void);
void delete_right(void);
void delete_left(void);
void display(void);
int main()
{
int choice;
do
{
printf("\n1.Insert at right ");
printf("\n2.Insert at left ");
printf("\n3.Delete from right ");
printf("\n4.Delete from left ");
printf("\n5.Display ");
printf("\n6.Exit");
printf("\n\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_right();
break;
case 4:
delete_left();
break;
case 5:
display();
break;
}
}while(choice!=6);
return 0;
}
//-------INSERT AT RIGHT-------
void insert_right()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (left==0 && right==MAX-1) || (left==right+1) )
{
printf("\nOVERFLOW");
}
if(left==-1) //if queue is initially empty
{
left=0;
right=0;
}
else
{
if(right==MAX-1)
right=0;
else
right=right+1;
}
deque[right]=val;
}
//-------INSERT AT LEFT-------
void insert_left()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (left==0 && right==MAX-1) || (left==right+1) )
{
printf("\nOVERFLOW");
}
if(left==-1) //if queue is initially empty
{
left=0;
right=0;
}
else
{
if(left==0)
left=MAX-1;
else
left=left-1;
}
deque[left]=val;
}
OUTPUT :
PROGRAM : 5
#include <stdio.h>
#include <stdlib.h>
void append();
int length();
void display();
void reverse();
void insbeg();
void deletebeg();
void deletepos();
void insertpos();
void deleteend();
struct node
{
int data;
struct node*link;
};
struct node*root=NULL;
int len;
int main()
{
int ch;char ans='y';
do
{
printf("\nmenu");
printf("\n1.append\n2.find length\n3.display\n4.reverse\n5.insert at
beg\n6.delete at beg\n7.delete from specified loc;\n8.insert at specified
position\n9.delete from end");
printf("\nenter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:append();
break;
case 2:len=length();
printf("the length of the linked list is = %d",len);
break;
case 3:display();
break;
case 4:reverse();
break;
case 5:insbeg();
break;
case 6:deletebeg();
break;
case 7:deletepos();
break;
case 8:insertpos();
break;
case 9:deleteend();
}
printf("\nwant to enter more? ");
scanf("%s",&ans);
}while(ans=='y');
return 0;
}
void append()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the data in the node ");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
struct node* p;
p=root;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}
int length()
{
int count=0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
temp=temp->link;
count++;
}
return count;
}
void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("list is empty");
}
else
{
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
printf("\n\n");
}
void reverse()
{
int i,j,temp;
len=length();
i=0;j=len-1;
struct node*p,*q;
p=q=root;
while(i<j)
{
int k=0;
while(k<j)
{
q=q->link;
k++;
}
temp=p->data;
p->data=q->data;
q->data=temp;
i++;
p=p->link;
j--;
q=root;
}
display();
void insbeg()
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("out of memory");
}
printf("enter the data in node ");
scanf("%d",&temp->data);
temp->link=root;
root=temp;
void deletebeg()
{
struct node* temp;
temp=root;
root=temp->link;
temp->link=NULL;
free(temp);
void deletepos()
{
int loc;
printf("enter the location you want to delete the node from");
scanf("%d",&loc);
struct node* p=root,*q;
int i=1;
while(i<loc-1)
{
p=p->link;
i++;
}
q=p->link;
p->link=q->link;
q->link=NULL;
free(q);
void insertpos()
{
int loc;
printf("enter the pos where you want to insert the node ");
scanf("%d",&loc);
struct node *temp,*p=root;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data in the node you want to insert ");
scanf("%d-->",&temp->data);
temp->link=NULL;
int i=1;
while(i<loc)
{
p=p->link;
i++;
}
temp->link=p->link;
p->link=temp;
void deleteend()
{
struct node* temp;struct node * secLast;
temp=root;
while(temp->link!=NULL)
{
secLast = temp;
temp=temp->link;
}
secLast->link=NULL;
free(temp);
}
OUTPUT
PROGRAM : 6
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
int e;
Position previous;
Position next;
};
int isLast(Position p)
{
return (p->next == NULL);
}
void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf("%d -> ", p->e);
p = p->next;
}
}
void main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->previous = NULL;
l->next = NULL;
List p = l;
printf("DOUBLY LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5.
QUIT\n\nEnter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;
case 2:
p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;
case 3:
p = l;
printf("Enter the element to be searched :: ");
scanf("%d",&x);
p = Find(x,p);
if(p == NULL)
printf("Element does not exist!!!\n");
else
printf("Element exist!!!\n");
break;
case 4:
Display(l);
break;
}
}
while(ch<5);
}
OUTPUT :
PROGRAM : 7(A)
#include<stdio.h>
void push();
void pop();
void display();
struct node
{
int data;
struct node *link;
};
struct node*top=NULL;
int main()
{
int choice;char ans='y';
do
{
printf("menu\n1.push\n2.pop\n3.display");
printf("\nenter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}
printf("\ndo you want to enter more ");
scanf("%s",&ans);
}
while(ans=='y');
}
void push()
{
struct node * temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the data in the node ");
scanf("%d",&temp->data);
temp->link=top;
top=temp;
}
void pop()
{
struct node* temp;
if(top==NULL)
{
printf("the list is empty");
}
else
{
temp=top;
printf("the node deleted is %d ",temp->data);
top=temp->link;
temp->link=NULL;
free(temp);
}
void display()
{
struct node *temp;
temp=top;
if(temp==NULL)
{
printf("\nempty");
}
while(temp!=NULL)
{
printf("\n%d<--",temp->data);
temp=temp->link;
}
OUTPUT
PROGRAM : 7(B)
AIM : Program to implement queues using linked list.
#include <stdio.h>
#include<stdlib.h>
void insert(void);
void delete(void);
void disp(void);
struct que {
int info;
struct que *new;
};
struct que * front=NULL;
struct que * temp=NULL;
struct que * rear=NULL;
//struct que * news;
int d;
int main() {
int c=0;
char ans='y';
while(ans=='y'){
printf(" 1.insert\n 2.Delete\n 3.Display\n 4.Exit\n");
scanf("%d",&c);
switch (c) {
case 1:
{insert();}
break;
case 2:{delete();}break;
case 3:{disp();}break;
default:
break;
}
printf("Do you want to continue\n");
scanf("%s", &ans);
}
return 0;
}
void insert(void)
{
struct que* news=NULL;
news = (struct que*) malloc(sizeof(struct que));
printf("Enter the info u want to add\n");
scanf("%d",&d);
news->info=d;
news->new=NULL;
if(rear==NULL&&front==NULL)
{ front=rear=news;
}
else
{ rear->new=news;
rear=news;
}
}
void delete()
{
if(front==NULL)
{printf("The que is empty\n");}
if(front!=NULL)
{
struct que *temp=front;
free(temp);
front=front->new;
printf("The first element has been deleted\n");
}
}
void disp()
{
if(front==NULL)
{printf("EMPTY");}
else
{ temp=front;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp= temp->new;
}
}
}
OUTPUT
#include<stdio.h>
void pop();
void push();
void display();
struct node
{
int data;
struct node *link;
};
struct node*front=NULL;
struct node*rear=NULL;
int main()
{
int choice;char ans='y';
do
{
printf("menu\n1.push\n2.pop\n3.display");
printf("\nenter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}
printf("\ndo you want to enter more ");
scanf("%s",&ans);
}
while(ans=='y');
void push()
{
void pop()
{
if(front==NULL)
{
printf("the queue is empty");
}
else
{
printf("the element popped is %d ",front->data);
front=front->link;
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct node* temp;
temp=front;
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
PROGRAM : 8
AIM : Program to demonstrate insert and display operations in binary search tree.
#include <stdio.h>
#include<stdlib.h>
void insert();
struct node
{
int data;
struct node * left;
struct node * right;
};
struct node * root=NULL;
int main()
{
int choice;char ans='y';
do
{
}
printf("\ndo you want to enter more ");
scanf("%s",&ans);
}
while(ans=='y');
void insert()
{
struct node * temp;struct node *p;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the value ");
scanf("%d",&temp->data);
temp->right=NULL;
temp->left=NULL;
p=root;
if(root==NULL)
{
root=temp;
}
else
{
struct node * curr;
curr=root;
while(curr)
{
p=curr;
if(temp->data > curr->data)
{
curr=curr->right;
}
else
{
curr=curr->left;
}
}
if(temp->data>p->data)
{
p->right=temp;
}
else
{
p->left=temp;
}
OUTPUT :
PROGRAM : 9(A)
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
return 0;
}
OUTPUT:
PROGRAM : 9(B)
int main()
{
int a[100], n, min,temp;
printf("Enter %d integers\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n-1;i++)
{
min=i;
for(int j=i+1 ;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
return 0;
}
OUTPUT :
PROGRAM : 9(C)
#include<stdio.h>
int main()
{
int a[5];
int key;
int j;
for(int i=0;i<5;i++)
{scanf("%d ",&a[i]);}
for(int i=1;i<5;i++)
{
key=a[i];
j=i-1;
PROGRAM : 9(D)
#include <stdio.h>
int main() {
int a[100], b[100], m, n, c, sorted[200];
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
return 0;
}
j = k = 0;
OUTPUT :
#include<stdio.h>
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
PROGRAM : 9(E)
AIM : Program to implement quick sort.
#include<stdio.h>
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
quicksort(number,0,count-1);
return 0;
}
OUTPUT :
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
while(i<j)
{
do
{ i++;
}
while(a[i]<v);
do
{ j--;
}
while(a[j]>v);
if(i<j)
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
a[l]=a[j];
a[j]=v;
return(j);
}
PROGRAM : 10
#include<stdio.h>
#include<string.h>
}
int main()
{
char a[25];
char b[25];
int i=0;
printf("Enter String A\n");
gets(a);
printf("Enter String B\n");
gets(b);
char ans='y';
while(ans=='y'||ans=='Y')
{
OUTPUT
Binary tree menuuu
#include<stdio.h>
#include<stdlib.h>
struct tree {
int info;
struct tree *left;
struct tree *right;
};
struct tree *insert(struct tree *,int);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);
struct tree *delet(struct tree *,int);
struct tree *search(struct tree *);
int main(void) {
struct tree *root;
int choice, item,item_no;
root = NULL;
/* rear = NULL;*/
do {
do {
printf("\n \t 1. Insert in Binary Tree ");
printf("\n\t 2. Delete from Binary Tree ");
printf("\n\t 3. Inorder traversal of Binary tree");
printf("\n\t 4. Postorder traversal of Binary tree");
printf("\n\t 5. Preorder traversal of Binary tree");
printf("\n\t 6. Search and replace ");
printf("\n\t 7. Exit ");
printf("\n\t Enter choice : ");
scanf(" %d",&choice);
if(choice<1 || choice>7)
printf("\n Invalid choice - try again");
}
while (choice<1 || choice>7);
switch(choice) {
case 1:
printf("\n Enter new element: ");
scanf("%d", &item);
root= insert(root,item);
printf("\n root is %d",root->info);
printf("\n Inorder traversal of binary tree is : ");
inorder(root);
break;
case 2:
printf("\n Enter the element to be deleted : ");
scanf(" %d",&item_no);
root=delet(root,item_no);
inorder(root);
break;
case 3:
printf("\n Inorder traversal of binary tree is : ");
inorder(root);
break;
case 4:
printf("\n Postorder traversal of binary tree is : ");
postorder(root);
break;
case 5:
printf("\n Preorder traversal of binary tree is : ");
preorder(root);
break;
case 6:
printf("\n Search and replace operation in binary tree ");
root=search(root);
break;
default:
printf("\n End of program ");
}
/* end of switch */
}
while(choice !=7);
return(0);
}
struct tree *insert(struct tree *root, int x) {
if(!root) {
root=(struct tree*)malloc(sizeof(struct tree));
root->info = x;
root->left = NULL;
root->right = NULL;
return(root);
}
if(root->info > x)
root->left = insert(root->left,x); else {
if(root->info < x)
root->right = insert(root->right,x);
}
return(root);
}
void inorder(struct tree *root) {
if(root != NULL) {
inorder(root->left);
printf(" %d",root->info);
inorder(root->right);
}
return;
}
void postorder(struct tree *root) {
if(root != NULL) {
postorder(root->left);
postorder(root->right);
printf(" %d",root->info);
}
return;
}
void preorder(struct tree *root) {
if(root != NULL) {
printf(" %d",root->info);
preorder(root->left);
preorder(root->right);
}
return;
}
/* FUNCTION TO DELETE A NODE FROM A BINARY TREE */
struct tree *delet(struct tree *ptr,int x) {
struct tree *p1,*p2;
if(!ptr) {
printf("\n Node not found ");
return(ptr);
} else {
if(ptr->info < x) {
ptr->right = delet(ptr->right,x);
/*return(ptr);*/
} else if (ptr->info >x) {
ptr->left=delet(ptr->left,x);
return ptr;
} else
/* no. 2 else */ {
if(ptr->info == x)
/* no. 2 if */ {
if(ptr->left == ptr->right)
/*i.e., a leaf node*/ {
free(ptr);
return(NULL);
} else if(ptr->left==NULL)
/* a right subtree */ {
p1=ptr->right;
free(ptr);
return p1;
} else if(ptr->right==NULL)
/* a left subtree */ {
p1=ptr->left;
free(ptr);
return p1;
} else {
p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
return p2;
}
}
/*end of no. 2 if */
}
/* end of no. 2 else */
/* check which path to search for a given no. */
}
return(ptr);
}
/* function to search and replace an element in the binary tree */
struct tree *search(struct tree *root) {
int no,i,ino;
struct tree *ptr;
ptr=root;
printf("\n Enter the element to be searched :");
scanf(" %d",&no);
fflush(stdin);
while(ptr) {
if(no>ptr->info)
ptr=ptr->right; else if(no<ptr->info)
ptr=ptr->left; else
break;
}
if(ptr) {
printf("\n Element %d which was searched is found and is = %d",no,ptr-
>info);
printf("\n Do you want replace it, press 1 for yes : ");
scanf(" %d",&i);
if(i==1) {
printf("\n Enter new element :");
scanf(" %d",&ino);
ptr->info=ino;
} else
printf("\n\t It's okay");
} else
printf("\n Element %d does not exist in the binary tree",no);
return(root);
}