Datastructure Unit 1 SKM
Datastructure Unit 1 SKM
18UCAM302
UNIT I
Introduction to Data Structures and Algorithms:
Basic Terminology - Classification of Data
Structures – Abstract Data Type - Time and
Space Complexity – Big O Notation.
Arrays: Introduction - Declaration of Arrays –
Accessing the Elements of an Array - Storing
Values in Arrays - Operations on Arrays - Two-
dimensional Arrays - Multi-dimensional Arrays.
Linked Lists: Introduction: Linked List Versus
Arrays – Memory Allocation and De-allocation
for a Linked List.
Basic Terminology
Our aim has been to design good programs,
where a good program is defined as a program
that
• runs correctly
• is easy to read and understand
• is easy to debug and
• is easy to modify.
Definition
A data structure is basically a group of data
elements that are put together under one name, and
which defines a particular way of storing and
organizing data in a computer so that it can be used
efficiently.
Data structures are widely applied in the following
areas:
• Compiler design
• Operating system
• Statistical analysis package
• DBMS
• Numerical analysis
• Simulation
• Artificial intelligence
• Graphics
Elementary Data Structure
Organization
Data structures are building blocks of a
program.
A program built using improper data
structures may not work as expected.
So as a programmer it is mandatory to
choose most appropriate data structures for a
program.
The term data means a value or set of values.
integer,
real,
character
boolean.
Non-primitive data structures
Non-primitive data structures are those data
structures which are created using primitive
data structures.
Examples of such data structures include
Linked lists.
Stacks
Trees, and
Graphs.
Non-primitive data structures can further be
Linear Structures
• By means of links
Non-linear data structure
if the elements of a data structure are not
stored in a sequential order, then it is a
non-linear data structure.
Definition :
A stack is a linear data structure in which
insertion and deletion of elements are done
at only one end, which is known as the top of
the stack.
Push
Pop
peep.
The push operation adds an element to the
top of the stack.
Variable part:
It varies from program to program. It includes
the space needed for recursion stack, and for
structured variables that are allocated space
dynamically during the runtime of a program.
BIG O NOTATION
The Big O notation, where O stands for ‘order of ’, is
concerned with what happens for very large values
of n.
If f(n) and g(n) are the functions defined on a
type name[size];
For example :
int marks[10];
ACCESSING THE ELEMENTS OF AN
ARRAY
order
Traversing an array
Traversing an array
upper_bound
Step 3: Apply Process to A[I]
Step 4: SET I=I+1
[END OF LOOP]
Step 5: EXIT
Write a program to read and display n numbers
using an array.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20];
clrscr();
printf("\n Enter the number of elements in
the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The array elements are 1 2 3 4 5
INSERTING AN ELEMENT IN AN ARRAY
Step 3: EXIT
Algorithm to Insert an Element in the
Middle of an Array
The algorithm INSERT will be declared as
INSERT (A, N, POS, VAL). The arguments are
(a) A, the array in which the element has to be
inserted
(b) N, the number of elements in the array
(c) POS, the position at which the element has
to be inserted
(d) VAL, the value that has to be inserted
Step 1: [INITIALIZATION] SETI=N
Step 2: Repeat Steps 3and4 while I>= POS
Step 3: SET A[I+1]=A[I]
Step 4: SET I=I–1
[END OF LOOP]
Step 5: SET N=N+1
Step 6: SET A[POS]=VAL
Step 7: EXIT
program to insert a number at a given location in an array.
int main()
clrscr();
scanf("%d", &n);
for(i=0;i<n; i++)
scanf("%d", &arr[i]);
}
printf("\n Enter the number to be inserted : ");
scanf("%d", &num);
printf("\n Enter the position at which the
number has to be added : ");
scanf("%d", &pos);
for(i=n–1;i>= pos; i––)
arr[i+1] = arr[i];
arr[pos] = num;
n = n+1;
printf("\n The array after insertion of %d is : ",
num);
for(i=0;i< n; i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the number to be inserted : 0
Enter the position at which the number has to be
added : 3
The array after insertion of 0 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 0
arr[4] = 4
arr[5] = 5
Deleting an element from an array
Deleting an element from an array means
removing a data element from an already
existing array.
column.
DECLARING TWO-DIMENSIONAL ARRAYS
A two-dimensional array is declared as:
data_type array_name[row_size][column_size];
int marks[3][5];
Two-dimensional array
There are two ways of storing a two-
dimensional array in the memory.
Solution
Address(A[I][J]) = Base_Address + w{N (I – 1) + (J – 1)}
Address(marks[18][4]) = 1000 + 2 {5(18 – 1) + (4 – 1)}
= 1000 + 2 {5(17) + 3}
= 1000 + 2 (88)
= 1000 + 176 = 1176
Initializing Two-dimensional arrays
A two-dimensional array is initialized in the
same way as a one-dimensional array is
initialized.
For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};