Sei sulla pagina 1di 20

CE201.

02 Data Structure and Algorithms

Practical -1.1
Implement programs using Pointers, Dynamic Memory Allocation and Recursion.
Write a Program to find the smallest number out of 10 integer numbers using
Pointer. (Input size of array from user and allocate memory using DMA).
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p,min,i,n;
p=(int *)malloc(sizeof(int)*10);
printf("\n Enter 10 int numbers");
for(i=0;i<10;i++){
scanf("%d",&p[i]);
}
min=*p;
for(i=0;i<10;i++)
{
if(*(p+i)<min)
{
min=*(p+i);
}
}
printf("min=%d",min);
return 0;
}

OUTPUT:

ID: 15CE146

Page 1

CE201.02 Data Structure and Algorithms

Practical -1.2
Define a structure called Student that will describe the following information
Name , ID_Number, Address, PercentageUsing Student structure declare an
array student with Dynamic Memory Allocation and write a program to read
the information about all students and print all details of students whose
percentage is greater than 60.
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
int id;
char add[50];
int per;
};
int main()
{
struct student *s;
int i,n;
printf("\n Enter no. of students you want :");// clrscr();
scanf("%d",&n);
s=(struct student *)malloc(sizeof(struct student)*n);
for(i=0;i<n;i++)
{
printf("enter name:");
scanf("%s",&s[i].name);
printf("enter id:");
scanf("%d",&s[i].id);
printf("enter add:");
scanf("%s",&s[i].add);
printf("enter per:");
scanf("%d",&s[i].per);
printf("\n");
}
for(i=0;i<n;i++)
{
if(s[i].per>60)
{
ID: 15CE146

Page 2

CE201.02 Data Structure and Algorithms


printf("\nname: %s",s[i].name);
printf("\nid: %d",s[i].id);
printf("\nadd: %s",s[i].add);
printf("\npercentage: %d",s[i].per);
printf("\n");
}
}
}

OUTPUT:

ID: 15CE146

Page 3

CE201.02 Data Structure and Algorithms

Practical -1.3
Write a program to find mn using Recursive function.
#include<stdio.h>
int mul(int ,int );
int main()
{
int i,m,n;
printf("Enter 2 numbers for m*n :");
scanf("%d %d",&m,&n);
int result = mul(m,n);
printf("\n multiplication is %d",result);
}
int mul(int m,int n)
{
int f;
if(n==1)
{
return m;
}
else
{
f=m+mul(m,n-1);
return f;
}
}
OUTPUT:

ID: 15CE146

Page 4

CE201.02 Data Structure and Algorithms

Practical -1.4
Write a program to find (a+b) using Recursive function.
#include<stdio.h>
int sum(int ,int );
int main()
{
int i,a,b;
printf("Enter 2 numbers for a+b :");
scanf("%d %d",&a,&b);
int result = sum(a,b);
printf("\n sum is %d",result);
}
int sum(int a,int b)
{
int f;
if(b==0)
{
return a;
}
else
{
f=1+sum(a,b-1);
return f;
}
}
OUTPUT:

ID: 15CE146

Page 5

CE201.02 Data Structure and Algorithms

Practical -2.1
Input an array of N elements and search a number using Linear Search.
#include<stdio.h>
int main()
{
int *a,n,key,i,c;
printf("enter number of elements of array you want");
scanf("%d",&n);
a =(int *) malloc(sizeof(int)*n);
printf("\n enter array element :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter number you want to search");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i] == key)
{
c=0;
printf("\n element found at position %d",i);
break;
}
}
if(c!=0)
{ printf("\n element is not found");
}
}
OUTPUT:

ID: 15CE146

Page 6

CE201.02 Data Structure and Algorithms

Practical -2.2
Input an array of N elements in ascending order and search number using iterative
Binary Search.
#include<stdio.h>
int main()
{
int *a,n,key,low,high,mid,i,c=0;
printf("enter number of elements of array you want");
scanf("%d",&n);
a =(int *) malloc(sizeof(int)*n);
printf("\n enter array element :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter number you want to search");
scanf("%d",&key);
low = 0;
high = n-1;
while(low <= high)
{
mid = (low + high)/2;
if(key < a[mid])
{
high = mid -1;
}
else if(key >a[mid])
{
low = mid+1;
}
else if(key == a[mid])
{
ID: 15CE146

Page 7

CE201.02 Data Structure and Algorithms


c=1;a
break;
}
}
if(c==1)
printf("\n element found at position %d",mid);
else
printf("\n Element is not found");
return 0;
}
OUTPUT:

ID: 15CE146

Page 8

CE201.02 Data Structure and Algorithms

Practical -2.3
Use recursive Binary Search for question 2.2 .
#include<stdio.h>
#include<stdlib.h>
int c=0;
int binary_srch(int *,int ,int,int );
int main()
{
int *a,n,i,key,low,high,mid,result;
printf("Enter numbers you want to add in array");
scanf("%d",&n);
a=(int *)malloc(sizeof(int) * n);
printf("\n enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter element you want to search");
scanf("%d",&key);
printf("\n applying binary search .....");
low=0;
high=n;
result = binary_srch(a,key,low,high);
if(key == a[result])
{
printf("\n element found at position %d",result);
}
else{
printf("\n element not found ");}
return 0;
}
int binary_srch(int *a,int key,int low,int high)
{
int mid;
if(low<=high)
{
ID: 15CE146

Page 9

CE201.02 Data Structure and Algorithms


mid = (low + high)/2;
if(key<a[mid])
{
return binary_srch(a,key,low,high-1);
}
else if(key>a[mid])
{
return binary_srch(a,key,low+1,high);
}
else if(key == a[mid])
{
return mid;
}
}
}

