0% found this document useful (0 votes)
11 views42 pages

Data STR Pratical File

The document is a practical file for a BSc IT course at Shri Guru Ram Rai University, containing various C programming exercises. It includes programs for array operations, matrix manipulations, string handling, searching algorithms, sorting algorithms, and data structures like queues and stacks. Each program is presented with code, comments, and expected outputs.

Uploaded by

dccreator9068
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)
11 views42 pages

Data STR Pratical File

The document is a practical file for a BSc IT course at Shri Guru Ram Rai University, containing various C programming exercises. It includes programs for array operations, matrix manipulations, string handling, searching algorithms, sorting algorithms, and data structures like queues and stacks. Each program is presented with code, comments, and expected outputs.

Uploaded by

dccreator9068
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/ 42

Shri Guru Ram Rai University

(Estd.By Govt,of Uttarakhand,vide Shri Guru Ram Rai Univarsity Act No.03 of 2017)

Patel Nagar,Dehradun-248001,Uttarakhand

Shri Guru Ram Rai CollegeCA & IT


“PRACTICAL FILE”
DATA STRUCTURE USING C

SUBMITTED BY SUBMITTED TO
DEEPAK SINGH Mr.Vaibhav sharma
COURSE:BSC.IT(IInd sem) (Asst.professor)

1|Page
1. ‘C’ Program to find sum of all elements in an array.

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a[20],i,n,sum=0;

printf("enter the size of array=");

scanf("%d",&n);

printf("enter elements in array=");

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

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

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

sum+=a[i];

printf("sum of array is: %d",sum);

getch();

}
2|Page
2. ‘ C’ Program to multiply two matrices with the help of 2D arrays.

#include<stdio.h>

#include<conio.h>

#define r1 3

#define c1 3

#define r2 3

#define c2 3

void main()

int a[r1][c1],b[r2][c2],c[r1][c2];

int i,j,k;

clrscr();

printf("enter value for 1 matrix:");

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

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

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

printf("enter value for 2 matrix:");

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

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

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

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

3|Page
{

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

c[i][j]=0;

for(k=0;k<c1;k++)

c[i][j]+=a[i][k]*b[k][j];

printf("matrix is\n");

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

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

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

printf("\n");

getch();

4|Page
OUTPUT:

5|Page
3. ‘C’ Program to find transpose of a matrix

#include<stdio.h>

#include<conio.h>

void main()

int a[10][10],b[10][10];

int r,c,i,j;

clrscr();

printf("enter the no of row &coloum\n");

scanf("%d%d",&r,&c);

printf("enter the elements\n");

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

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

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

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

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

b[j][i]=a[i][j];

printf("transpose of matrix:\n");

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

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

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

printf("\n");

getch();

6|Page
OUTPUT

7|Page
4. ‘C’ Program to find reverse of a string.

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

char a[20];

int i,len;

clrscr();

printf("enter the string=");

gets(a);

len=strlen(a);

for(i=len;i>=0;i--)

if(a[i]==' ')

a[i]='\0';

printf("%s\t",&(a[i])+1);

printf("%s\n",a);

getch();

8|Page
OUTPUT

9|Page
5. To search a number in an array using Linear Search Method

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a[10]={6,1,2,5,8,10,12,13,16};

int item,i;

printf("enter the item");

scanf("%d",&item);

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

if(item==a[i])

break;

if(i<9)

printf("item found on %d position",i+1);

else

printf("item not found");

getch();

OUTPUT

10 | P a g e
6. ‘C’ Program to count total number of vowel and consonants in a string.

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

int i=0,vowels=0,consonants=0;

char a[100];

clrscr();

printf("enter the string=");

gets(a);

while(a[i]!='\0')

if(a[i]=='a'|| a[i]=='A'||

a[i]=='e'||a[i]=='E'||

a[i]=='i'||a[i]=='I'||

a[i]=='o'||a[i]=='O'||

a[i]=='u'||a[i]=='U')

vowels++;

else

consonants++;

i++;

printf("\n no of vowels:%d",vowels);

11 | P a g e
printf("\n no of consonants:%d",consonants);

getch();

OUTPUT:

12 | P a g e
7. Program to search a number using Binary Search Method.

#include<stdio.h>

#include<conio.h>

void main()

int n,a[100],i,high=5-1,low=0,mid,ele;

clrscr();

printf("enter the size of array=");

scanf("%d",&n);

printf("enter the element in array=");

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

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

printf("enter the number to be searched:");

scanf("%d",&ele);

while(low<=high)

mid=(low+high)/2;

if(a[mid]<ele)

low=mid+1;

if(a[mid]==ele)

printf("element %d found at %d",a[mid],mid+1);

13 | P a g e
break;

if(ele<a[mid])

high=mid-1;

getch();

Output:

14 | P a g e
8. Program of sorting using bubble sort

#include<stdio.h>

#include<conio.h>

void main()

int a[100],i,j,temp,n;

clrscr();

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

scanf("%d",&n);

printf("enter the data=");

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

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

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

for(j=0;j<n-i-1;j++)

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

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

15 | P a g e
printf("sorted array is:\n");

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

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

printf("\n");

getch();

16 | P a g e
9. Program to sort an array using selection sort

#include<stdio.h>

#include<conio.h>

void main()

int a[100],i,j,min,temp,n;

clrscr();

printf("enter the size of the array=");

scanf("%d",&n);

printf("enter the data");

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

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

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

min=1;

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

if(a[j]<a[min]);

min=j;

temp=a[min];

a[min]=a[i];

a[i]=temp;

17 | P a g e
printf("sorted array is\n");

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

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

getch();

18 | P a g e
10. Program to sort an array using insertion sort

#include<stdio.h>

#include<conio.h>

void main()

int a[100],i,j,n,temp;

clrscr();

printf("enter the size of array=");

scanf("%d",&n);

printf("--> enter elements in the array=",n);

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

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

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

j=i;

while(j> 0 && a[j-1]>a[j])

temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

j--;

printf("after sorting\n");

19 | P a g e
for(i=0;i<=n-1;i++)

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

getch();

20 | P a g e
11. Program to sort an array using quick sort.

#include<stdio.h>

#include<conio.h>

void quicksort(int a[10],int low,int high);

void main()

int a[100],i,j,n;

clrscr();

printf("enter the size of the array=");

scanf("%d",&n);

printf("enter the element=");

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

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

quicksort(a,0,n-1);

printf("sorted array\n");

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

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

getch();

void quicksort(int a[10],int low,int high)

int i,j,p,t;

21 | P a g e
if(low<high)

p=low;

i=low;

j=high;

while(i<j)

while(a[i]<=a[p]&&i<high)

i++;

while(a[j]>a[p])

j--;

if(i<j)

t=a[i];

a[i]=a[j];

a[j]=t;

t=a[p];

a[p]=a[j];

a[j]=t;

quicksort(a,low,j-1);

22 | P a g e
quicksort(a,j+1,high);

}}

