Professional Documents
Culture Documents
Data Structure Chapter 1 PDF
Data Structure Chapter 1 PDF
Basic Terminologies:
Data: The collection of raw facts and figures is called data. Simply data are value or set of values. Data
can’t be used in decision making.
Data item: Data item refers to single unit values. For example name, roll-number marks etc.
Data item has two types:
Group data items: Data items that can be divided into sub data items are called group data items.
For example name is a group data item because it can be divided into First name, Middle name
and last name.
Elementary data item: Data item that can’t be divided into sub data item are called elementary
data item. For example roll number, which can’t be decomposed into further sub data items.
Field: A field is a single elementary data item. A single piece of information about an object is called
field.
Roll No Name Marks
For example , , are example of fields.
Record: A record is a collection of related fields. For example a record of STUDENT is:
Record Roll No Name D_O_B Marks Contact no
File: A file is a collection of related records about an object. When we combine more than one record
about an object then it form a file.
File Roll No Name D_O_B Marks Contact No Address
1 Ali 1992 745 034267xxxxx Drosh
2 Khan 1993 756 034566xxxxx Chitral
3 Anita 1999 845 033389xxxxx Chitral
Data Structure:
The representation / placement of data in computer memory is called data structure.
ORThe way in which data is stored in computer memory is called data structure.
ORThe organization of data in computer memory is called data structure.
By organization of data we mean that representing the data in such a way that the data is efficiently stored,
processed and retrieved.
Data structure is used in computer to organize the data in a specific way and to avoid memory wastage.
Types of data Structure:
There are following types of data structure:
Based on storage
Based on size
Based on storage:
Based on storage there are following types of data structure:
1. Linear data Structure: A type of data structure in which data is stored in consecutive memory locations
is called linear data structure. In linear data structure sequential / adjacent memory locations are used for
B C
D E
Based on Size:
Based on size there are following types of data structure:
1. Static data Structure: A type of data structure which has a fixed size is called Static data Structure. The
size of static data structure can’t grow or shrink according to need as it has a fixed predefined size. Static
data structures are very good for storing well defined number of data items.
Example: Array, Queue, Stack
2. Dynamic data Structure: A type of data structure which has no fixed size is called Dynamic data
Structure. The size of dynamic data structure can grows or shrinks according to the need. Dynamic data
structures are very good in situations where the numbers of items to be stored are not known in advance.
Example: Tree, Graph
Example#2:
Algorithm: AVERAGE
This algorithm is used to find the average of
three numbers.
Step#1: Read: A , B, C
Step#2: Average= (A+B+C) / 3
Step#3: Print: ‘The average is ’, Average
Step#4: Exit
Example#3:
Algorithm: ADD
This algorithm is used to find the sum of two
numbers.
Step#1: Read: A, B
Step#2: Sum= A+B
Step#3: Print: ‘The Sum is ’, Sum
Step#4: Exit
if Statement: if statement is used to check a single condition. If the condition is true a statement or set of
statement executes, otherwise not.
Syntax:
if(condition)
Statements;
End if
if else statement: if else statement is also used for making condition. In if-else, the condition is tested if true
then statements in first block are executed and if false then statements in second block [else block] are
executed.
Syntax:
if (condition)
Statements;
else
Statements;
End if
else if statement: else if statement is just like if else but the difference is that else if is used to check
multiple conditions.
Syntax:
if (condition)
Statements;
else if (condition)
Statements;
else if (condition)
Statements;
End if
for loop: for loop is a type of looping structure which is used when the number of execution of statement(s)
is known in advance. It executes a statement or set of statements again and again (repeatedly) up to n times,
where n is the end value.
Syntax:
Repeat for K=R to S by T
Statements;
[End of loop]
Where R is the initial value, S is the end value and T is the increment.
while loop: while loop is also a type of loop. It executes a statement or set of statements again and again
(repeatedly) until a given condition remains true.
Syntax:
Repeat while condition
Statements;
[End of loop]
Algorithm: FACTORIAL
This algorithm is used to find the factorial of a number.
Step#1: Set F=1, I=1
Step#2: Read: N
Step#3: Repeat Step#4 and Step#5 while (I<=N)
Step#4: F=F*I
Step#5: I=I+1;
[End of loop]
Step#6: Print: ‘the factorial of given number is:’ F
Step#7: Exit
Prepared By: Muhammad Fayaz
M.Sc Computer Science, SBBU.
Contact#: 0342-5850786
E-mail: fayazsbbu786@gmail.com