OUTPUT:

ID: 15CE146

Page 10

CE201.02 Data Structure and Algorithms

Practical -3.1
BUBBLE SORT that arranges in ascending order.
#include<stdio.h>
#include<stdlib.h>
void swap(int *, int *);
void bubble_sort(int *,int );
int main()
{
int *a,n,i;
printf("Enter numbers you want to add in array");
scanf("%d",&n);
a=(int *)malloc(sizeof(int) * n);
printf("\n enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n APPLYING BUBBLE SORT..");
bubble_sort(a,n);
return 0;
}
void bubble_sort(int *a,int n)
{
int i,j,k,*p,*q,f;
for(i=0;i<n-1;i++)
{
f=0;
for(j=0;j<n-i-1;j++)
{
if(f=0) //STOPPING CONDITION,,
break;
else if(a[j] > a[j+1])
{
f=1;
swap(&(a[j]),&(a[j+1]));
}
ID: 15CE146

Page 11

CE201.02 Data Structure and Algorithms


}
}
printf("\n sorted array..\n ");
for(k=0;k<n;k++)
{
printf("\n%d",a[k]);
}
}
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a =*b;
*b =temp;
}

OUTPUT:

ID: 15CE146

Page 12

CE201.02 Data Structure and Algorithms

Practical -3.2
SELECTION SORT that arranges in descending order.
#include<stdio.h>
#include<stdlib.h>
void swap(int *, int *);
void selection_sort(int *,int );
int main()
{
int *a,n,i;
printf("Enter numbers you want to add in array");
scanf("%d",&n);
a=(int *)malloc(sizeof(int) * n);
printf("\n enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n APPLYING SELECTION SORT..");
selection_sort(a,n);
return 0;
}
void selection_sort(int a[],int n)
{
int min_index,i,j;
for(i=0;i<n;i++)
{
min_index = i;
for(j=i+1;j<n;j++)
{
if(a[j]>a[min_index])
{
min_index=j;
}
}

ID: 15CE146

Page 13

CE201.02 Data Structure and Algorithms


if(min_index != i)
{
swap(&(a[i]),&(a[min_index]));
}

}
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
}
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

OUTPUT:

ID: 15CE146

Page 14

CE201.02 Data Structure and Algorithms

Practical -3.3
INSERTION SORT that arranges in ascending order.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p,i,j,pivot,n,temp;
printf("enter size of an array:");
scanf("%d",&n);
p=(int *)malloc(sizeof(int)*n);
printf("enter elements in array:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(j=0;j<n;j++)
{
pivot=p[j];
i=j-1;
while(i>=0 && p[i]>pivot)
{
p[i+1]=p[i];
i--;
}
p[i+1]=pivot;
}
printf("sorted array...\n");
for(i=0;i<n;i++)
printf("\n%d",p[i]);
return 0;
}

ID: 15CE146

Page 15

CE201.02 Data Structure and Algorithms


OUTPUT:

ID: 15CE146

Page 16

CE201.02 Data Structure and Algorithms

Practical -4
Implement a program for STACK that performs following operations using array.
a) PUSH b) POP c) PEEP d) CHANGE e) DISPLAY
#include<stdio.h>
void push(int );
int pop();
int peep(int );
void change(int ,int );
void display();
int stack[5],top = -1;
int main()
{
int x,add,del,dis,m,n,f,g;
do
{
printf("\n press 1 for enter element in stack..");
printf("\n press 2 for delete element from stack..");
printf("\n press 3 for display element in array..");
printf("\n press 4 for display");
printf("\n press 5 for change");
printf("\n press 0 for exit");
scanf("%d",&x);
switch(x)
{
case 1:
scanf("%d",&add);
push(add);
break;
case 2:
scanf("%d",&del);
m=pop();
printf("\n item %d is deleted",m);
break;
case 3:
ID: 15CE146

Page 17

CE201.02 Data Structure and Algorithms


printf("\n enter position");
scanf("%d",&dis);
n=peep(dis);
printf("\n element is %d",n);
break;
case 4:
display();
break;
case 5:
printf("\n enter position of element for change");
scanf("%d",&f);
printf("\n enter new value");
scanf("%d",&g);
change(f,g);
break;
default:
printf("\n Invalid choice..");
}
}
while(x);
return 0;
}
int peep(int dis)
{
int i;
if(top == -1 || top >5)
{
printf("\n not display..");
}
else if(top == -1 || top >5)
{
printf("\n not display..");
}
else
{ //printf("\n %d",top);
i=top-dis+1;
return stack[i];
}
i=top-dis+1;
return stack[i];
ID: 15CE146

Page 18

CE201.02 Data Structure and Algorithms


}

void push(int n)
{
if(top > 5)
{
printf("\n stack overflow..");
}
else{
top++;
stack[top] = n;
}
}
int pop()
{
int i;
if(top <= -1)
{
printf("\n cannot pop element because stack is underflow..");
}
else
{
i= stack[top];
stack[top] = NULL;
top --;
return i;
}
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("\t %d",stack[i]);
}
}
void change(int f,int g)
ID: 15CE146

Page 19

CE201.02 Data Structure and Algorithms


{
int i;
if(top == -1 || top >5)
{
printf("\n not display..");
}
else
{
i=top-f+1;
stack[i] = g;
}
}

OUTPUT:

ID: 15CE146

Page 20

Potrebbero piacerti anche