You are on page 1of 5

Data structures Lecture Notes Unit - I

UNIT – I
Introduction to Data Structures
1.1 Introduction
Pseudo code
 One of the most common tools for defining algorithms in pseudo code, which is part English, part
structure code.
 The pseudo code to represent both data structures and code
 Data items need not be declared, the first time we use a data name in an algorithm, it is
automatically declared.
Ex: set count to o
This statement declares a numeric data item named count and sets its value to zero
 But the structure of data to be declared as shown below
node
data
link
end node
here in the above declaration it describes node consisting of a nested structure data and a pointer to
the next node(link)
Algorithm: Example of Pseudo code
Algorithm sample(pageNumber)
This algorithm reads a file and prints a report.
Pre pageNumber passed by reference
Post Report Printed
pageNumber contains number of pages in report
Return Number of lines printed
1. loop(not end of file)
1. read file
2. if (full page)
1. increment page number
2. write page heading
3. end if
4. write report line
5. increment line count
2. end loop
3. return line count
end sample
the colored comments provide documentation clarification when required
Algorithm can also be defined as finite set of instructions that are used to accomplish a specific task.
Algorithm header
 The header will have
o Name
o List of parameters
o Precondition and post conditions

1
Prepared by: G. B. Hima Bindu, Asst. Prof., Dept. of IT, SVCET, CTR
Data structures Lecture Notes Unit - I

 This information serves to document the algorithm so it should be complete.


 In the above algorithm there is only one parameter, the page number
Purpose, Conditions, and Return
 Purpose is short statement about what algorithm does, it need to describe only general algorithm
processing but not to describe all of the processing.
 Precondition lists any precursor requirements for the parameters, if there are no preconditions we
can represent as shown below.
Pre-nothing
If there are several input parameters the preconditions should be shown for each as shown
Algorithm search (list, argument, location)
Search array for specific item and return index location
Pre list contains data array to be searched
argument contains data to be located in list
in search the precondition specifies that the two input parameters, list and argument, must be
initialized.
 The postcondition identifies any action taken and the status of any output parameters.
 If a value is returned, it is identified by a return condition. Often there is none, and no return
condition is needed.
Statement Numbers
 Statements are numbered using an abbreviated decimal notation in which only the last of the
number sequence is shown on each statement.
Variables
 We use intelligent data names – that is, names that describe the meaning of the data.
 The names of algorithm or variable makes the algorithm and its coded implementation more
readable.
 In general you should follow these rules,
o Do not use single – character names.
o Do not use generic names in application programs.
o Abbreviations are not excluded as intelligent data names
Statement constructs
 Any algorithm could be written using only three programming constructs:
o Sequence
o Selection
o Loop
Sequence
 A sequence is one or more statements that do not alter the execution path within an
algorithm. The calls are also considered as a sequence statement.
Selection
 A Selection statement evaluates a condition and executes zero or more alternatives. The
results of the evaluation determine which alternates are taken.
 The statement may be a two – way selection (such as if statement) or multiway selections
(such as switch)
 Short pseudo code is shown below

2
Prepared by: G. B. Hima Bindu, Asst. Prof., Dept. of IT, SVCET, CTR
Data structures Lecture Notes Unit - I

1 If (condition)
1 action1
2 else
1 action2
3 end if
Loop
 A Loop statement iterates a block of code. The loop that we use in our pseudo code closely
resembles the while loop. It is a pretest loop; that is, the condition is evaluated before the
body of the loop is executed. If the condition is true, the body is executed. If the condition is
false, the loop terminates.
 In the above algorithm statement 1 is an example of a loop. The end of the loop is indicated
by end loop in statement 2
Algorithm Analysis
 For selected algorithms, we give the analysis section which explains salient points.
 That examines only the points need to be emphasized or that may require some clarification.
1.2 Definition
 A data structure is an aggregation of atomic and composite data into a set with defined
