0% found this document useful (0 votes)
408 views87 pages

DS Lab

The document provides programs to perform operations on data structures like linked lists, stacks, queues and binary trees. It includes programs to insert and delete elements from a linked list, perform sorting, traverse trees, and other standard operations on these data structures.

Uploaded by

CHETHAN KUMAR
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)
408 views87 pages

DS Lab

The document provides programs to perform operations on data structures like linked lists, stacks, queues and binary trees. It includes programs to insert and delete elements from a linked list, perform sorting, traverse trees, and other standard operations on these data structures.

Uploaded by

CHETHAN KUMAR
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/ 87

DATA STRUCTURES LAB MANNUAL

1 MCA7: DATA STRUCTURES LAB PROGRAMS

1. Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and also display
its first occurrence.
2. Given {5, 3,1,6,0,2,4} order the numbers in ascending order using Quick Sort.
3. Perform the Merge 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 add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.
6. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack,
also display the popped numbers.
7. Write a recursive program to find GCD of 4,6,8.
8. 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).
9. Given S1={“Flowers”} ; S2={“are beautiful”} ,
a) Find the length of S1.
b) Concatenate S1 and S2.
c) Extract the substring “low” from S1.
d) Find “are” in S2 and replace it with “is”.
10. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix
expression.
11. Write a program to evaluate a postfix expression 5 3+8 2 - *.
12. 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.
13. 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.
14. Write a program to Sort the following elements using heap sort
{9.16,32,8,4,1,5,8,0}.
1.Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and also display its first
occurrence.

#include<stdio.h>

int binsearch(int A[],int item,int first,int last)

int middle;

if(first>last)

return(-1);

else

middle=(first+last)/2;

if(item<A[middle])

binsearch(A,item,first,middle-1);

else if(item>A[middle])

binsearch(A,item,middle+1,last);

else

return(middle);

void main()

int A[20],n,i,item,result;

printf("Enter the element into the array\n");

scanf("%d",&n);

