Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DATA STRUCTURES

USING
C
(PRACTICAL FILE)

Govind Ballabh Pant Govt. Engineering College,


Okhla, New Delhi -110020

Submitted To:

Submitted By:

Ms. P. Sujitha

Aditya Kumar

Asst. Prof.(C.S.E.)

03020902813
3RD SEM.

1 | Page

INDEX
S.NO.

EXPERIMENT NAME

DATE

REMARKS

SIGN.

EXPERIMENT NUMBER 01
AIM: - Perform Linear search and Binary search on an array.
2 | Page

Code: #include <conio.h>


#include<stdio.h>
void main(){
int ch;
int lb,ub,mid,i,find,arr[10];
clrscr();
printf("enter choice");
printf("case 1-Linear search");
printf("case 2-Binary search");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the nos.\n");
for(i=0;i<10;i++){
scanf("%d",&arr[i]);}
printf("\n enter the no to be searched");
scanf("%d",&find);
for(i=0;i<10;i++) {
if(arr[i]==find)
printf("no found at %d",i+1);}
break;
case 2:
ub=9;
lb=0;
mid=(ub+lb)/2;
printf("enter elements\n");
for(i=lb;i<=ub;i++){
scanf("%d",&arr[i]);}
printf("Enter no to be searched\n");
scanf("%d",&find);
if(arr[mid]==find)
printf("no is found at %d",mid+1);
else if(arr[mid]>find){
ub=mid-1;
for(i=lb;i<=ub;i++) {
if(arr[i]==find)
printf("no is found at %d",i+1);
}
}
else
{
3 | Page

lb=mid+1;
for(i=lb;i<=ub;i++)
{
if(arr[i]==find)
printf("no is found at %d",i+1);
}
}
break;
}
getch();
}

EXPERIMENT NUMBER 02
AIM: - To perform the following function in an array
4 | Page

1. Insertion
2. Deletion
3. Sorting
Code:
#include<stdio.h>
#include<conio.h>
#includeSEARCH.h
void insert(int [],int);
void del(int [],int);
void sort(int[],int);
void main()
{
int ar[10],b,i,o;
clrscr();
printf("Enter the number of elements in the array - ");
scanf("%d",&o);
o=o-1;
printf("Enter the array\n");
for(i=0;i<=o;i++)
{
printf("Enter element %d - ",i+1);
scanf("%d",&ar[i]);
}
printf("What do you want to do next?\n1.Insert A value\n2.Delete A value\n3.Sort the array\n");
scanf("%d",&b);
switch(b)
{
case 1:
insert(ar,o);
break;
case 2:
del(ar,o);
break;
case 3:
o=o+1;
sort(ar,o);
break;
default:
printf("Wrong Input");}
getch();
}
void insert(int ar[10],int o)
5 | Page

{
int i,j,k;
printf("Enter the position - ");
scanf("%d",&j);
printf("Enter the Value - ");
scanf("%d",&k);
j=j-1;
for(i=o;i>=j-1;i--)
{
ar[i+1]=ar[i];
}
ar[j]=k;
o=o+1;
printf("The new array is ");
for(i=0;i<=o;i++)
{
printf("%d ",ar[i]);
}
}
void del(int ar[10],int o)
{
int i,j;
printf("Enter the position from where the element is to be deleted - ");
scanf("%d",&j);
j=j-1;
for(i=j;i<=o;i++)
{
ar[i]=ar[i+1];
}
o=o-1;
printf("The new array is ");
for(i=0;i<=o;i++)
{
printf("%d ",ar[i]);
}
}
void sort(int ar[10],int o)
{
int i,j,k,temp;
printf("\nUnsorted Data: ");
for(k=0;k<o;k++)
printf("%5d",ar[k]);
for(i=1;i<o;i++)
6 | Page

{
for(j=0;j<o-1;j++)
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
printf("\nAfter pass %d : ",i);
for(k=0;k<o;k++)
printf("%5d",ar[k]);
}
}

EXPERIMENT NUMBER 03
AIM: - To implement sparse matrix using array.
7 | Page

Code:
#include<stdio.h>
#include<conio.h>
struct matrix
{
int row,col,ele;
}s[10],t[10];
void main()
{
int ar[5][5],b,c,d=1,e=1,f,i,j,count=0;
clrscr();
printf("Enter the number of rows in matrix - ");
scanf("%d",&b);
b=b-1;
printf("Enter the number of columns in matrix - ");
scanf("%d",&c);
c=c-1;
printf("Enter the elements\n");
for(i=0;i<=b;i++)
{for(j=0;j<=c;j++){
printf("Enter element [%d][%d]",i+1,j+1);
scanf("%d",&ar[i][j]);
if(ar[i][j]!=0)
count++;}}
for(i=0;i<=b;i++){
printf("\n");
for(j=0;j<=c;j++){
printf("%d\t",ar[i][j]);}}
s[0].row=b;
s[0].col=c;
s[0].ele=count;
for(i=0;i<=b;i++){
for(j=0;j<=c;j++){
if(ar[i][j]!=0){
s[d].row=i;
s[d].col=j;
s[d].ele=ar[i][j];
d++;}}}
printf("\n");
t[0].row=c;
t[0].col=b;
t[0].ele=count;
for(i=0;i<=c;i++)
8 | Page

{for(j=0;j<=b;j++){
if(ar[i][j]!=0){
t[e].row=j;
t[e].col=i;
t[e].ele=ar[i][j];
e++;}}}
printf("1.To get three tupple matrix\n2.To get transpose matrix\n");
scanf("%d",&f);
switch(f)
{
case 1:
printf("ROW\tCOLUMN\tELEMENT\n");
for(i=0;i<=count;i++){
printf("%d\t",s[i].row+1);
printf("%d\t",s[i].col+1);
printf("%d\t",s[i].ele);
printf("\n");}
break;
case 2:
printf("ROW\tCOLUMN\tELEMENT\n");
for(i=0;i<=count;i++){
printf("%d\t",t[i].row+1);
printf("%d\t",t[i].col+1);
printf("%d\t",t[i].ele);
printf("\n");}
break;
default:
printf("Wrong Input");
}
getch();
}

9 | Page

10 | P a g e

You might also like