Lab Manual
BCA 3rd Sem
Data structures- I
Nitin Bansal
A.P.
Department of Computer Science &
Applications
Introduction
The course consists of two laboratory classes per week.
The basic thrust of the course would be to learn programming language C and implementing
data structures.
To understand how various data structures work. To understand some important applictions of
various data structures. To familiarize how certain applications can benefit from the choice of
data structures. To understand how the choice of data structures can lead to efficient
implementations of algorithms.
P.D.M COLLEGE OF ENGINEERING
DATA STRUCTURE-I
BCA- 3rd SEM
LIST OF EXPERIMENTS
1. Program to insert an element in an array.
2. Program to delete an element from array.
3. Program to implement linear search.
4. Program to implement bubble sort.
5. program to implement binary search.
6. Program to implement matrix multiplication.
7. Program to implement linked list.
8. Program to implement insertion in Linked list.
9. Program to implement Deletion in Linked list.
10.
Program to implement searching in linked list.
11.
Program to implement sorting in linked list.
12.
Program to implement deletion in linked list.
13.
Program to implement stack using array with both
of its operation push and pop.
14.
Program to implement queue.
15.
Program to implement quick sort.
Data Structures
A data structure is a scheme for organizing data in the memory of a
computer. Some of the more commonly used data structures include lists,
arrays, stacks, queues, heaps, trees, and graphs.
The way in which the data is organized affects the performance of a program
for different tasks. Computer programmers decide which data structures to
use based on the nature of the data and the processes that need to be
performed on that data.
Many algorithms apply directly to a specific data structures. When working with certain data
structures you need to know how to insert new data, search for a specified item, and deleting a
specific item.
Commonly used algorithms include are useful for:
Searching for a particular data item (or record).
Sorting the data. There are many ways to sort data. Simple sorting, Advanced sorting
Iterating through all the items in a data structure. (Visiting each item in turn so as to
display it or perform some other action on these items) .
Characteristics of Data Structures
Data Structure
Array
Advantages
Quick inserts
Fast access if index known<
Disadvantages
Slow search
Slow deletes
Fixed size
Slow inserts
Ordered Array Faster search than unsorted array Slow deletes
Fixed size
Stack
Last-in, first-out access
Slow access to other items
Queue
First-in, first-out access
Slow access to other items
Linked List
Quick inserts
Quick deletes
Slow search
Binary Tree
Quick search
Quick inserts
Quick deletes
(If the tree remains balanced)
Deletion algorithm is complex
Heap
Quick inserts
Quick deletes
Access to largest item
Slow access to other items
Graph
Best models real-world
situations
Some algorithms are slow and very
complex
Efficiency of algorithms
It can be measured on two scales:
I.
II.
III.
Time : the amount of time neede to run to completion.
Space : The amount of memory space requires by algorithm during the
course of execution.
Space needed by program components:
Instruction space: to store the executable version of the program.
Data space : to store all constants and values.
Environmemt space : to store the information needed to resume the
suspended functions.
WRITE A PROGRAM IN C TO SEARCH IN
ARRAY AFTER INSERTING THE ELEMENTS
IN IT BY THE USER
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10];
clrscr();
printf("enter the elements of the array\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("elements entered are\n");
for(i=0;i<10;i++)
printf("a[%d]=%d\n",i,a[i]);
printf("enter the element to be searched\n");
scanf("%d",&j);
for(i=0;i<10;i++)
{
if(a[i]==j)
{
printf("success\nrequired data is found at position %d\n",i);
break;
}
if(i==9)
printf("failure\ndata not found\n");
}
getch();
}
OUTPUT
{WHEN DATA FOUND}
enter the elements of the array
11
22
33
44
55
66
77
88
99
56
elements entered are
a[0]=11
a[1]=22
a[2]=33
a[3]=44
a[4]=55
a[5]=66
a[6]=77
a[7]=88
a[8]=99
a[9]=56
enter the enter the element to be searched
44
success
required data is found at position 3
{WHEN DATA NOT FOUND}
enter the elements of the array
54
4
56
34
76
87
98
34
67
98
elements entered are
a[0]=54
a[1]=4
a[2]=56
a[3]=34
a[4]=76
a[5]=87
a[6]=98
a[7]=34
a[8]=67
a[9]=98
enter the element to be searched
44
failure
data not found
WRITE A PROGRAM IN C TO INSERT A
ELEMENT IN ARRAY AT DESIRED
POSITION
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,k,l,a[10];
clrscr();
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("list\n");
for(i=1;i<=n;i++)
printf("a[%d]=%d\n",i,a[i]);
printf("enter the position at which new element to be inserted\n");
scanf("%d",&k);
printf("enter the element to be inserted\n");
scanf("%d",&l);
j=n;
for(j=n;j>=k;j--)
{
a[j+1]=a[j];
}
a[k]=l;
n=n+1;
printf("new list after insertion is\n");
for(i=1;i<=n;i++)
printf("a[%d]=%d\n",i,a[i]);
getch();
}
OUTPUT
990
new list after insertion is
a[1]=11
a[2]=22
a[3]=33
a[4]=44
a[5]=990
a[6]=55
a[7]=66
a[8]=77
a[9]=88
write a program in c to delete an
element from desired position
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,k,j;
clrscr();
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("elements entered are\n");
for(i=1;i<=n;i++)
printf("a[%d]=%d\n",i,a[i]);
printf("enter the position of the element to be deleted\n\n");
scanf("%d",&k);
for(j=k;j<=(n-1);j++)
a[j]=a[j+1];
n--;
printf("new list\n");
for(i=1;i<=n;i++)
printf("a[%d]=%d\n",i,a[i]);
getch();
}
Output:
enter the size of array
8
enter the elements of array
2
3
4
5
6
7
8
9
elements entered are
a[1]=2
a[2]=3
a[3]=4
a[4]=5
a[5]=6
a[6]=7
a[7]=8
a[8]=9
enter the position of the element to be deleted
5
new list
a[1]=2
a[2]=3
a[3]=4
a[4]=5
a[5]=7
a[6]=8
a[7]=9
Write a program in c to sort a list using
bubble sort technique
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,ptr;
clrscr();
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=(n-1);i++)
{
ptr=1;
while(ptr<=(n-i))
{
if(a[ptr]>a[ptr+1])
{
a[ptr]=a[ptr]+a[ptr+1];
a[ptr+1]=a[ptr]-a[ptr+1];
a[ptr]=a[ptr]-a[ptr+1];
}
ptr++;
}
}
printf("new list\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}
Output:
enter the size of array
9
enter the elements of array
9
4
7
2
8
1
6
3
5
new list
1
2
3
4
5
6
7
8
9