relationships.
 Structure means a set of rules that holds data together
 Data structure can be nested
 We can have a data structure that consists of other data structures for ex, we can define the two
structures array and record as shown in table:
Array Record
Homogeneous sequence of data or data types Heterogeneous combination of data into a
known as elements single structure with an identified key
Position associated among the elements No association
Fig: Data structure examples
 Modern programming languages allow programmers to create new data structures for an
application.
Data structure
1. A combination of elements in which each is either a data type or another data structure
2. A set of associations or relationships (structure) involving the combined elements

Atomic and Composite data


 Atomic data are data that consist of a single piece of information; that is, they cannot be divided
into other meaningful pieces of data.
 The opposite of atomic data is composite data.
 Composite data can be broken into sub-fields that have meaning.
Data type
 A data type consists of two parts:
o A set of values
o A set of operations on values
 Table shows three data types found in all systems.

3
Prepared by: G. B. Hima Bindu, Asst. Prof., Dept. of IT, SVCET, CTR
Data structures Lecture Notes Unit - I

Type Values Operations


integer -∞, . . . , -2, -1, 0, 1, 2, . . . , ∞ *, +, -, %, /, ++, --, . . .
floating point -∞, . . . , 0.0, . . . , ∞ *, +, -, /, . . .
character \0, . . . , ‘A’, ‘B’, . . . , ‘a’, ‘b’, . . . , <, >, . . .
~
Table: Three Data Types
1.3 Abstract Data Type
 When we first started programming, there were no abstract data types.
 If we wanted to read a file, we wrote the code to read the physical file device.
 It did not take long to realize that we were writing the same code over and over again.
 So we created what is known today as an abstract data type (ADT).
 We wrote the code to read a file and placed it in a library for all programmers to use.
 This concept is found in modern languages today.
 ADT users are not concerned with how the task is done but rather with what it can do.
 The ADT consists of a set of definitions that allow programmers to use the functions while hiding
the implementation.
 This generalization of operations with unspecified implementations is known as abstraction.
The concept of abstraction means:
1. We know what a data type can do.
2. How it is done is hidden.
 An abstract data type is a data declaration packaged together with the operations that are
meaningful for the data type.
 In other words, we encapsulate the data and the operations on the data, and then we hide them
from the user.
Abstract Data Type
1. Declaration of data
2. Declaration of operations
3. Encapsulation of data and operations
1.4 Classification of Data Structures:
The data structure can be divided into two basic types: preliminary data structure and secondary data
structure.
 The primitive data structures are the basic data types such as int, char, float where the non
primitive data structures are the data structures which are basically derived from primitive data
structures.
 They can be further categorized into linear and non linear data structures.
 Linear data structures are the data structures in which the data is arranged in a list or a straight
sequence.
For ex: arrays, lists, stacks, queues
 Non linear data structures are the data structures in which data may be arranged in hierarchical
manner.
For ex: trees, graphs
Linear data structures:
Arrays:
An array is a group of homogeneous data which shares the same name.

4
Prepared by: G. B. Hima Bindu, Asst. Prof., Dept. of IT, SVCET, CTR
Data structures Lecture Notes Unit - I

List
 List is basically the collection of elements arranged in a sequential manner.
 In memory we can store the list in two ways –
o List of sequentially stored elements – using arrays
o List of elements with associated pointers – using linked lists
Stack
 A stack is a last in – first out (LIFO) data structure in which all insertions and deletions
are restricted to one end, called the top.
Queue
 A Queue is a list in which data can be inserted at one end, called the rear and deleted
from the other end, called the front. It is a first in first out(FIFO) restricted data structure.
Non Linear data structures:
Trees
 A tree consists of a finite set of elements, called nodes, and a finite set of directed lines,
called branches that connect the nodes.
Graphs
 A graph is a collection of nodes, called vertexes, and line segments , called arcs or edges,
that connect pairs of nodes.

5
Prepared by: G. B. Hima Bindu, Asst. Prof., Dept. of IT, SVCET, CTR

You might also like