for(i=0;i<n;i++)
{

printf("Enter Element %d\n",i+1);

scanf("%d",&A[i]);

printf("Enter the element to be searched\n");

scanf("%d",&item);

result=binsearch(A,item,0,n-1);

if(result==1)

printf("%d not found ",item);

else

printf("element %d found at %d \n",item,result+1);

getch();

INPUT&OUTPUT

Enter the element into the array :8

Enter array element1:4

Enter array element2:7

Enter array element3:3

Enter array element4:2

Enter array element5:1

Enter array element6:7

Enter array element7:9

Enter array element8:0


Enter the element to be searched : 7

Element 7 founded at position 6

*************************************************

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

#include<stdio.h>

quick(int a[],int lb,int ub);

void main()

int i,n,a[20],lb,ub;

clrscr();

printf("+++++++++++++++++++++++++\n");

printf("\n INPUT \n");

printf("\n enter the size of the array :");

scanf("%d",&n);

printf("\n Enter the elements of the array :");

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

lb=1;

ub=n;

printf("\n***********************\n");

printf("\n OUTPUT \n");

printf("\n Original List \n");

for(i=1;i<=n;i++)
printf("%d",a[i]);

quick(a,lb,ub);

printf("\n Sorted List \n");

for(i=1;i<=n;i++)

printf("%d",a[i]);

printf("\n+++++++++++++++++++++++\n");

getch();

quick(int a[],int lb,int ub)

int up,down,temp,key;

int flag=1;

if(lb<ub)

up=lb;

down=ub;

key=a[lb];

while(flag)

up++;

while(a[up]<key)

up++;

while(a[down]>key)

down--;

if(up<down)
{

temp=a[up];

a[up]=a[down];

a[down]=temp;

else

flag=0;

temp=a[lb];

a[lb]=a[down];

a[down]=temp;

quick(a,lb,down-1);

quick(a,down+1,ub);

return;

}
3. Perform the Merge 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>

mergesort(int *,int,int);

merge(int *,int,int,int);

void main()

int a[20],n,i;

clrscr();

printf("\n Enter the number of elements :");

scanf("%d",&n);

printf("enter the elements :");

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

scanf("%d",&a[i]);

printf("Original List \n");

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

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

mergesort(a,0,n-1);

printf("\n Sorted List in decreasing order \n");

for(i=n-1;i>=0;i--)

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

getch();

mergesort(int a[],int lb,int ub)


{

int mid;

if(lb<ub)

mid=(lb+ub)/2;

mergesort(a,lb,mid);

mergesort(a,mid+1,ub);

merge(a,lb,mid,ub);

merge(int a[],int lb,int mid,int ub)

int i,j,k,c[20];

i=lb;

k=lb;

j=mid+1;

while((i<=mid)&&(j<=ub))

if(a[i]<a[j])

c[k]=a[i];

k++;

i++;

else
{

c[k]=a[j];

k++;

j++;

while(i<=mid)

c[k]=a[i];

k++;

i++;

while(j<=ub)

c[k]=a[j];

k++;

j++;

for(i=lb;i<=k-1;i++)

a[i]=c[i];

}
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>

struct link

int item;

struct link *next;

};

typedef struct link node;

void addfirst();

void addlast();

void addmid();

void delfirst();

void dellast();

void delmid();

void display();

node *head=NULL;

void main()

int ch;

clrscr();

do

printf("\nSINGLY LINKED LIST OPERATIONS\n");


printf("\n1.Addfirst\n2.AddMid\n3.AddLast\n4.DeleteFirst\n5.DeleteMiddle\n6.DeleteLast\
n7.Display\n8.Exit\n");

printf("Enter your option:\t");

scanf("%d",&ch);

switch(ch)

case 1:

addfirst();

display();

break;

case 2:

addmid();

display();

break;

case 3:

addlast();

display();

break;

case 4:

delfirst();

display();

break;

case 5:

delmid();

display();
break;

case 6:

dellast();

display();

break;

case 7:

display();

break;

case 8:

exit(0);

break;

default:

printf("Invalid Choice\n");

while(ch<=8);

getch();

void addfirst()

node *temp;

temp=(node *)malloc(sizeof(node));

printf("Enter the data....\t");

scanf("%d",&temp->item);

temp->next=head;
head=temp;

void addmid()

int i=1,pos;

node *cur=head,*temp;

printf("\nEnter the position\t");

scanf("%d",&pos);

while(pos!=i+1&&cur!=NULL)

cur=cur->next;

i++;

if(pos==i+1)

temp=(node *)malloc(sizeof(node));

printf("Enter the data...");

scanf("%d",&temp->item);

temp->next=cur->next;

cur->next=temp;

void addlast()

node *temp,*cur=head;
temp=(node *)malloc(sizeof(node));

printf("\nEnter the data....");

scanf("%d",&temp->item);

while(cur->next!=NULL)

cur=cur->next;

temp->next=cur->next;

cur->next=temp;

void delfirst()

node *temp=head;

head=head->next;

printf("Deleted item is %d\n",temp->item);

free(temp);

void delmid()

int i=1,pos;

node *cur=head,*temp;

printf("Enterthe positionto bedeleted\t");

scanf("%d",&pos);

while(pos!=i+1&&cur->next!=NULL)

{
cur=cur->next;

i++;

if(pos==i+1)

temp=cur->next;

cur->next=temp->next;

printf("Deleted item is %d\n",temp->item);

free(temp);

void dellast()

node *temp,*cur=head;

while(cur->next->next!=NULL)

cur=cur->next;

temp=cur->next;

cur->next=NULL;

printf("Deleted item is %d\n",temp->item);

free(temp);

void display()

{
node *cur=head;

printf("\nHead->");

while(cur!=NULL)

printf("\t%d",cur->item);

cur=cur->next;

printf("<-NULL\n");

---------------------------------------------------------------------------------------------------------------------
Output:

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit

Enter your option: 1

Enter the data.... 61

Head-> 61<-NULL
SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit

Enter your option: 2

Enter the position 2

Enter the data.... 16

Head->61 16<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit

Enter your option: 2

Enter the position 3


Enter the data.... 8

Head->61 16 8<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit

Enter your option: 3

Enter the data.... 27

Head->61 16 8 27<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit
Enter your option: 5

Enter the position to be deleted 3

Deleted item is 8

Head->61 16 27<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit

Enter your option: 4

Deleted item is 61

Head->16 27<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast
7.Display

8.Exit

Enter your option: 6

Deleted item is 27

Head-> 16<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle

6.DeleteLast

7.Display

8.Exit

Enter your option: 7

Head-> 16<-NULL

SINGLY LINKED LIST OPERATIONS

1.Addfirst

2.AddMid

3.AddLast

4.DeleteFirst

5.DeleteMiddle
6.DeleteLast

7.Display

8.Exit

Enter your option: 8

5. 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 Node

int coeff;

int pow;

struct Node* next;

};

void readPolynomial(struct Node** poly)

int coeff, exp, cont;

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

*poly = temp;

do{

printf("\n Coeffecient: ");

scanf("%d", &coeff);

printf("\n Exponent: ");

scanf("%d", &exp);
temp->coeff = coeff;

temp->pow = exp;

temp-> next = NULL;

printf("\nHave more terms? 1 for y and 0 for no: ");

scanf("%d", &cont);

if(cont)

temp->next = (struct Node*)malloc(sizeof(struct Node));

temp = temp->next;

temp->next = NULL;

}while(cont);

void displayPolynomial(struct Node* poly)

printf(" Polynomial expression is: ");

while(poly != NULL)

printf("%dX^%d", poly->coeff, poly->pow);

poly = poly->next;

if(poly != NULL)

printf("+");

}
void addPolynomials(struct Node** result, struct Node* first, struct Node* second)

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

temp->next = NULL;

*result = temp;

while(first && second)

if(first->pow> second->pow)

temp->coeff = first->coeff;

temp->pow = first->pow;

first = first->next;

else if(first->pow< second->pow)

temp->coeff = second->coeff;

temp->pow = second->pow;

second = second->next;

else

temp->coeff = first->coeff + second->coeff;

temp->pow = first->pow;

first = first->next;
second=second->next;

if(first && second)

temp->next = (struct Node*)malloc(sizeof(struct Node));

temp = temp->next;

temp->next = NULL;

while(first || second)

temp->next = (struct Node*)malloc(sizeof(struct Node));

temp = temp->next;

temp->next = NULL;

if(first)

temp->coeff = first->coeff;

temp->pow = first->pow;

first = first->next;

else if(second)

temp->coeff = second->coeff;

temp->pow = second->pow;
second = second->next;

void main()

struct Node* first = NULL;

struct Node* second = NULL;

struct Node* result = NULL;

clrscr();

printf("\nEnter the corresponding data:-\n");

printf("\nFirst polynomial:\n");

readPolynomial(&first);

printf("\nFirst");

displayPolynomial(first);

printf("\nSecond polynomial:\n");

readPolynomial(&second);

printf("\nSecond");

displayPolynomial(second);

printf("\n ------------------------------------------------\n");

printf("\n First");

displayPolynomial(first);

printf("\n Second ");

displayPolynomial(second);

printf("\n ------------------------------------------------\n");
addPolynomials(&result, first, second);

printf("\nAdded");

displayPolynomial(result);

getch();

6. 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>

struct node

int info;

struct node *link;

*top=NULL;

void main()

int choice;

while(1)

printf("STACK OPERATION\n");

printf("---------------\n");

printf("1. Push \n");


printf("2. Pop \n");

printf("3. Display \n");

printf("4. Quit \n");

printf("Enter Your Choice :");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong Choice!!!\n");

push()

{
struct node *NEWNODE;

int pushed_item;

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

printf("Input the new value to be pushed on the stack :");

scanf("%d",&pushed_item);

NEWNODE ->info=pushed_item;

NEWNODE ->link=top;

top=NEWNODE;

pop()

struct node *NEWNODE;

if(top==NULL)

printf("Stack is Empty!!!");

else

NEWNODE = top;

printf("Poped item is %d \n",NEWNODE->info);

top=top->link;

free(NEWNODE);

display()

struct node *ptr;


ptr=top;

if(top==NULL)

printf("Stack is empty\n");

else

printf("Stack elements are : \n");

while(ptr!=NULL)

printf("%d\n",ptr->info);

ptr= ptr->link;

Output:

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :1

Input the new value to be pushed on the stack :5

STACK OPERATION

----------------
1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :1

Input the new value to be pushed on the stack :9

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :1

Input the new value to be pushed on the stack :34

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :1

Input the new value to be pushed on the stack :17


STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :1

Input the new value to be pushed on the stack :32

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :3

Stack elements are :

32

17

34

STACK OPERATION

----------------
1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :2

Poped item is 32

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :2

Poped item is 17

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :2

Poped item is 34
STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :3

Stack elements are :

STACK OPERATION

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

1. Push

2. Pop

3. Display

4. Quit

Enter Your Choice :4

*****************************************************************************

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

#include<stdio.h>

#include<conio.h>

int greatest_num(int a, int b, int c)

if(a>=b && a>=c)


{

return a;

else if(b>=a && b>=c)

return b;

else if(c>=a && c>=b)

return c;

void main()

Int m,n,o,result;

clrscr();

printf ("Enter the first number: ");

scanf("%d",&m);

printf("Enter the second number: ");

scanf("%d",&n);

printf("Enter the third number: ");

scanf("%d",&o);

for(result=greatest_num(m,n,o); result>=1; result--)

{
if(m%result==0 && n%result==0 && o%result==0)

break;

printf("GCD of the given numbers is : %d\n",result);

getch();

******************************************************************************

8. 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>

struct cq

int data;

struct cq *next;

}*f=NULL,*r=NULL,*n,*temp,*temp1;

void cq_ins();

void cq_del();

void cq_dis();

int main()

int choice;

clrscr();
while(1)

printf("\n\n\t Main Menu");

printf("\n##########################");

printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Your Choice: ");

scanf("%d",&choice);

switch(choice)

case 1:

cq_ins();

cq_dis();

break;

case 2:

cq_del();

break;

case 3:

cq_dis();

break;

case 4:

exit(0);

getch();

break;

default:

printf("\n\nWrong Choice!!! TryAgain.");

}
}

return 0;

void cq_ins()

int i;

n=(struct cq*)malloc(sizeof(struct cq));

printf("\nEnter the Element: ");

scanf("%d",&n->data);

if(f==NULL)

f=n;

else

r->next=n;

r=n;

r->next=f;

void cq_del()

int x;

temp=f;

if(f==NULL)
{

printf("\nCircular Queue Empty!!!");

else

if(f==r)

x=f->data;

free(temp);

f=NULL;

r=NULL;

else

x=temp->data;

f=f->next;

r->next=f;

free(temp);

printf("\nElement %d is Deleted",x);

cq_dis();

void cq_dis()

{
temp=f;

temp1=NULL;

if(f==NULL)

printf("\n\nCircular Queue Empty!!!");

else

printf("\n\nCircular Queue Elements are:\n\n");

while(temp!=temp1)

printf("%d ",temp->data);

temp=temp->next;

temp1=f;

Output:

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 1


Enter the Element: 5

Circular Queue Elements are:

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 1

Enter the Element: 7

Circular Queue Elements are:

57

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 1

Enter the Element: 0


Circular Queue Elements are:

570

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 1

Enter the Element: 6

Circular Queue Elements are:

5706

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 1

Enter the Element: 3


Circular Queue Elements are:

57063

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 1

Enter the Element: 9

Circular Queue Elements are:

570639

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 2

Element 5 is Deleted

Circular Queue Elements are:


70639

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 2

Element 7 is Deleted

Circular Queue Elements are:

0639

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 2

Element 0 is Deleted

Circular Queue Elements are:

639
MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 3

Circular Queue Elements are:

639

MAIN MENU

#################################################

1. Insert

2. Delete

3. Display

4. Exit

Enter Your Choice: 4

******************************************************************************

9: Given S1={“Flowers”} ; S2={“are beautiful”} ,


a) Find the length of S1.

b) Concatenate S1 and S2.

c) Extract the substring “low” from S1.

