0% found this document useful (0 votes)
8 views34 pages

Data Structure Practical File

Uploaded by

ywhsgpbud
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)
8 views34 pages

Data Structure Practical File

Uploaded by

ywhsgpbud
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

JCD MEMORIAL (PG) COLLEGE

BARNALA ROAD, SIRSA, HRY-125055

PRACTICAL FILE
OF
DATA STRUCTURE

SUBMITTED TO: SUBMITTED BY:


MRS. SAARIKA NAME: MANOJ SHARMA
[Link] CLASS: BCA (1st ) YEAR
JCD MEMORIAL COLLEGE ROLL NO: 23063115470057
INDEX
[Link]. TITLE DATE Pg. NO. SIGN

1. IMPLEMENTATION OF INSERTING 1-2


AN ELEMENT IN ARRAY.
2. IMPLEMENTATION OF DELETING 3-4
AN ELEMENT IN AN ARRAY.
3. IMPLEMENTATION OF SELECTION 5-7
SORT.
4. IMPLEMENTATION OF BUBBLE 8-9
SORT.
5. IMPLEMENTATION OF INSERTION 10 - 11
SORT.
6. IMPLEMENTATION OF LINEAR 12 - 13
SEARCH.
7. IMPLEMENTATION OF BINARY 14 - 15
SEARCH.
8. IMPLEMENTATION OF QUICK 16 - 18
SORT.
9. IMPLEMENTATION OF MERGE 19 - 21
SORT.
10. IMPLEMENTATION OF LINKED 22 - 24
LIST.
11. IMPLEMENTATION OF STACK AS 25 - 28
AN ARRAY.
12. IMPLEMENTATION OF CIRCULAR 29 - 32
QUEUE.
PROGRAM-1

 IMPLEMENTATION OF INSERTING AN ELEMENT IN ARRAY.

#include<stdio.h>
int main() {
int arr[100], n, pos, val, i;
printf("Enter the number of elements in the
array:");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i= 0;i<n,i++)
scanf("%d", &arr[i]);
printf("Enter the position where you want to insert an element: ");/
scanf("%d", &pos);
printf("Enter the value you want to insert: ");
scanf("%d",&val);
for(i = n; i>= pos; i--)
arr[i] = arr[i-1]; }
arr[pos-1]= val;
n++;
printf("The array after insertion is: ");
for(i=0;i<n;i++) {
Printf(“%d”,arr[i]);
}
Return 0; }
OUTPUT
PROGRAM-2

 IMPLEMENTATION OF DELETING AN ELEMENT IN AN ARRAY.

