5. Data Structure
• Data structure defines a particular way to organize a data in a computer so
that it can be used efficiently.
• Data structure are a containers which hold a data.
• Efficient mean take minimum time and minimum memory space.
• Data structures are used in almost every program
6. Application of Data Structure
• Data structures are widely applied in the following areas:
• Compiler design
• Operating system
• DBMS
• Artificial intelligence
7. Classification of Data Structure
• Data structure are normally divided into two broad categories:
• Primitive Data Structure
• Non-Primitive Data Structure
8. Classification of Data Structure
Data structure
Primitive DS Non-Primitive DS
Integer Float Character PointerFloatInteger Float
9. Classification of Data Structure
Non-Primitive DS
Linear Non-Linear
Array
Link List Stack
Queue Graph Trees
10. Primitive Data Structure
• Primitive data structures are the fundamental data types which are supported
by a programming language. Some basic data types are integer, real, character,
float. The terms ‘data type’, ‘basic data type’, and ‘primitive data type’ are
often used interchangeably
11. Non Primitive Data Structure
• 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 classified into two categories: linear and non-linear data structures.
12. Linear Data Structure
• If the elements of a data structure are stored in a linear or sequential order,
then it is a linear data structure. Examples include arrays, linked lists, stacks,
and queues
13. Non Linear Data Structure
• However, if the elements of a data structure are not stored in a sequential
order, then it is a non-linear data structure. Examples include trees and
graphs.
14. Operation on Data Structure
• The data in data structure is processed using certain operations. Some commonly used
operations performed on data structures are
• Inserting: adding new data items into a data structure is called inserting
• Deleting: Removing data items from a data structures is called deleting.
• Searching : Find specific data items in a data structure is called searching
• Traversing: Accessing each item in a data structure exactly once for processing is called
traversing.
• Sorting: Arranging data items in a data structure into a specific order is called sorting
• Merging: Combining two list of data items into a single data list is called merging.
15. • The operation through which data structure is created is called the creation
operation.
• For example, to create a variable of integer type in C++, the statement is
written as :
• Int a, b;
• The above statement creates a memory space of two bytes for each variable a
& b.
16. Algorithm
• The step by step procedure to solve a particular problem is called algorithm.
• For example
• A recipe for cooking a cake is an algorithm
• Program=algorithm + Data Structure
17. How to write Algorithm?
• Name of algorithm : Every Algorithm is given a name that represents the
purpose of the algorithm. For example: An algorithm to find the sum of two
numbers is given the name “’sum’’.
• Introductory comment: The algorithm name is followed by a brief
description of the tasks that the algorithm performs.
• Steps: The algorithm consists of a sequence of numbered steps. Each step
described one task to be performed.
18. • Comments: Comments are used to explain purpose of a step or statement. These
may be given at the end of each statement or at the beginning of the step. In C++
the comments are given starting with double slash(//). In algorithms , these are
written between square brackets [ ].
• Variable Name: A variable is an entity that possesses a value. The name of the
variable is chosen according to the name of value it is to hold. For example, a
variable that is used to hold the maximum value is named as Max. Usually , variable
names in algorithm are written in capital letters. A variable name consists of letters,
numeric digits and some special characters. It always begins with a letter. Blank
spaces are not allowed within variable names.
19. • Operators: Arithmetic (+,-,*,/), Relational(<,>,>=,<=, etc.) and logical (AND,
OR, NOT) operators are used to describe various mathematical operations.
• Assignment Statement: The assignment statement is used to evaluate an
expression and assign the calculated value to the variable. The assignment operator
‘=’ sign is used for this purpose. For example evaluate the expression N+M assign
its value to variable S. the assignment statement is written as S= N+M
20. Input Statement
• Input or read statement is used in algorithms to enter data into variable. The
statement is written as:
Input variable-Name
For example , to input a value into variable N, the input statement is written
as:
Input N
21. Output Statement
• To Print a message or contents of a variable, PRINT statement is used with
the following format.
PRINT message or variable name
The message is written within double quotes. To print the contents of variable ,
its is written without double quotes. In C++, usually the ‘cin’ object is used
to get input from the keyboard and ‘cout’ object is used to print the output
on the screen.
22. Algorithm example
Write an algorithm to get two numbers and to calculate their sum
• STEP 1 : START
• STEP 2 : INPUT FIRST NUMBER N
• STEP 3 : INPUT SECOND NUMBER M
• STEP 4 : ADD N AND M AND STORE RESULT IN S i.e S=N+M
• STEP 5 : PRINT S
• STEP 6 : STOP
Algorithm-Sum
23. Selection statement
• Selection statement are used for making decisions. The decision is made by testing a
given condition. After testing a condition, a statement or set of statements are
executed or ignored. The structure that implements this logic in a programming
language is called conditional structure. A conditional statement is represented by
the IF structure. It has one of the following two forms:
• 1- IF condition THEN
statement (s)
END IF
25. Selection Statement Example
Write an algorithm to find the greater of the two given numbers
• STEP 1 : START
• STEP 2 : INPUT TWO NUMBER A AND B
• STEP 3 : IF A>B THEN
• STEP 4: PRINT “A IS GREATER”
• STEP 5 : ELSE
• STEP 6 : PRINT “B IS GREATER”
• STEP 7: EXIT
Algorithm-Greater number
26. Looping Statement
• The looping statement are used to execute certain statement(s) repeatedly.
The statement that are executed repeatedly are called body of the loop. In
algorithm ‘Repeat’ statement is used to execute the statement repeatedly. The
Repeat statement has two different forms
• Repeat for loop
• Repeat while loop
27. Repeat For Loop
• Repeat for loop is used to execute statements for a specified number of
times. This loop is also called the counter loop. Its general format is :
• Repeat FOR index= I-value TO F-value By S-value
• Where
• I-value represent the initial value for index variable
• F-value represent the final value
• S-value represent the step value i.e. Increment/ decrement value.
28. Repeat For Example
Write an algorithm to print first ten numbers using REPEAT FOR
strucute
• STEP 1: START
• STEP 2: REPEAT STEP 3 FOR C=1 TO 10 BY 1
• STEP 3: PRINT C [END OF STEP 2 LOOP]
• STEP 4: EXIT
Algorithm-Natural Numbers
29. Repeat while Structure
• This loop structure is used to execute a statement or a set of statements
repeatedly until the given condition remains true. It is also referred to as
conditional loop. Its general syntax is
• REPEAT WHILE ( condition)
Body of the loop
[end of loop]
30. Repeat While Example
Write an algorithm to print first ten numbers using REPEAT WHILE strucute
• STEP 1: START
• STEP 2: C=1
• STEP 3: REPEAT STEP 4 TO 5 WHILE (C<=10)
• STEP 4: PRINT C
• STEP 5 C=C+1 [END OF STEP-3 LOOP]
• STEP 6: EXIT
Algorithm-Natural Numbers
31. Simple/Structure data types
• So far, we have seen only simple data types, such as int, float, and
char.
• Simple variables can hold only one value at any time during program
execution, although that value may change.
• A Structure data type is a data type that can hold multiple values at
the same time. (Synonyms: complex data type, composite data
type)
• The array is one kind of structure data types.
32. Arrays
• An array is a group of related elements that all have the same
name and the same data type.
• Arrays are static in that they remain the same size throughout
program execution.
• An array’s data items are stored contiguously in memory.
• Each of the data items is known as an element of the array.
Each element can be accessed individually.
33. Array Declaration and Initialization
int numbers[ 5 ] ;
• The name of this array is “numbers”.
• Initializing an array may be done with an array
initialization, as in :
int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;
5 2 6 9 3numbers
34. Accessing Array Elements
• Each element in an array has a subscript (index) associated with it.
• Subscripts are integers and always begin at zero.
• Values of individual elements can be accessed by indexing into the
array. For example,
numbers[ 2 ]
would give the output
The third element = 6.
5 2 6 9 3numbers
0 1 2 3 4
35. Types of the array
• One-dimensional Array
• Two dimensional Array
36. One Dimensional Array
• When the consecutive homogeneous data is stored in the form of single row
or column then it is called one dimensional array.
37. Two dimensional array
• When data is stored in both rows and Coloums then its is called two
dimensional array. It is also called matrices.
38. How to declare a two dimensional
array?
A two dimensional array:
int arr[2][3];
This array has total 2*3 = 6 elements
Initialization:
We can initialize the array
int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22};
41. Traversal Algorithm
Algorithm-Traverse (LB, UB, A)
Introductory comment
Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses
A applying an operation process to each element of A.
Step no 1. Start
Step no 2. Repeat steps 3 For I=LB to UB by 1
Step no 3. Print A[I]
[End of step 2 loop]
Step no 4. Exit
42. Insertion Operation
• Insertion operation is used to insert a new element at specific position in to
one dimensional array.
In order to insert a new element into one dimensional array we have to create
space for new element.
Suppose there are N elements in an array and we want to insert a new
element between first and second element. We have to move last N-1
elements down in order to create space for the new element.
43. Insertion of element at the end of the
array
• Algorithm-Insert(A,N, Item)
• Introductory comment:
• This algorithm is used to insert an element at the end of the array where
• A=array name N=total element item= the element which is to be inserted
• Step no1. Set A[N+1]=item
• Step no2. Set N=N+1
• Step no3. stop
44. Insertion algorithm at middle
Algorithm-Insertion(A, N,K, ITEM)
Introductory Comment
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N.
Following is the algorithm where ITEM is inserted into the Kth
position of LA.
45. Algorithm
Step no1. Start
Step no2. [Initialize counter] Set J=K
Step no 3. Repeat steps 4 and 5 while N >=J
Step no4. [Move the jth element downward]Set LA[N+ 1]= LA[N]
Step no5. [decrease counter]Set N= N-1
[end of step 3 loop]
Step no 6. [Insert Item] set LA[K]=ITEM
Step no 7. Stop
46. Deletion operation in array
• Deletion is the operation that removes an element from a given location of
the array
• Delete an element at the end of array
• Delete an element from specific location
47. Delete a specific element from the array
• Algorithm-Delete
• Introductory comment
• Consider LA is a linear array with N elements and K is a positive integer
such that K<=N. Following is the algorithm to delete an element available
at the Kth
position of LA.
48. • Step no1. set J=k
• Step no2. Repeat Step no 3 while J<=N-1
• Step no3. Set A[j]=A[j+1] [end of loop]
• Step no4 Set J=J+1
• Step no4. Set N=N-1[reset the number of element in array]
• Step no5. Exit