d) Find “are” in S2 and replace it with “is”.

#include<stdio.h>

#include<conio.h>

void concat(char *,char *);

void extract(char *,char *,int,int);

void strreplace(char *,char *,int);

void main()

char str[20],str1[25],str2[25],str3[25],og[50],ss[50];

int l,x,y,st,len,pos,ch;

do

clrscr();

printf("\n\t***** MAIN MENU *****\n");

printf("\n1. Length of a String\n");

printf("\n2. Concatenate TwoStrings\n");

printf("\n3. Extract a Substring from a given String\n");

printf("\n4. Find and Replace a String\n");

printf("\n5. Exit\n");

printf("\n----------------------------------");

printf("\nSelect any one of the above==>\n");


scanf("%d",&ch);

switch(ch)

case 1: printf("Enter any string:\n");

scanf("%s",&str);

l=stringln(str);

printf("The length of the given string %s is:%d",str,l);

break;

case 2: fflush(stdin);

printf("\nEnter First String:\n");

scanf("%[^\n]s",&str1);

fflush(stdin);

printf("\nEnter Second String:\n");

scanf("%[^\n]s",&str2);

concat(str1,str2);

printf("\nConcatenated String is %s",str1);

break;

case 3:fflush(stdin);

printf("Enter the String:\n");

