0% found this document useful (0 votes)
7 views37 pages

DS Lab 2022

Uploaded by

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

DS Lab 2022

Uploaded by

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

Sindhi College

Department of Computer Science


C Programming Lab Manual – I Semester BCA & Bsc
Subject CA-C5P: DATA STRUCTURES LAB

1. Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear and Binary search and also display its
first occurrence.

2. Given {5,3,1,6,0,2,4} order the numbers in ascending order using Bubble Sort Algorithm

3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display the output in
descending order.

4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete 8,61,27 from
the list. Display your list after each insertion and deletion.

5. Write a program to insert the elements {61,16,8,27} into linear queue and delete three elements
from the list. Display your list after each insertion and deletion.

6. Write a program to insert the elements {61,16,8,27} into circular queue and delete 4 elements from
the list. Display your list after each insertion and deletion.

7. Write a program to insert the elements {61,16,8,27} into ordered singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion.

8. Write a program to add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.

9. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also display the
popped numbers.

10. Write a recursive program to find GCD of 4,6,8.

11. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete 6,9&5 from
it(using linked list implementation)..

12. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression

13. Write a program to evaluate a postfix expression 5 3+8 2 - *.

14. Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} after creation
insert 45 and 19 into tree and delete 15,17 and 41 from tree. Display the tree on each insertion and
deletion operation

15. Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform
inorder, preorder and post order traversal.

16. Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}

17. Given S1={“Flowers”} ; S2={“are beautiful”} I. Find the length of S1 II. Concatenate S1 and S2
III. Extract the substring “low” from S1 IV. Find “are” in S2 and replace it with “is”

1
1. Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear and Binary search and also display its
first occurrence.
#include<stdio.h>
#include<conio.h>
int LINEAR_SEARCH(int a[10], int n, int key)
{
int i;
for(i=0;i<n;i++)
{
if(key==a[i])
{
return i+1;
}
}
return -1;
}
int BINARY_SEARCH(int a[10], int n,int key)
{
int first,last,mid;
first =0;
last =n-1;
while(first <=last)
{
mid=(first+last)/2;
if(key == a[mid])
return mid+1;
else if(key < a[mid])
last = mid-1;
else if(key > a[mid])
first = mid+1;
}
return -1;
}
void DISPLAY_ARRAY(int a[10],int n)
{
int i;
printf("\n Given array : ");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
}
void main()
{
int a[8] = {4,7,3,2,1,7,9,0};
int sa[8] = {0,1,2,3,4,7,7,9};
int n=8;
int key=7;
int loc;
clrscr();
DISPLAY_ARRAY(a,n);
loc = LINEAR_SEARCH(a,n,key);
if(loc==-1)
printf("\nItem not found");
else
printf("\nItem found at location %d",loc);
2
DISPLAY_ARRAY(sa,n);
loc = BINARY_SEARCH(sa,n,key);
if(loc==-1)
printf("\nItem not found");
else
printf("\nItem found at location %d",loc);

getch();
}
Output:

2. Given {5,3,1,6,0,2,4} order the numbers in ascending order using Bubble Sort Algorithm
#include<stdio.h>
#include<conio.h>

void bubble_sort(int a[],int n)