#include <stdio.h>
int mainOl
int arrl100], n, pos, i;
printf("'Enter the number of elements in the
array: ");
scanf('%d", &n);
printf("Enter the elements of the array: ");
for(i = 0;i <n;it+){
scanf("%d", &arr[il);
}
printf("Enter the position of the element
you want to delete:");
scanf("%d", &pos);
for(i = pos-1; i< n-l;i++)
{
arrlil = arr[i+1];
}
n--;
printf("The array after deletion is:");
for(i = 0;i<n;i++){
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT
PROGRAM-3

 IMPLEMENTATION OF SELECTION SORT.

#include <stdio.h>
void selection sort(int arr[l, int n)
{
int i, j, min_idx;
for(i=0;i<n-l;i++)
{
min idx =I;
for0 =i+1:jcn j++)
{
if(arr[j] < arr[min_idx])
{
min_idx=j;
}
}
int temp = arr(min idx];
arr[min_idx]= arrlil;
arr[i] = temp;
}
}
int main()
{
int arr[100], n, i;
printf("Enter the number of elements in the array: ");
scanf("%d". &n);
printf (“Enter the elements of the array.");
for(i=0;i<n;i++)
{
Scanf(“'%d”, &arr[i]);
Selection_sort(arr,n);
printf("The sorted array is!");
for(i= 0;i<n;i++)
printf("%d", arr[i]);
}
return 0;
}
OUTPUT
PROGRAM-4

 IMPLEMENTATION OF BUBBLE SORT.

#include<stdio.h>
#include<conio.h>
int main() {
int a[40],n,I,j,temp;
printf(“Enter the number of elements:");
scanf("%d"&n);
printf(“Enter the array elements :\n");
for(i=1;i<=n;++i)
scanf("%d", &a[i]);
for(i=1;i<=n,i++)
{
For(j=1;j<=n-1;++j)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1l; alj+1]=temp;
}
}
}
printf("The sorted array is :\n");
for(i=1;i<=n;i++) printf("%d\n",a[i]);
return 0;
}
OUTPUT
PROGRAM-5

 IMPLEMENTATION OF INSERTION SORT.

#include<stdio.h>
#include<conio.h>
int main() {
int a[40],n,i,position,temp;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the array elements :\n");
for(i=l;i<=n;i++)
scanf(“%d”, &a[i]);
for(i=2:i<=n:i++) {
temp=a[i];
position=i-1;
while(temp<a[positionl]&& position>=1)
{
a[position+1]-alpositonl;
position=position-1;
}
alposition+1]=temp;
}
printf("The sorted array is :\n");
for(i=1;i<=n;i++)
printf("%d\n"alil);
return 0;
}
OUTPUT
PROGRAM-6

 IMPLEMENTATION OF LINEAR SEARCH.

#include<stdio.h>
#include<conio.h>
int main()
{
int al40],n.i,se,found=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the array elements :\n");
for(i=l;i<=nii++)
scanf("%d".&a[i])i
printf("Enter the element to be searched for:”);
scanf('%d" &se); i-l;
while(i<=n && found==0)
{
if(a[i]==se) found=l; i++;
if(found==1)
printf(“Element found at position :\n%d”,i-1);
else
printf(“Elements not found ”);
return 0;
}
OUTPUT
PROGRAM-7

 IMPLEMENTATION OF BINARY SEARCH.

#include<stdio.h>
#include<conio.h>
int main() {
int a[40],n,i,se,First,last,mid;
printf("'Enter the number of elements:");
scanf(“%d”, &n);
printf(“Enter the array elements :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("Enter the element to be searched for :”);
scanf("%d",&se); first=1;
last=n; mid=(first+last)/2;
while(first<last && a[mid]!=se) {
if(a(mid]<se)first=mid+1;
if(a[mid]>se)last=mid-1;
mid=(first+last)/2;
}
If(a[mid]==se)
Printf(“Element found at position:\n%d”, mid);
Else
printf(“Element not found”);
return 0;
}
OUTPUT
PROGRAM-8

 IMPLEMENTATION OF QUICK SORT.

#include<stdio.h>
#include<conio.h>
int quicksort(int number[30] int first,int last)
int i,j,pivot,temp;
if(first<last)
{
pivot=first;
i-first;
j=last;
while(i<j)
{
while(numberli]<=number[pivotl&&i<last) i++;
while(number[j]>number(pivotl) j--;
if(i<j)
{
temp-number[il;
numberli]-numbertjl;
numberli]=temp;
}
}
temp-number[pivot];
number [pivot]-numberlj];
numberll-temp;
quicksort(number,first.j-1);
quicksort(number,j+1,last);
}
}
int main()
{
int i,count,number[30];
printf("Enter the elements(max.-30):"):
scanf("%d",&count);
printf("Enter %d elements :n",count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("The sorted array is :\n");
for(i=0;i<count;i++)
printf("%d ",number[i]);
return 0;
}
OUTPUT
PROGRAM-9

 IMPLEMENTATION OF MERGE SORT.

#include<stdio.h>

#include<conio.h>

int divide(int a[ ],int first,int last);

int merge(int a[ ],int first,int mid,int last);

int main()

int a[40],n,i,o;

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

scanf("%d",&n);

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

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

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

printf("The sorted array is :\n");

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

return 0;

int divide(int a[],int first,int last)

int mid; if(first<last)

mid=(first+last)/2;

divide(a,first, mid);

divide(a,mid+1,last);

merge(a,first,mid last);

int merge(int al [Link] first,int mid,int last)


{

int I,j.k,temp[40];

i=first;

j=mid+1:

k=first;

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

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

templk]=a[i];i++;k++;

Else

temp[k]=a[j];j++;k++;

while(i<=mid)

temp[k]=a[i];k++;i++;

while(j<=last)

temp(kl=a[i];k++5j++;

for(i=first;i<=last;i++) a[i]=temp[i];

return 0;

}
OUTPUT
PROGRAM-10
 IMPLEMENTATION OF LINKED LIST.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
struct node *start=NULL, *ptr=NULL,
*new1=NULL; int insert()
{
New1= (struct node*) malloc(sizeof(struct node));
printf("Enter the info part:");
scanf("%d", &newl->info);
new1->next = NULL;
if(start == NULL)
{
start = new1;
return 0;
}
for(ptr = start; ptr->next != NULL; ptr = ptr->next);
ptr->next = newl; return 0;
}
int display()
{
if(start == NULL)
{
printf("List is empty);
return 0;
}
for(ptr = start; ptr !=NULL; ptr = ptr->next)
printf('%d\t', ptr->info);
return 0;
}
Int deletion()
{
If(start == NULL)
{
Printf(“list is empty”);
Return0 ;
}
Return 0;
]
Int main()
{
Insert();
Insert();
Display();
Return();
}
OUTPUT
PROGRAM-11

 IMPLEMENTATION OF STACK AS AN ARRAY.

#include <stdio.h>
#define MAX_SIZE 10
int stack(MAX_SIZEI;
int top =-1;
int isFull(){
return top == MAX_SIZE- 1;
}
int isEmpty(){
return top ==-1;
}
void push(int element){
if (isFull()) {
printf("'Stack Overflow\n")
return;
}
stack[++top] = element;
}
int pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return-1;
}
return stack[top--];
}
void display() {
int i;
if (isEmpty()) {
printf("Stack is empiy\n");
return;
}
printf("Stack elements are:\n");
for (i= top; i>= 0; i-)
printf(“ %d\n”, stackf[i]);
}
int main() {
int choice, element;
do {
printf("\n1. Push\n")
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter the element to be pushed:");
scanf("%d", &element);
push(element);
break;
case 2:
element = pop0);
if (element !=-1)
printf("Popped element is %d\n"
element);
break;
case 3:
display();
break;
case 4:
printf("Exiting from the program\n");
break;
default:
printf("Invalid choice\n");
break;
}
}
while (choice != 4);
return 0;
}
OUTPUT
PROGRAM-12

 IMPLEMENTATION OF CIRCULAR QUEUE.

#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Oueue is overflow..");
}
else
{ rear=(rear+1)%max;
Queue[rear]=element;
}
}
int dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d".queue[front]);
front=-1:
rear=-1;
}
Else
{
printf("\nThe dequeued element is %d",queuelfrontD);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1&& rear==-1){
printf("\n Oueue is empty..");
}
Else
{
printf("\nElements in a Oueue are :"):
while(i<=rear)
{
printf("%d.", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=l.x;
while(choice<4 && choice!=0)
{
printf("\nPress 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be
inserted :"); scanf("%d", &x);
enqueue(x);
break;
case 2.
Dequeue():
break;
case 3:
display0;
}
return 0;
}
OUTPUT

You might also like