scanf("%[^\n]s",&og);

printf("Enter the start & end position to be extracted\n");

scanf("%d%d",&st,&len);

extract(og,ss,st,len);

printf("Your Substring is %s",ss);

break;
case 4:fflush(stdin);

printf("Enter the String\n");

scanf("%[^\n]s",&str1);

fflush(stdin);

printf("Enter the string to be found\n");

scanf("%[^\n]s",&str2);

pos=strfind(str1,str2);

if(pos>0)

fflush(stdin);

printf("Enter the string to be replaced\n");

scanf("%[^\n]s",&str3);

strreplace(str1,str3,pos);

printf("String After Replacing = %s",str1);

else

printf("String Not Found\n");

break;

case 5: exit(0);

break;

if(ch!=5) getch();

}
while(ch!=5);

int stringln(char *p)

int count=0;

while(*p!='\0')

count++;

p++;

return count;

void concat(char *s1,char *s2)

while(*s1!='\0')

s1++;

while(*s2!='\0')

*s1=*s2;

s1++;

s2++;

*s1='\0';

void extract(char *str1,char *str2,int s,int l)


{

int i;

str1=str1+(s-1);

for(i=0; i< l && str1 != '\0' ;i++)

*str2++=*str1++;

*str2='\0';

int strfind(char *p1, char *p2)

int len1,len2,i,j,f,pos=0;

len1=stringln(p1);

len2=stringln(p2);

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

f=1;

pos++;

for(j=0;j<len2;j++)

if(*(p1+i+j)!=*(p2+j))

f=0;

break;

}
}