{
int pass,temp,j;
for(pass =1;pass<n;pass++)
{
for(j=0;j<=n-pass-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1] = temp;
}
}
}
}
void main()
{
int i,j,a[20],n,temp;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
printf("\nThe sorted elements are: \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

3
Output:
Enter the number of elements: 7
Enter the array elements: 5 3 1 6 0 2 4

The sorted elements are


0123456

4
3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display the output in
descending order.

#include<stdio.h>
#include<conio.h>
//to find the location of max element
int sa[8] = {75,8,1,16,48,3,7,0};
int ia[8] = {75,8,1,16,48,3,7,0};
int n=8;
int max(int a[],int k,int n)
{
int loc,j,max;
max=a[k];
loc=k;
for(j=k+1;j<=n-1;j++)
if(max>a[j])
{
max=a[j];
loc=j;
}
return(loc);
}
void display(int a[])
{
int i;
printf("The sorted array in descending order is:\n");
for(i=n-1;i>=0;i--)
printf("%d ",a[i]);
}
void insertion_sort(int a[])
{
int pass,k,temp,j;
for(pass=1;pass<n;pass++)
{
k=a[pass];
for(j=pass-1;j>=0&&k<a[j];j--)
a[j+1] = a[j];
a[j+1] =k;
}
}
void selection_sort(int a[])
{
int i,loc,temp;
for(i=0;i<n;i++)

5
{
loc = max(a,i,n);
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
void main()
{
clrscr();
printf("\nPerforming Insertion sort ... \n");
insertion_sort(ia);
display(ia);

printf("\n\nPerforming Selection sort ...\n");


selection_sort(sa);
display(sa);

getch();
}

Ouput:

4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete 8,61,27 from
the list. Display your list after each insertion and deletion.

# include<stdio.h>
# include<conio.h>
# include<alloc.h>
# include<ctype.h>
typedef struct node
{
int info;
struct node *link;
}NODE;

NODE *start=NULL;

6
void DISPLAY()
{
NODE *curptr=start;
printf("\n *** LIST *** : ");
while(curptr!=NULL)
{
printf("%4d",curptr->info);
curptr=curptr->link;
}
}
void INSERT(int item)
{
NODE *newnode,*curptr;
newnode = (NODE *) malloc(sizeof(NODE));
newnode->info=item;
newnode->link=NULL;
if(start==NULL)
start=newnode;
else
{
curptr=start;
while(curptr->link !=NULL)
{
curptr=curptr->link;
}
curptr->link=newnode;
}
DISPLAY();
}
void DELETE(int item)
{
NODE *curptr = start,*prevptr=NULL;
if(start == NULL)
{
printf("\nThe linked list is empty");
return;
}
else if(start->info == item)
{
start=start->link;
free(curptr);
}
else
{
while(curptr !=NULL && curptr->info != item)
{
prevptr = curptr;
curptr = curptr->link;
}
if(curptr == NULL)
printf("\nThe item is not found in the linked list\n");
else
prevptr->link = curptr->link;

7
DISPLAY();
}

void main()
{
clrscr();
printf("\n Insertion :");
INSERT(61);
INSERT(16);
INSERT(8);
INSERT(27);
printf("\n\n Deletion :");
printf("\n Deleting 8...");
DELETE(8);
printf("\n Deleting 61...");
DELETE(61);
printf("\n Deleting 27...");
DELETE(27);
getch();
}

Output:

5. Write a program to insert the elements {61,16,8,27} into linear queue and delete three elements
from the list. Display your list after each insertion and deletion.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 10
int queue[N],front=0,rear=-1,item;
void Qdisplay()
{
int i;
if(rear == -1)
printf("\nNo elements in the queue");
else
{
printf("\nQueue: ");

8
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}

void Qinsert(int item)


{
if(rear == N-1)
printf("\nQueue overflow");
else
{
rear++;
queue[rear]=item;
}
Qdisplay();

}
void Qdelete()
{
if(rear == -1)
printf("\nQueue Underflow");
else
{
item=queue[front];
printf("\nDeleted item is %d",queue[front]);
if(front==rear)
{
front=0;
rear=-1;
}
else
front++;
}
Qdisplay();

}
void main()
{
clrscr();
Qinsert(61);
Qinsert(16);
Qinsert(8);
Qinsert(27);

Qdelete();
Qdelete();
Qdelete();
getch();
}

9
Output

6. Write a program to insert the elements {61,16,8,27} into circular queue and delete 4 elements from
the list. Display your list after each insertion and deletion.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 10
int queue[N],front=-1,rear=-1,item;
void CQdisplay()
{
int i;
if(rear==-1)
printf("\nNo elements in the queue");
else
{
printf("\nCircular queue: ");
if(front<=rear)
{
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
if(front>rear)
{
for(i=front;i<=N-1;i++)
printf("\t%d",queue[i]);
for(i=0;i<=rear;i++)
printf("%d",queue[i]);
}
}
}

void CQinsert(int item)


{
if(front == (rear+1)%N)
printf("\nQueue overflow");
else
{
if(front == -1)
front=rear=0;

10
else
rear=(rear+1)%N;
queue[rear]=item;
}

CQdisplay();

}
void CQdelete()
{
if(rear== -1)
printf("\nQueue Underflow");
else
{
item=queue[front];
printf("\nDeleted item is %d",queue[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=(front+1)%N;
}

CQdisplay();

void main()
{
clrscr();
CQinsert(61);
CQinsert(16);
CQinsert(8);
CQinsert(27);

CQdelete();
CQdelete();
CQdelete();
getch();
}

Output:

10
7. Write a program to insert the elements {61,16,8,27} into ordered singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion.

# include<stdio.h>
# include<conio.h>
# include<alloc.h>
# include<ctype.h>
typedef struct node
{
int info;
struct node *link;
}NODE;

NODE *start=NULL;

void DISPLAY()
{
NODE *curptr=start;
printf("\n *** LIST *** : ");
while(curptr!=NULL)
{
printf("%4d",curptr->info);
curptr=curptr->link;
}
}
void INSERT(int item)
{
NODE *newnode,*curptr,*prevptr;
newnode = (NODE *) malloc(sizeof(NODE));
newnode->info=item;
newnode->link=NULL;
if(start==NULL)
start=newnode;
else if(item < start->info)
{
newnode->link = start;
start = newnode;
}
else
{
prevptr = start;
curptr= start->link;
while(curptr->link !=NULL && item >curptr->info)
{
prevptr = curptr;
curptr=curptr->link;
}
prevptr->link=newnode;
newnode->link = curptr;
}
DISPLAY();
}
void DELETE(int item)

11
{
NODE *curptr = start,*prevptr=NULL;
if(start == NULL)
{
printf("\nThe linked list is empty");
return;
}
else if(start->info == item)
{
start=start->link;
free(curptr);
}
else
{
while(curptr !=NULL && curptr->info != item)
{
prevptr = curptr;
curptr = curptr->link;
}
if(curptr == NULL)
printf("\nThe item is not found in the linked list\n");
else
prevptr->link = curptr->link;

}
DISPLAY();
}

void main()
{
clrscr();
printf("\n Insertion :");
INSERT(61);
INSERT(16);
INSERT(8);
INSERT(27);
printf("\n\n Deletion :");
printf("\n Deleting 8...");
DELETE(8);
printf("\n Deleting 61...");
DELETE(61);
printf("\n Deleting 27...");
DELETE(27);

getch();
}

12
Output

8. Write a program to add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct polynomial
{
int coeff;
int pow;
struct polynomial *link;
};

typedef struct polynomial node;


node *poly1=NULL, *poly2=NULL, *poly3=NULL;
void display_poly(node *ptr)
{
while(ptr != NULL)
{
printf("%dx^%d",ptr->coeff,ptr->pow);
ptr = ptr->link;
if(ptr !=NULL)
printf(" + ");
}
}

13
node* create_poly()
{
char ch;
int coeff,pow;
node* tmp_node=(node*)malloc(sizeof(node));
node* poly =tmp_node;
while(1)
{
printf("\nEnter coeff:");
scanf("%d",&coeff);
printf("\nEnter power:");
scanf("%d",&pow);
tmp_node->coeff = coeff;
tmp_node->pow=pow;

printf("Do you want to add more terms?(Y/N): ");


ch=getche();
if(toupper(ch)=='Y')
{
tmp_node->link = (node*)malloc(sizeof(node));
tmp_node=tmp_node->link;
tmp_node->link = NULL;
}
else
{
tmp_node->link = NULL;
break;
}
}
return poly;
}
void add_poly(node* poly1,node* poly2)
{
node* tmp_node;
tmp_node=(node*)malloc(sizeof(node));
tmp_node->link=NULL;
poly3=tmp_node;

while(poly1&&poly2)
{
if(poly1->pow >poly2->pow){
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->link;
}
else if(poly1->pow <poly2->pow){
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->link;
}
else
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff+poly2->coeff;

14
poly1 = poly1->link;
poly2 = poly2->link;
}
if(poly1&&poly2)
{
tmp_node->link =(node*)malloc(sizeof(node));
tmp_node = tmp_node->link;
tmp_node->link = NULL;
}
}
while(poly1 || poly2)
{
tmp_node->link =(node*)malloc(sizeof(node));
tmp_node = tmp_node->link;
tmp_node->link = NULL;

if(poly1)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1=poly1->link;
}
if(poly2)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1=poly1->link;
}
}
}
void main()
{
clrscr();
printf("\nEnter the first polynomial\n");
poly1=create_poly();
printf("\nEnter the second polynomial\n");
poly2=create_poly();
add_poly(poly1,poly2);

printf("\nThe first polynomial is \n");


display_poly(poly1);
printf("\nThe second polynomial is \n");
display_poly(poly2);
printf("\nThe addition of two polynomials is:\n");
display_poly(poly3);
getch();
}

15
Output:

9. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also display the
popped numbers.

#include<stdio.h>
#include<conio.h>
#define maxstk 5

void display();
int top=-1;
int s[maxstk];

void push(int item)


{
if(top == (maxstk-1))
printf("Stack overflow\n");
else
{
top=top+1;
s[top]=item;
}
display();

16
}
void pop()
{
if(top==-1)
printf("Stack underflow\n");
else
{
printf("\nPopped element is : %d",s[top]);
top=top-1;
}
display();
}

void display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("\nStack elements :");
for(i=top;i>=0;i--)
printf("%d ",s[i]);
}
}
void main()
{
clrscr();
push(5);
push(9);
push(34);
push(17);
push(32);

pop();
pop();
pop();

getch();
}

Output:

17
10. Write a recursive program to find GCD of 4,6,8.

#include<stdio.h>
int gcd(int m,int n)
{
if(n==0)
return m;
else
return(gcd(n,m%n));
}
void main()
{
int k,m,n;
clrscr();
printf("Enter three numbers :\n");
scanf("%d %d %d",&k,&m,&n);
printf("GCD = %d\n",gcd(k,gcd(m,n)));
getch();
}

Ouput:

Enter three numbers:


12 15 21
GCD = 3

11. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete 6,9&5 from
it(using linked list implementation)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct queue
{
int info;
struct queue *link;
};
struct queue *front=NULL,*rear=NULL;

void CQdisplay()
{
struct queue *ptr;
ptr=front;
if(front== NULL && rear == NULL)
printf("\nNo elements in the queue");
else
{
printf("\nCircular queue: ");
do{
printf(" %d ",ptr->info);
ptr=ptr->link;
}while(ptr!=front);
}
}

18
void CQinsert(int item)
{
struct queue *new_node;
new_node = (struct queue*)malloc(sizeof(struct queue));
new_node->info = item;
new_node->link = NULL;
if(front == NULL && rear==NULL)
{
front = rear = new_node;
rear->link = front;
}
else
{
rear->link = new_node;
rear = new_node;
rear->link = front;
}
CQdisplay();
}
void CQdelete()
{
struct queue *ptr;
ptr=front;
if(front== NULL&&rear==NULL)
printf("\nQueue Underflow");
else if(front == rear)
{
front=rear=NULL;
printf("\nThe value being deleted is %d",ptr->info);
free(ptr);
}
else
{
front=front->link;
rear->link =front;
printf("\nThe value being deleted is %d",ptr->info);
free(ptr);
}
CQdisplay();
}

void main()
{
int ch;
clrscr();
printf("\n*****Insertion*****\n");
CQinsert(5);
CQinsert(7);
CQinsert(0);
CQinsert(6);
CQinsert(3);
CQinsert(9);
printf("\n*****Deletion*****\n");
CQdelete();
CQdelete();

19
CQdelete();
CQdelete();
CQdelete();
CQdelete();
getch();
}

Output:

12. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#define max 20
char s[max];
int top=0;

int precedence(char elem)


{
switch(elem)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '^':return(3);
case '(':
case '#':return(0);
}
}

void push(char elem)


{
++top;

20
s[top] = elem;
}
int pop()
{
char elem;
elem=s[top];
--top;
return(elem);
}
void main()
{
char infix[max],postfix[max],ch,elem;
int i=0,j=0;
clrscr();
printf("\nEnter the infix expression :\n");
scanf("%s",infix);
push('#');
for(i=0;i<strlen(infix);i++)
{
ch=infix[i];
if(isalnum(ch))
postfix[j++] =ch;
else if(ch=='(')
push(ch);
else if(ch==')')
{
while(s[top] !='(')
postfix[j++] = pop();
elem = pop();
}
else
{
while(precedence(s[top]) >=precedence(ch))
postfix[j++] = pop();
push(ch);
}
}
while(s[top]!='#')
postfix[j++] = pop();
postfix[j]='\0';
printf("\nPostfix Expression coversion is:\n %s\n",postfix);
getch();
}
Ouput:

Enter the infix expression :


x^y/(5*z)+2

Postfix Expression coversion is:


xy^5z*/2+

13. Write a program to evaluate a postfix expression 5 3+8 2 - *.

#include<stdio.h>

#include<conio.h>
2
11
#include<ctype.h>
#include<string.h>
#include<math.h>
#define max 20
int s[max],top=0;
void push(int element)
{
++top;
s[top] =element;
}
int pop()
{
int element;
element=s[top];
--top;
return element;
}
void main()
{
char postfix[max],ch;
int i,op1,op2,res;
clrscr();
printf("Enter the postfix expression\n");
scanf("%s",&postfix);
for(i=0;i<strlen(postfix);i++)
{
ch=postfix[i];
if(isdigit(ch))
push(ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+' : res = op1+op2;
break;
case '-' : res = op1 - op2;
break;
case '*' : res = op1 * op2;
break;
case '/' : res = op1 / op2;
break;
case '^' : res = pow(op1,op2);
break;
}
push(res);
}
}
printf("Result of the above expression is : %d \n",pop());
getch();
}
Ouput:
Enter the postfix expression

2
12
53+82-*
Result of the above expression is : 48
14. Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} after creation
insert 45 and 19 into tree and delete 15,17 and 41 from tree. Display the tree on each insertion and
deletion operation

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *left;
struct node *right;
};
typedef struct node NODE;
NODE *root=NULL;
void create(int item)
{
NODE *newnode,*currptr,*ptr;
newnode = (NODE*)malloc(sizeof(NODE));
newnode->info = item;
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)
root = newnode;
else
{
currptr =root;
while(currptr!=NULL)
{
ptr=currptr;
currptr=(item>currptr->info)?currptr->right:currptr->left;
}
if(item < ptr->info)
ptr->left = newnode;
else
ptr->right = newnode;
}
}
void disp(struct node *ptr,int level)
{
int i;
if(ptr!=NULL)
{
disp(ptr->right,level+1);
for(i=0;i<level;i++)
printf(" ");
printf("%5d\n",ptr->info);
disp(ptr->left,level+1);
}
}
NODE* getInSuccessor(NODE *ptr)
{
while(ptr->left !=NULL)
ptr=ptr->left;
return ptr;
2
13
}
NODE* delete(NODE *p,int item)
{
NODE *temp;
if(!p)
{
printf("Unable to delete. No such key exists\n");
return p;
}
else if(item> p->info)
p->right=delete(p->right,item);
else if(item < p->info)
p->left = delete(p->left,item);
else
{
if(p->left ==NULL){
temp = p->right;
free(p);
return temp;
}
else if(p->right ==NULL)
{
temp = p->left;
free(p);
return temp;
}
temp = getInSuccessor(p->right);
p->info = temp->info;
p->right = delete(p->right,temp->info);
}
return p;
}
void main()
{
int item,ch,n,i;
clrscr();
while(1)
{
printf("\n1. Create");
printf("\n2. Insert");
printf("\n3. Delete");
printf("\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the number of nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("dEnter the data for the node: ");
scanf("%d",&item);
create(item);
}
printf("\nBinary Tree is as follows\n");

2
14
disp(root,1);
break;
case 2: printf("\nEnter the date for the node: ");
scanf("%d",&item);
create(item);
printf("\nBinary tree after insertion\n ");
disp(root,1);
break;
case 3: printf("\nEnter the item to be deleted: ");
scanf("%d",&item);
root=delete(root,item);
printf("\nBinary tree after deletion\n ");
disp(root,1);
break;
case 4: exit(1);
}
}
}
Output:

