0% found this document useful (0 votes)
16 views

DS Unit-1

1. Data structures act as the fundamental foundations of all computer software processes by organizing data items and their relationships. (2) They specify organization of data, accessing methods, degree of associability, and processing alternatives. (3) Primitive data structures are the most basic types like integers and characters that are directly supported by machines, while non-primitive structures are derived types like arrays and structures.

Uploaded by

ashwini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DS Unit-1

1. Data structures act as the fundamental foundations of all computer software processes by organizing data items and their relationships. (2) They specify organization of data, accessing methods, degree of associability, and processing alternatives. (3) Primitive data structures are the most basic types like integers and characters that are directly supported by machines, while non-primitive structures are derived types like arrays and structures.

Uploaded by

ashwini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

DATA STRUCTURES

INTRODUCTION TO DATA
STRUCTURES
• Data structures act as the fundamental foundations of all computer software
processes.

• Data Structure is a way of organizing all data items that considers not only the
elements stored but also their relationship to each other.

• Data Structure as a mathematical or logical model of a particular organization


of data items.

• The study of Data Structures in C deals with the study of how the data is
organized in the memory, how efficiently the data can be retrieved from the
memory, how the data is manipulated in the memory and the possible ways in
which different data items are logically related.
DATA STRUCTURE MAINLY SPECIFIES THE
FOLLOWING FOUR THINGS

• Organization of Data
• Accessing methods
• Degree of associability
• Processing alternatives for information
ELEMENTARY DATA ORGANIZATION

1.Data and Data Item


• Data are simply collection of facts and figures. Data are values or set of values. A
data item refers to a single unit of values. Data items that are divided into sub items
are group items; those that are not are called elementary items. For example, a
student’s name may be divided into three sub items – [first name, middle name and
last name] but the ID of a student would normally be treated as a single item. In the
above example ( ID, Age, Gender, First, Middle, Last, Street, Area ) are elementary
data items, whereas (Name, Address ) are group data items
2. Data Type

• Data type is a classification identifying one of various types of data, such as floating-
point, integer, or Boolean, that determines the possible values for that type; the
operations that can be done on values of that type; and the way values of that type can be
stored. It is of two types: Primitive and non-primitive data type. Primitive data type is
the basic data type that is provided by the programming language with built-in support.
This data type is native to the language and is supported by machine directly while non-
primitive data type is derived from primitive data type. For example- array, structure etc.

3.Variable

• It is a symbolic name given to some known or unknown quantity or information, for the
purpose of allowing the name to be used independently of the information it represents.
A variable name in computer source code is usually associated with a data storage
location and thus also its contents and these may change during the course of program
execution.
4. Record
• Collection of related data items is known as record. The elements of records are
usually called fields or members. Records are distinguished from arrays by the fact
that their number of fields is typically fixed, each field has a name, and that each
field may have a different type.

5. Program
• A sequence of instructions that a computer can interpret and execute is termed as
program.

6. Entity
• An entity is something that has certain attributes or properties which may be
assigned some values. The values themselves may be either numeric or non-
numeric.
7. Entity Set
• An entity set is a group of or set of similar entities. For example, employees of an organization, students
of a class etc. Each attribute of an entity set has a range of values, the set of all possible values that could
be assigned to the particular attribute. The term “information” is sometimes used for data with given
attributes, of, in other words meaningful or processed data.
8. Field
• A field is a single elementary unit of information representing an attribute of an entity, a record is the
collection of field values of a given entity and a file is the collection of records of the entities in a given
entity set.
9. File
• File is a collection of records of the entities in a given entity set. For example, file containing records of
students of a particular class.
10. Key
• A key is one or more field(s) in a record that take(s) unique values and can be used to distinguish one
record from the others.
Eg:-
Entity : Employee
Attribute : Name , Age phone
Values : "ABC", 42, 9847092568
Entity set : All employees in an organization.
ABSTRACT DATA TYPE (ADT)
• Abstract Data type (ADT) is a type (or class) for objects whose behaviour is
defined by a set of value and a set of operations.
• The definition of ADT only mentions what operations are to be performed but
not how these operations will be implemented. It does not specify how data will
be organized in memory and what algorithms will be used for implementing the
operations.
• It is called “abstract” because it gives an implementation independent view. The
process of providing only the essentials and hiding the details is known as
abstraction.
ALGORITHMS

