Dsa File
Dsa File
Dsa File
Practical file
on
Data structure & algorithm
(BCA 174)
SUBMITTED BY submitted to
LAgneshwar.s ms. juveria mam Enroll no.
112002021 (ASSISTANT PROFESSOR )
INDEX
1. Write a program to calculate factorial of a number
using functions.
2. Write a program to input an array of ten elements &
prints that array.
3. Write a program to read two matrices and then print
a matrix which is addition of these two matrices.
4. Write a program which reads two matrices and
multiply them.
5. Write a program to display a square matrix.
6. Write a program to find transpose of a matrix.
7. Write a program to read and write array elements
and find the sum of even and odd elements.
8. Write a program to insert an element in array.
9. Write a program to delete an element in an array.
10. Write a program to traverse an array.
11. Write a program to initialize and display array
elements using pointers.
12. Write a program to display the contents of a 2 D
array using pointer.
13. Write a program to accept a matrix from user and
find out matrix is sparse or not.
14. Write a program to sort elements of an array using
bubble sort.
15. Write a program to sort elements of an array using
selection sort.
16. Write a program to sort elements of an array using
insertion sort.
17. Write a program to sort elements of an array using
merge sort.
18. Write a program to search an element using linear
search.
19. Write a program to search an element using binary
search.
20. Write a program to implement singly linked list.
21. Write a program to implement doubly linked list.
22. Write a program to implement header linked list.
23. Write a program to demonstrate the operations
performed on stack.
24. Write a program to demonstrate the linked list
implementation of stack.
25. Write a program to convert infix expression to
postfix form.
26. Write a program to implement queue using array.
27. Write a program to illustrate the working of a
queue when implemented using pointers.
28. Write a program n to implement circular queue
using array.
29. Write a program to implement binary tree
traversals: Inorder, Preorder, Postorder.
30. Write a program to perform insertion, deletion and
traversal in Binary Search tree.
Answers of questions
{
printf("Enter a Number to Find Factorial: ");
printf("\nFactorial of a Given Number is: %d ",fact());
return 0;
}
int fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
return fact;
}
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each
other.\n");
else
{
printf("Enter the elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
return 0;
}
#include <stdio.h>
void main(){
int a[3][4]={{1, 2, 3, 4},{5,6,7,8},{9,10,11,12}};
int *ptr=&a[0][0];
int rows=3;
int col=4;
int total_cells= rows*col,i;
Case 8: exit(0);
}
}
While (choice!=8);
Getch();
}
Q21. Write a program to implement doubly linked list?
int count=0;
struct node
{
int data;
struct node* next;
}*start,*header;
void print()
{
struct node *p=start;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("\n");
}
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/
/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/
int del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
printf("Tree empty");
return 0;
}
find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return 0;
}
if(location->lchild==NULL && location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
break;
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/