You are on page 1of 7

B.

Sc Part-I Paper-B Section (B)


Data Structure

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.
ORThe way in which data is stored in computer memory is called data structure.
ORThe 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

Chapter # 1 | Introduction to Data Structure 1


storing data. The data is not scattered here and there in memory, instead it takes a consecutive memory
location.
Examples: Array, Link List, Queue, Stack etc.
Data: A, B, C,D, E
Linear Data Structure A B C D E
2. Non linear data Structure: A type of data structure in which data is stored in non-consecutive memory
locations is called non-linear data structure. In non linear data structure non sequential memory locations
are used for storing data. The data is stored in scattered form but linked with each other.
Examples: Tree, Graphs
Non linear data Structure A

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

Data Structure Operations:


Following types of operations can be performed on data structure:
 Insertion Operation:
Adding a new data to the data structure is called insertion operation.
 Deletion Operation:
Removing or erasing existing data from the data structure is called deletion Operation.
 Searching Operation:
Finding a specific data in the data structure is called searching operation. Searching operation is said to
be successful if the required data found in the data structure and will be said unsuccessful if the required
data is not found in data structure.
 Sorting Operation:
Arranging the data, in the data structure, in ascending or descending order is called sorting operation.
 Traversing Operation:
Accessing each element (or data) of the data structure exactly once is called traversing operation.
Traversing operation is also called visiting. Traversing is done for processing the data in data structure.
 Merging Operation: Combining two files or more into single one is called merging operation.

Chapter # 1 | Introduction to Data Structure 2


Algorithm:
Algorithm is a step by step procedure to solve a problem.
OR  Algorithm is a set of rules to complete a task.
OR  Algorithm is a finite set of instruction which accomplish a particular task.
We know that we can communicate with computer by means of computer languages. But computer languages
are strictly tight; we can’t violate their rules. So before writing the actual program we write our problem in
algorithmic language called Pseudo language; which does not follow any strict rules. Pseudo language is much
like English language which is simple. Pseudo language cannot directly implement in programming language. A
little change is required for implementing.
Steps for writing algorithms:
 Name of algorithm: it is the name of algorithms
 Introductory comment: this step is used to explain the purpose of algorithm
 Steps: these are the statement which performs some task
 End / Exit: it is the end of algorithm
Features of algorithm:
Algorithm must satisfy the following criteria.
 Input: Algorithm must have some inputs
 Output: Algorithm must have some output.
 Finiteness: Algorithm must have finite steps.
 Efficient: Algorithm should be efficient; it use less number of resources and runs fast.
 Definiteness: Each step in algorithm must be clear and unambiguous.
Algorithmic notations:
Algorithmic notations mean different essential parts of algorithm. A complete algorithmic notation is given
below:
1. Name of algorithm: Every algorithm is given an identifying name written in capital letters.
2. Introductory comment: The algorithm name is followed by a description of that task that algorithm
performs. This part explains the purpose of algorithm.
3. Steps: An algorithm is made of sequence of numbered steps. Each step performs some task. The steps
must be finite and clear. Steps may be used for input / input or for calculating some values.
4. Variables: variables are also part of an algorithm. These are used for holding values. The value is assign
to the variable by assignment operator = or.
5. End / Exit: It is the last statement. It denotes the end of algorithm.
Input and Output Statements in Algorithms:
 Input Statement: Data may be input and assigned to a variable by means of Read statement. The syntax
of Read statement is:
Read: Variables Name
For example: Read: A
 Output Statement: Similarly message, placed in quotation ‘’ and data in variables may be out by means
of Print statement. The syntax of Print statement is:
Print: ‘Message’, Variable Name
For example: Print: ‘The value is’, A

Chapter # 1 | Introduction to Data Structure 3


Examples of Algorithm:
Example#1:
Algorithm: SWAPPING
This algorithm is used to swap the values of two
variables.
Step#1: Read: A
Step#2: Read: B
Step#3: Temp=A
Step#4: A=B
Step#5: B=Temp
Step#6: Print: ‘the value in A is’, A
Step#7: Print: ‘the value in B is’, B
Step#8: Exit

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

Chapter # 1 | Introduction to Data Structure 4


Selection Statements in Algorithms:
Selection statements are used for making decisions. Decision is made by testing a condition. By applying
condition the user becomes able to execute some statement and skip the others (according to some condition).
With the help of selection stamen normal execution of steps (instructions) can be changed.
Following are some important type of selection statement used in algorithms:
 if statement
 if else statement
 else if statement

 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

Chapter # 1 | Introduction to Data Structure 5


Examples:
Algorithm: GREATER
This algorithm is used to find the greater value between two values.
Step#1: Read: A, B
Step#2: if (A>B)
Print: ‘the greater value is:’, A
else
Print: ‘the greater value is’, B
[end of if]
Setp#3: Exit

Looping Statements in Algorithms:


Looping statements are used in algorithms to execute a single statement or set of statements repeatedly.
Looping statements are used in situations where we need to repeat a statement or set of statements again and
again.
Following are important looping structures used in algorithms:
 for loop
 while loop

 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]

Chapter # 1 | Introduction to Data Structure 6


Examples:

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

Chapter # 1 | Introduction to Data Structure 7

You might also like