What’s the Need for studying algorithms?

• The study of algorithms is the cornerstone of computer


science. It can be recognized as the core of computer science.

• Another reason for studying algorithms is that if we know a


standard set of important algorithms ,They help us to
enhance our analytical skills & in developing new algorithms
for required applications
An algorithm is finite set of instructions that is followed, accomplishes a particular task.

In

addition, all algorithms must satisfy the following criteria:

1. INPUT: Zero or more quantities are externally supplied.

2. OUTPUT: At least one quantity is produced.

3. UNAMBIGUITY: Each instruction is clear and produced.

4. FINITENESS: If we trace out the instruction of an algorithm, then for all cases, the

algorithm terminates after a finite number of steps.

5. EFFECTIVENESS: Every instruction must be very basic so that it can be carried out,

in

principal, by a person using only pencil and paper. It is not enough that each operation

be definite as in criterion 3, it also must be feasible.


ALGORITHMIC NOTATIONS

• While writing algorithms the following notations are considered,

1. Name of the algorithm: It specifies the problem to be solved.

2. Step number: Identification tag of an instruction and it is an unsigned

positive integer.

3. Explanatory comment: It follows the step number and describes the

operation. It should be written within a pair of square brackets.

4. Termination: It specifies the end of the algorithm. It is generally a

STOP statement and the last instruction in the algorithm.


ADVANTAGES:-

• It is step by step solution, easily understandable.

• Algorithm consists of definite & finite number of steps to arrive at a solution.

• Debugging is easy as the logical sequence is listed stepwise.

• Algorithm is independent of programming, so the user need not know the syntax of

programming language to develop the logic.

• Easy and efficient coding as it acts as a blue print during a program development.

DISADVANTAGES:-

• It is a time consuming & lengthy procedure as in algorithm is developed first which

is then converted into a flowchart & then into a computer program.

• Understanding complex logic through algorithms would be difficult.


Different patterns of algorithms

1) Sequential: In this different steps occur in a sequence

2) Conditional: In this different steps are executed based on a condition

(whether true/false). In programming languages, a conditional pattern is

implemented using decision making statements.

3) Iterational: In this a task (one or more steps) is repeated more than once.

In programming languages, an iterational pattern is implemented using

loops. An iterational construct is also known as “repetitive” construct.


Ex: Algorithm to, compute the area of a circle

Algorithm: Area of Circle

Step 1: [Read the value of radius].

Read radius

Step 2: [compute the area]

Area=3.142*radius*radius

Step 3: [Print the area]

Print ‘Area of a circle=’, Area

Step 4: [End of the algorithm]

Stop
ASYMPTOTIC NOTATIONS

Following are commonly used asymptotic


notations used in calculating running time
complexity of an algorithm
• Ο Notation

• Ω Notation

• θ Notation
BIG OH NOTATION, Ο
OMEGA NOTATION, Ω
THETA NOTATION, Θ
CLASSIFICATION OF DATA
STRUCTURES
PRIMITIVE DATA STRUCTURE
• Data at their most primitive level with in a computer is primitive data structure.
• Primitive data structure are the most basic & simple which are directly operated upon by
machine level instructions.
• The storage structure of these data structures vary from one machine to another.
• These data types consists of characters that cannot be divided and hence they also called
simple data types.
• Basic data types such as integer, real, character and Boolean are known as Primitive data
structures

NON-PRIMITIVE DATA STRUCTURE


• Data structures which are not primitive are called non-primitive data structures. They are
created using primitive.
• They can be classified as either linear data structure or non-linear data structure depending on
the type of relationship between data elements in a data structure.
• Non- Primitive data structures is the processing of complex numbers, linked lists, stacks,
TYPE OF PRIMITIVE DATA STRUCTURE
• INTEGER: it is a simple type data structure which when created is used to store

integer values. When an integer type is used we can store only one numeric value at a

time but we can vary the values at any point of time.

• REAL: it is also a simple type data like integer, but here we can store fractional values

or real values.

• CHARACTER: it is of type non-numeric simple type data which can store only one

character at a time.