2
15
2
16
2
17
2
18
2
19
15. Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform
inorder, preorder and post order traversal.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct BST {
int data;
struct BST *lchild, *rchild;
}node;

node *create_node()
{
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert(node *root, node *new_node)
{
if (new_node->data < root->data)

30
{
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);
}
}
void inorder(node *temp)
{
if (temp != NULL)
{
inorder(temp->lchild);
printf("%3d", temp->data);
inorder(temp->rchild);
}
}
void preorder(node *temp)
{
if (temp != NULL)
{
printf("%3d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
void postorder(node *temp)
{
if (temp != NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%3d", temp->data);
}
}
void main()
{
int n=7,i=1;
node *new_node, *root;
node *create_node();
root = NULL;
clrscr();
printf("\nProgram For Binary Search Tree ");
for(i=1;i<=n;i++)
{
new_node = create_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */

31
root = new_node;
else
insert(root, new_node);
}
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
getch();
}

Output:

16. Write a program to Sort the following elements using heap sort {9,16,32,8,4,1,5,8,0}
#include<stdio.h>
#include<conio.h>
void heapify(int arr[],int n,int i)
{
int largest=i;
int left=2*i+1;
int right=2*i+2;
int temp;
if(left <n && arr[left] >arr[largest])
largest =left;
if(right<n && arr[right] >arr[largest])
largest = right;
if(largest !=i)
{
temp =arr[i];
arr[i] =arr[largest];
arr[largest] = temp;
heapify(arr,n,largest);
}
}
void heapSort(int arr[],int n)
{
int i,temp;
for(i=n/2-1;i>=0;i--)
heapify(arr,n,i);
for(i=n-1;i>=0;i--)

32
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr,i,0);
}
}
void main()
{
int arr[20];
int n,i;
clrscr();
printf("Enter the size of the array");
scanf("%d",&n);
printf("\nEnter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
heapSort(arr,n);
printf("Sorted array is ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
getch();
}

Output:

17. Given S1={“Flowers”} ; S2={“are beautiful”} I. Find the length of S1 II. Concatenate S1 and S2 III.
Extract the substring “low” from S1 IV. Find “are” in S2 and replace it with “is”
#include<stdio.h>
#include<conio.h>
#include<string.h>
int LENGTH(char *str)
{
int i=0, len=0;
while(str[i]!='\0')
{
len++;
i++;
}
return(len);
}
void CONCAT(char *str1, char *str2)
{
int i=0,j=0;
while(str1[i]!='\0')
{
i++;

33
}
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
printf("\n Concatenated string = %s",str1);
}
void EXTRACT(char *str,int pos, int elen)
{
int i=0,j=0;
char substr[10];
for(i=pos;i<=elen;i++)
{
substr[j]=str[i];
j++;
}
substr[j]='\0';
printf("\n Substring = %s",substr);
}
void REPLACE(char *str, char *sstr, char *rstr, int pos)
{
char output[50];
int i=0,j=0,k=0;
for(i=0;i<LENGTH(str);i++)
{
if(i==pos)
{
for(k=pos;k<LENGTH(rstr);k++)
{
output[j]=rstr[k];
j++;
i++;
}
}
else
{
output[j]=str[i];
j++;
}
}
output[j]='\0';

34
printf("\n Output = %s",output);
getch();
}
void main()
{
char *S1,*S2;
int len,choice,pos,elen;
while(1)
{
clrscr();
strcpy(S1,"Flowers");
strcpy(S2,"are beautiful");
printf("\n S1 = %s S2 = %s",S1,S2);
printf("\n 1. Length 2.Concatenate 3.Extract Substring 4.REPLACE
5.Exit\n");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :len = LENGTH(S1);
printf("\n Length of %s = %d",S1,len);
break;
case 2: CONCAT(S1,S2);
break;
case 3: printf("\n Enter position & length of substring in S1 : ");
scanf("%d %d",&pos,&elen);
EXTRACT(S1,pos,elen);
break;
case 4: REPLACE(S2,"are","is",0);
break;
case 5: exit(0);
default : printf("\n Invalid option");
}
getch();
}
}
Output:

35
36

You might also like