SlideShare a Scribd company logo
1 of 48
Data Structure and
Algorithm
Lecture No 1
Prepared by Muhammad Farhan
Text Book
• Text book
Data Structures and Algorithms By Mark Allen Weise
• Reference Book
Data structure in C++ by Muhammad Tauqeer Aikman series
Data vs. Information
Data
• A set of values have no
meaning
Information
• Meaningful processed data
Data vs. Information
• Data: 51007
• Information:
• 5/10/07 The date of your final exam.
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
Application of Data Structure
• Data structures are widely applied in the following areas:
• Compiler design
• Operating system
• DBMS
• Artificial intelligence
Classification of Data Structure
• Data structure are normally divided into two broad categories:
• Primitive Data Structure
• Non-Primitive Data Structure
Classification of Data Structure
Data structure
Primitive DS Non-Primitive DS
Integer Float Character PointerFloatInteger Float
Classification of Data Structure
Non-Primitive DS
Linear Non-Linear
Array
Link List Stack
Queue Graph Trees
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
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.
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
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.
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.
• 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.
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
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.
• 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.
• 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
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
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.
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
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
Cont…......
• 2- IF condition THEN
BLOCK A
ELSE
BLOCK B
END IF
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
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
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.
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
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]
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
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.
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.
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
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
Types of the array
• One-dimensional Array
• Two dimensional Array
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.
Two dimensional array
• When data is stored in both rows and Coloums then its is called two
dimensional array. It is also called matrices.
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};
Operation on Array Data Structures
• Traversal
• Insertion
• Deletion
Traversal
Traversal: To print all the element one by one in array.
 
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
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.
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
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.
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
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
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.
• 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

More Related Content

What's hot

Array in c language
Array in c languageArray in c language
Array in c language
home
 

What's hot (20)

queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Arrays
ArraysArrays
Arrays
 
Circular queue
Circular queueCircular queue
Circular queue
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
2D Array
2D Array 2D Array
2D Array
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Array in c++
Array in c++Array in c++
Array in c++
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
List and Dictionary in python
List and Dictionary in pythonList and Dictionary in python
List and Dictionary in python
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
single linked list
single linked listsingle linked list
single linked list
 

Similar to Data structure

ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
AlliVinay1
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
OnkarModhave
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 

Similar to Data structure (20)

ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptx
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptx
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptx
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Data structure

  • 1. Data Structure and Algorithm Lecture No 1 Prepared by Muhammad Farhan
  • 2. Text Book • Text book Data Structures and Algorithms By Mark Allen Weise • Reference Book Data structure in C++ by Muhammad Tauqeer Aikman series
  • 3. Data vs. Information Data • A set of values have no meaning Information • Meaningful processed data
  • 4. Data vs. Information • Data: 51007 • Information: • 5/10/07 The date of your final exam.
  • 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
  • 24. Cont…...... • 2- IF condition THEN BLOCK A ELSE BLOCK B 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};
  • 39. Operation on Array Data Structures • Traversal • Insertion • Deletion
  • 40. Traversal Traversal: To print all the element one by one in array.  
  • 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