• BOOLEAN: This is a logical simple type data which can take 2 possible values, true

or false.

• POINTER(LINKS): Pointer is a reference to a data structure pointer is a data type

which stores the address of the other data items or the address of the memory location.

• Ex: ‘P’ is a pointer to an integer which means P contains the address of memory

location where the contents of the memory location is an integer.


OPERATIONS ON PRIMITIVE DATA
STRUCTURE
• CREATION OPERATION: An operation frequently used with
data structure is one which creates a data structure. This operation
is called creation operation.
• Ex:- in ‘C’ using the declaration statement into K; causes memory
spaces to be created for K at execution time.
• DESTROY OPERATION: another operation, providing the
complimentary effect of a creation operation is one which destroys the
created data structure. This operation is called destroy operation. In ‘C’
one can de-allocate data structure by using a function called ‘free ( )’.
This aids in efficient use of memory.
• SELECTION OPERATION: the most frequently used operation with
data structures is accessing the data with a data structure. This type of
operation is known as selection. For more complex structure, method of
access is one of the important properties of a structure.
• UPDATE OPERATION: this operation is used to change data / value of
the structure. An assignment operation is a good example of an update
operation.
1. LINEAR DATA STRUCTURE:
• Linear Data structures are kind of data structure that has
homogeneous elements.
• The data structure in which elements are in a sequence and form a
liner series.
• Linear data structures are very easy to implement, since the memory
of the computer is also organized in a linear fashion.
• Some commonly used linear data structures are Stack, Queue and
Linked Lists.
2. NON-LINEAR DATA STRUCTURE
• A Non-Linear Data structures is a data structure in which
data item is connected to several other data items.
• Non-Linear data structure may exhibit either a hierarchical
relationship or parent child relationship.
• The data elements are not arranged in a sequential structure.
• The different non-linear data structures are trees and graphs.
DIFFERENCE BETWEEN LINEAR & NON-LINEAR DS

1. The linear data structure exhibits adjacency


relationship whereas non-linear data structure
exhibits hierarchical or parent-child relationship.
• In case of linear D.S only one link connects two
nodes, the two nodes being the predecessor node
& successor node as shown below.
• Where as in the case of non-linear D.S a node may be
connected to more than one successor & one predecessor as
shown below.

• For Node 2, we have one predecessor & 2 successors i.e.,


Node 4 & Node 5.
1. ARRAYS: An array is a type of linear D.S consisting of finite number of
similar/homogeneous

objects. Here we can derive linear relationships between the elements because elements are

arranged one after the other.

2. LINKED LIST: A linked list or one way list is a linear collection of data elements, called

nodes, where the linear order is given by means of pointers.


Each node is divided into two parts:

2. The first part contains the information of the element/node

3. The second part contains the address of the next node (link /next pointer field) in the list.
There is a special pointer Start/List contains the address of first node in the list. If this
special pointer contains null, means that List is empty. Example:
3. QUEUES: A queue, also called FIFO system, is a linear list in which
deletions can take place only at one end of the list, the Front of the list
and insertion can take place only at the other end Rear.
4. STACKS: It is an ordered group of homogeneous items of elements.

Elements are added to and removed from the top of the stack (the most

recently added items are at the top of the stack). The last element to be

added is the first to be removed (LIFO: Last In, First Out).

5.GRAPHS: Data sometimes contains a relationship between pairs of

elements which is not necessarily hierarchical in nature, e.g. an airline

flights only between the cities connected by lines. This data structure is

called Graph.
6. TREES: Tree Data frequently contain a hierarchical
relationship between various elements. The data structure
which reflects this relationship is called a rooted tree graph
or, simply, a tree.
OPERATIONS ON NON-PRIMITIVE
DS
• TRAVERSAL: Accessing / processing each element of linear list exactly once.

• INSERTION: Inserting an Item into an array.

• SEARCHING :Find the location of Item / element with given key value.

• SORTING :Arranging the element of a linear list in some logical order


(ascending or descending order)

• MERGING :Combining the elements in 2 different sorted linear lists into a


single sorted linear list.

• DESTROY OR DELETE: It is used to delete an existing data item from the


given collection of data items.
Arrays

• An array is a type of linear D.S consisting of finite number of similar/homogeneous objects.