if(f==1)

return(pos);

return (0);

void strreplace(char *p1,char *p3,int pos)

int i,len;

len=stringln(p3);

for(i=pos-1;i<(len+pos)-1;i++)

*(p1+i)=*p3;

p3++;

input / output

***** MAIN MENU*****

1. Length of a string

2. concatenate two string

3. Extract a Substing from a given string

4. Find and Replace a String


5. Exit

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

select any one of the above==>

Enter any string:

flowers

The length of the given string flowers is :7

***

select any one of the above==>

Enter the First string:

flowers

Enter the second string:

are beautiful

Concatenated String is flowers are beautiful

***

select any one of the above==>

Enter the First string:

flowers

Enter the start & end position to be extracted

2 3

your substring is low

***

select any one of the above==>


4

Enter the String

Flowers are beautiful

Enter the string to be found

are

Enter the string to be replaced

is

String After Replacing = flowers ise beautiful

******************************************************************************

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

#define SIZE 50

#include<ctype.h>

char s[SIZE];

int top=-1;

void push(char elem)

s[++top]=elem;

char pop()

return(s[top--]);

int pr(char elem)

{
switch(elem)

case '#':

return 0;

case '(':

return 1;

case '+':

case '-':

return 2;

case '*':

case '/':

return 3;

void main()

char infx[50],pofx[50],ch,elem;

int i=0,k=0;

clrscr();

printf("\n \n Read 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();

else

while(pr(s[top])>=pr(ch))

pofx[k++]=pop();

push(ch);

while(s[top]!='#')

pofx[k++]=pop();

pofx[k]='\0';

printf("\n \n Given Infix Expression %s \n postfix expression: %s \n",infx,pofx);

getch();

output :
Read the Infix expression : x^y/(5*z)+2

Given Infix expression : x^y/(5*z)+2

postfix expression: xy^5z*/2+

******************************************************************************

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

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

Symbol scanned stack

5 5

3 5 3

+ 8

8 8 8

2 8 8 2

- 8 6

* 48

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

#include<stdio.h>

#include<ctype.h>

int s[50];

int top = -1;

void push(int elem)

s[++top] = elem;

}
int pop()

return s[top--];

void main()

char pofx[50];

char *ch;

int op1,op2,num;

clrscr();

printf("Read the postfix expression :: ");

scanf("%s",pofx);

ch = pofx;

while(*ch != '\0')

if(isdigit(*ch))

num = *ch - 48;

push(num);

else

op1 = pop();
op2 = pop();

switch(*ch)

case '+': push(op1+op2);break;

case '-': push(op2-op1);break;

case '*': push(op1*op2);break;

case '/': push(op1/op2);break;

ch++;

printf("\n Result after the Evaluation %s = %d\n\n",pofx,pop());

getch();

output:

Read the Postfix expression :: 5 3 + 8 2 - *

Result after the Evaluation 5 3 + 8 2 - * = 48

******************************************************************************

12. 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<conio.h>

#include<stdio.h>

#include<malloc.h>

#include<process.h>

struct node
{

struct node *llink;

struct node*rlink;

int data;

};

void main()

struct node *head,*t;

int s,d;

struct node* finsert();

struct node* delenode(struct node*,int);

void insert(struct node *);

void inorder(struct node *);

head=NULL;

clrscr();

do

printf("\n\nImplementation of Binary Tree\n\n");

printf("1-Insertion\n\n");

printf("2-Deletion\n\n");

printf("3-Display\n\n");

printf("4-Exit\n\n");

printf("---------------------\n");

printf("Enter Choice:\n");

scanf("%d",&s);
switch(s)

case 1://insertion

if(head==NULL)

head=finsert();

else

insert(head);

break;

case 2://Deletion

if(head==NULL)

printf("Binary Tree Empty.......");

else

printf("Enter Data to delete:\n");

scanf("%d",&d);

if(head->llink==NULL && head->rlink==NULL && head->data==d)

t=head;

head=NULL;

free(t);

else

head = delenode(head,d);
}