OUTPUT:

23 | P a g e
12. Program to sort an array using merge sort.

#include<conio.h>

#include<stdio.h>

void mergesort(int,int);

void merge(int,int,int);

int a[100];

void main()

int i,n;

clrscr();

printf("Enter the size of the array:");

scanf("%d",&n);

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

printf("Enter the element %d : ",i+1);

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

mergesort(0,n-1);

printf("Sorted array\n");

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

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

getch();

void mergesort(int low,int high)

24 | P a g e
{

int mid;

if(low!=high)

mid=(low+high)/2;

mergesort(low,mid);

mergesort(mid+1,high);

merge(low,mid,high);

void merge(int low,int mid,int high)

int b[100];

int h=low;

int j=mid+1;

int i=low;

while((h<=mid)&&(j<=high))

if(a[h]<=a[j])

b[i++]=a[h++];

else

b[i++]=a[j++];

while(h<=mid)

b[i++]=a[h++];

while(j<=high)

25 | P a g e
b[i++]=a[j++];

for(h=low;h<=high;h++)

a[h]=b[h];

OUTPUT:

26 | P a g e
13. Program in C to implement various operations on Circular queue using array

#include<stdio.h>

#include<conio.h>

#define SIZE 5

void enQueue(int);

void deQueue();

void display();

int cQueue[SIZE], front = -1, rear = -1;

void main()

int choice, value;

clrscr();

while(1){

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

printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1: printf("\nEnter the value to be insert: ");

scanf("%d",&value);

enQueue(value);

break;

case 2: deQueue();

break;

case 3: display();

break;

27 | P a g e
case 4: exit(0);

default: printf("\nPlease select the correct choice!!!\n");

void enQueue(int value)

if((front == 0 && rear == SIZE - 1) || (front == rear+1))

printf("\nCircular Queue is Full! Insertion not possible!!!\n");

else{

if(rear == SIZE-1 && front != 0)

rear = -1;

cQueue[++rear] = value;

printf("\nInsertion Success!!!\n");

if(front == -1)

front = 0;

void deQueue()

if(front == -1 && rear == -1)

printf("\nCircular Queue is Empty! Deletion is not possible!!!\n");

else{

printf("\nDeleted element : %d\n",cQueue[front++]);

if(front == SIZE)

front = 0;

28 | P a g e
if(front-1 == rear)

front = rear = -1;

void display()

if(front == -1)

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

else{

int i = front;

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

if(front <= rear){

while(i <= rear)

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

else{

while(i <= SIZE - 1)

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

i = 0;

while(i <= rear)

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

29 | P a g e
OUTPUT:

30 | P a g e
14. C Program to implement stack operation using linked list

#include<stdio.h>
#include<conio.h>
void insert();
void delet();
void display();
struct node
{
int d;
struct node *l;
};
struct node *top;
void main()
{
int c;
clrscr();
do{
printf("\nEnter choice:\n1>insert \n2>delete \n3>display \n4>Exit");
scanf("%d",&c);
switch(c)
{
case 1:
insert();
break;
case 2: delet();
break;
case 3: display();
break;

}
}while(c<=3);
getch();
}
void insert()
{
struct node* temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data");
scanf("%d",&temp->d);
temp->l=top;
top=temp;
}
void delet()
{
int i=1;
struct node* temp;

31 | P a g e
temp=top;
if(temp==NULL){
printf("\n The list is empty");
}
else {
printf("Deleted data is %d",temp->d);
top=temp->l;
}
}
void display()
{
struct node* temp;
temp=top;
if(temp==NULL)
{
printf("\n Empty linked list");
}
else{
while(temp!=NULL)
{
printf(" %d ",temp->d);
temp=temp->l;
}
}
}

OUTPUT:

32 | P a g e
15. Program in C to implement various operations on Linear queue using Linked List

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
struct node{
int d;
struct node *l;
};
struct node *f,*r;
void insert();
void delet();
void display();
void main()
{
int c;
clrscr();
do{
printf("\nEnter a choice \n1-->insert \n2-->delete \n3-->display \n4-->Exit");
scanf("%d",&c);
switch(c)
{
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
break;
}
}while(c<=3);
if(c==4)
{
printf("\nDhanyavaad ;) ");
}
if(c>4)
{
printf("\nInvalid Choice ");
}
getch();
}
void insert()
{
struct node* temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data");
scanf("%d",&temp->d);
temp->l=NULL;

33 | P a g e
if(f==NULL)
{
f=r=temp;
}
else{
r->l=temp;
r=temp;
}
}
void delet()
{
struct node* temp;
if(f==NULL)
{
printf("\nList is empty");
}
else{
temp=f;
printf("\nDeleted element is %d ",temp->d);
f=temp->l;
free(temp);
}
}
void display()
{
if(f==NULL)
{
printf("\nList is empty");
}
else{
struct node *temp;
temp=f;
while(temp!=NULL)
{
printf(" %d ",temp->d);
temp=temp->l;
}}}

34 | P a g e
OUTPUT:

35 | P a g e
16. Program to create Binary Search Tree

#include<stdio.h>
#include<stdlib.h>

typedef struct BST


{
int data;
struct BST *left;
struct BST *right;
}node;

node *create();
void insert(node *,node *);
void preorder(node *);

int main()
{
char ch;
node *root=NULL,*temp;

do
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);

printf("nDo you want to enter more(y/n)?");


getchar();
scanf("%c",&ch);
}while(ch=='y'|ch=='Y');

printf("nPreorder Traversal: ");


preorder(root);
return 0;
}

node *create()
{
node *temp;
printf("nEnter data:");
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->left=temp->right=NULL;
return temp;

36 | P a g e
}

void insert(node *root,node *temp)


{
if(temp->data<root->data)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}

if(temp->data>root->data)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}

void preorder(node *root)


{
if(root!=NULL)
{
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
}
Output

37 | P a g e
17 program in C to implement various operations on Circular queue using Linked List.
#include<stdio.h>

#include<conio.h>

struct node

int d;

struct node *l;

};

struct node *r=NULL;

void insert();

void delet();

void display();

void main()

int c;

clrscr();

do{

printf("\nEnter your choice \n1--->insert\n2-->delete\n1->display");

scanf("%d",&c);

switch(c)

case 1: insert();

break;

case 2: delet();

break;

case 3: display();

38 | P a g e
break;

}while(c<=3);

if(c==4)

printf("\n Dhanyavaad ");

if(c>4)

printf("\nGalat dalra pagle upar choice dekh");

getch();

void insert()

struct node *temp;

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

printf("\nEnter data");

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

temp->l=NULL;

if(r==NULL)

r=temp;

temp->l=r;

else

39 | P a g e
{

temp->l=r->l;

r->l=temp;

r=temp;

void delet()

struct node *temp;

if(r==NULL)

printf("Empty");

else if(r->l==r)

temp=r;

r=NULL;

printf("\nDeleted is %d",temp->d);

free(temp);

else

temp=r->l;

r->l=temp->l;

printf("\nDeleted is %d ",temp->d);

free(temp);

40 | P a g e
}

void display()

if(r==NULL)

printf("\nEmpty");

else{

struct node * q;

q=r->l;

printf(" \nElements are ");

while(q!=r)

printf(" %d ",q->d);

q=q->l;

printf(" %d ",r->d);

41 | P a g e
OUTPUT:

42 | P a g e

You might also like