• Here we can derive linear relationships between the elements because elements are arranged
one after the other.

• The number of elements that can be stored in an array, that is the size of array or its length is
given by the following equation: (Upperbound-lowerbound)+1

• For the above array it would be (9-0)+1=10,where 0 is the lower bound of array and 9 is the
upper bound of array.

• Array can always be read or written through loop.

• for(i=0;i<=9;i++) {

scanf(“%d”,&arr[i]);

printf(“%d”,arr[i]);

}
TYPES OF ARRAY
• Single Dimension Array (Array with one subscript)
• Two Dimension Array (Array with two subscripts (Rows
and Column))
• Multi Dimension Array (Array with Multiple subscripts)
1. ONE-DIMENSIONAL ARRAY: An array with only one row or column is called
one-dimensional array.

It is finite collection of n number of elements of same type such that:

2. can be referred by indexing.

3. The syntax Elements are stored in continuous locations.

4. Elements x to define one-dimensional array is:

Syntax: Data type Array_Name [Size];

Where,

Data type : Type of value it can store (Example: int, char, float)

Array_Name: To identify the array.

Size : The maximum number of elements that the array can hold.
REPRESENT LINEAR ARRAY IN
MEMORY

• The elements of linear array are stored in


consecutive memory locations.
2. TWO-DIMENSIONAL ARRAY: A two dimensional array is a
collection of elements and each element is identified by a pair of
subscripts.( A[3] [3] )
• Syntax: Data type Array_Name [ x ][ y ];
• The elements are stored in continuous memory locations.
• The elements of two-dimensional array as rows and columns.
• The number of rows and columns in a matrix is called as the order of
the matrix and denoted as m x n.
• The number of elements can be obtained by multiplying number of
rows and number of columns.
• Row-Major Method: All the first-row elements are stored in
sequential memory locations and then all the second-row elements are
stored and so on. Ex: A[Row][Col]
• Column-Major Method: All the first column elements are stored in
sequential memory locations and then all the second column elements
are stored and so on. Ex: A [Col][Row]
3. MULTI-DIMENSIONAL ARRAY: An array of arrays is called a
multi-dimensional array.
• Syntax: Data type Array_name[size1][size2]…[sizeN];
• TRAVERSING: It is used to access each data item exactly once
so that it can be processed.
INSERTING: It is used to add a new data item in the
given collection of data items.
DELETING: Deleting an element at the end of an array is easy but
deleting an element somewhere in the middle of an array will
require subsequent elements to be move one location upwards.
• It is used to delete an existing data item from the given collection
of data items.
Algorithm for deletion of element at specific position:
Step1.Read K[Position of the element to be deleted]
Step2. For I ← K to N-1 Do
A [I] ←A[I+1]
End for
Step3.N ← N-1[decrease the length of array by 1]
Step4.end
SPARSE MATRIX
• Matrix: Is a two-dimensional data object made of m rows and n columns,
therefore having total m x n values.
• The non-zero elements can be stored with triples, i.e., rows,

columns, and value.


• The 2d array can be used to represent a sparse matrix in which there are
three rows named as:
• Row: It is an index of a row where a non-zero element is located.

• Column: It is an index of the column where a non-zero element is located.

• Value: The value of the non-zero element is located at the index (row,
column).
Why to use Sparse Matrix instead of
simple matrix ?
• Storage: There are lesser non-zero elements than
zeros and thus lesser memory can be used to store
only those elements.
• Computing time: Computing time can be saved by
logically designing a data structure traversing only
non-zero elements..
ADVANTAGES OF ARRAYS

• It is used to represent multiple data items of same type by


using single name.
• It can be used to implement other data structures like linked
lists, stacks, queues, tree, graphs etc.
• Two-dimensional arrays are used to represent matrices.
• Many databases include one-dimensional arrays whose
elements are records.
DISADVANTAGES OF ARRAYS
• We must know in advance the how many elements are to be stored in

array.

• Array is static structure. It means that array is of fixed size. The

memory which is allocated to array cannot be increased or decreased.

• Array is fixed size; if we allocate more memory than requirement

then the memory space will be wasted.

• The elements of array are stored in consecutive memory locations. So

insertion and deletion are very difficult and time consuming.

You might also like