break;

case 3://to display

printf("\nThe Elements are ");

if(head==NULL)

printf("Binary TreeEmpty....");

else

inorder(head);

break;

case 4://exit

exit(0);

}while(s<5 ||s>0);

getch();

struct node* finsert()

struct node * head;

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

printf("Enter The Elements:");

scanf("%d",&head->data);

head->llink=NULL;

head->rlink=NULL;

return head;

}
void insert(struct node*head)

struct node *t,*n;

t=head;

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

printf("Enter TheElements:");

scanf("%d",&n->data);

n->llink=NULL;

n->rlink=NULL;

while(t->llink!=NULL ||t->rlink!=NULL)

if(t->llink!=NULL)

if(n->data < t->data)

t=t->llink;

if(t->rlink!=NULL)

if(n->data>=t->data)

t=t->rlink;

if((t->llink==NULL)&&(n->data < t->data)&&(n->data <

t->rlink->data))

break;

if((t->rlink==NULL)&&(n->data>= t->data)&&(n->data >

t->llink->data))

break;

if((n->data<t->data)&&(t->llink==NULL))
t->llink=n;

if((n->data>t->data)&&(t->rlink==NULL))

t->rlink=n;

void inorder(struct node *head)

if(head!=NULL)

inorder(head->llink);

printf("%d ",head->data);

inorder(head->rlink);

struct node * delenode(struct node *head,int d)

int f=0,f1=0;

struct node *p,*t,*t1,*x;

t=head;

//to search found or not

while(t!=NULL)

if(t->data==d)

f=1;

x=t;
break;

if(t->data > d)

p=t;

t=t->llink;

else if(t->data <=d)

p=t;

t=t->rlink;

if(f==0)

printf("Given element not found.......");

return head;

//Deleted node has no child

if(x->llink==NULL && x->rlink==NULL)

if(p->rlink==x)

p->rlink=NULL;

else

p->llink=NULL;
free(x);

return head;

//deleted node has 2 children

if(x->llink!=NULL && x->rlink!=NULL)

p=x;

t1=x->rlink;

while(t1->llink!=NULL)

p=t1; f1=1;

t1=t1->llink;

if(t1->llink==NULL && t1->rlink==NULL)

x->data=t1->data;

if(f1==1)

p->llink=t1->llink;

if(f1==0)

x->rlink=t1->rlink;

free(t1);

return head;

if(t1->rlink!=NULL)

{
x->data=t1->data;

if(f1==1)

p->llink=t1->rlink;

if(f1==0)

p->rlink=t1->rlink;

free(t1);

return head;

//Deleted node has only right child

if(x->llink==NULL && x->rlink!=NULL && x->data!=head->data)

if(p->llink==x)

p->llink=x->rlink;

else

p->rlink=x->rlink;

free(x);

return head;

//Deleted node has oniy left child

if(x->llink!=NULL && x->rlink==NULL && x->data!=head->data)

if(p->llink==x)

p->llink=x->llink;

else
p->rlink=x->llink;

free(x);

return head;

if(x->llink!=NULL && x->rlink==NULL && x->data==head->data)

head=x->llink;

free(p);

return head;

if(x->llink==NULL && x->rlink!=NULL && x->data==head->data)

head=x->rlink;

free(p);

return head;

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

Output:

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

---------------------------------------
Enter Choice:

Enter The Elements: 18

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 18

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The elements: 15

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display
4-Exit

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

Enter Coice:

The Elements are 15 18

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The Elements: 40

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 15 18 40

Implmentation of Binary Tree

1-Insertion
2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The Elements: 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 15 18 40 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The Elements: 30


Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 15 18 30 40 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The Elements: 17

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:
3

The Elements are 15 17 18 30 40 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The Elements: 41

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 15 17 18 30 40 41 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit
---------------------------------------

Enter Choice:

Enter The Elements: 45

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 15 17 18 30 40 41 45 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter The Elements: 19

Implmentation of Binary Tree


1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 15 17 18 19 30 40 41 45 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter Data to Delete:

15

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

---------------------------------------
Enter Coice:

The Elements are 17 18 30 40 41 45 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter Data to Delete:

17

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 18 30 40 41 45 50

Implmentation of Binary Tree

1-Insertion
2-Deletion

3-Display

4-Exit

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

Enter Choice:

Enter Data to Delete:

41

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Coice:

The Elements are 18 30 40 45 50

Implmentation of Binary Tree

1-Insertion

2-Deletion

3-Display

4-Exit

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

Enter Choice:
4

13.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 tnode

int data;

struct tnode *right, *left;

TNODE;

TNODE *CreateBST(TNODE *,int);

void inorder(TNODE *);

void preorder(TNODE *);

void postorder(TNODE *);

void main()

TNODE *root=NULL;

int opn,elem,n,i;

do

clrscr();

printf("\n ### Binary Search Tree Operation ### \n\n");


printf("\n Press 1 - Creation of BST");

printf("\n 2 - Traversal In Inorder");

printf("\n 3 - Traversal In Preorder");

printf("\n 4 - Traversal In Postorder");

printf("\n 5 - Exit \n");

printf("\n Enter Your Choice : ");

scanf("%d", &opn);

switch(opn)

case 1:

root=NULL;

printf("\n\n BST for How many Nodes?? :");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n\n Read the data for node %d :",i);

scanf("%d",&elem);

root=CreateBST(root,elem);

printf("\n BST with %d node is ready to use!!!\n",n);

break;

case 2:

printf("BST Traversal in INORDER \n");

inorder(root);
break;

case 3:

printf("BST Traversal in PREORDER \n");

preorder(root);

break;

case 4:

printf("BST Traversal in POSTORDER \n");

postorder(root);

break;

case 5:

printf("\n\n Terminating \n\n");

break;

default:

printf("\n\n Invalid Option!!! Try again!! \n\n");

break;

printf("\n\n\n\n Press a key to continue...");

getch();

while(opn!=5);

TNODE *CreateBST(TNODE *root,int elem)

if(root==NULL)
{

root=(TNODE *)malloc(sizeof(TNODE));

root->left=root->right=NULL;

root->data=elem;

return root;

else

if(elem<root->data)

root->left=CreateBST(root->left,elem);

else if (elem> root->data)

root->right=CreateBST(root->right,elem);

else

printf("Duplicate Element Not Allowed!!! ");

return(root);

}------

void inorder(TNODE *root){

if(root!=NULL)

inorder(root->left);

printf("%d \t",root->data);

inorder(root->right);

}
}

void preorder(TNODE *root){

if(root!=NULL)

printf("%d \t",root->data);

preorder(root->left);

preorder(root->right);

void postorder(TNODE *root){

if(root!=NULL)

postorder(root->left);

postorder(root->right);

printf("%d \t",root->data);

-------

Output:

### Binary Search Tree Operation ###

Press 1 - Creation of BST

2 - Traversal In Inorder

3 - Traversal In Preorder

4 - Traversal In Postorder
5 - Exit

Enter Your Choice : 1

BST for How many nodes?? : 7

Read the data for node 1 : 2

Read the data for node 1 : 5

Read the data for node 1 : 1

Read the data for node 1 : 3

Read the data for node 1 : 9

Read the data for node 1 : 0

Read the data for node 1 : 6

BST with 7 node is ready to use!!!

Press any key to continue...

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

### Binary Search Tree Operation ###

Press 1 - Creation of BST

2 - Traversal In Inorder

3 - Traversal In Preorder

4 - Traversal In Postorder

5 - Exit

Enter Your Choice : 2

BST Traversal in INOREDER

0123569
Press any key to continue...

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

### Binary Search Tree Operation ###

Press 1 - Creation of BST

2 - Traversal In Inorder

3 - Traversal In Preorder

4 - Traversal In Postorder

5 - Exit

Enter Your Choice : 3

BST Traversal in PREOREDER

2105396

Press any key to continue...

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

### Binary Search Tree Operation ###

Press 1 - Creation of BST

2 - Traversal In Inorder

3 - Traversal In Preorder

4 - Traversal In Postorder

5 - Exit

Enter Your Choice : 4

BST Traversal in POSTOREDER

0136952
Press any key to continue...

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

### Binary Search Tree Operation ###

Press 1 - Creation of BST

2 - Traversal In Inorder

3 - Traversal In Preorder

4 - Traversal In Postorder

5 - Exit

Enter Your Choice : 5

Terminating

Press any key to continue...

***************************************************************************

LAB14 . 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>

createheap(int[],int);

heapsort(int[],int);

void main()

{
int k[10],i,n;

clrscr();

printf("\n Enter how many elements:");

scanf("%d",&n);

printf("\n enter array elements \n");

for(i=1;i<=n;i++)

scanf("%d",&k[i]);

printf("\n Original list \n");

for(i=1;i<=n;i++)

printf("%4d",k[i]);

heapsort(k,n);

printf("\n sorted list \n");

for(i=1;i<=n;i++)

printf("%4d",k[i]);

getch();

createheap(int k[],int n)

int temp,q,i,j,key;

for(q=2;q<=n;q++)

i=q;

key=k[q];

j=i/2;

while((i>1)&&(key>k[j]))
{

temp=k[j];

k[j]=k[i];

k[i]=temp;

i=j;

j=i/2;

if(j<1)

j=1;

k[i]=key;

heapsort(int k[],int n)

int temp,q,i,j,key;

createheap(k,n);

for(q=n;q>=2;q--)

temp=k[q];

k[q]=k[1];

k[1]=temp;

i=1;

j=2;

key=k[1];

if((j+1)<q)
{

if(k[j+1]>k[j])

j++;

while((j<=(q-1))&&(k[j]>key))

temp=k[j];

k[j]=k[i];

k[i]=temp;

i=j;

j=2*i;

if(j+1<q)

if(k[j+1]>k[j])

j++;

else

if(j>n)

j=n;

k[i]=key;

input & output


Enter how many Elemnts : 8

Enter array elements

9,16,32,8,4,1,5,8,0

original list

9 16 32 8 4 1 5 8 0

sorted list

0 1 4 5 8 8 9 16 32

You might also like