Lecture-01-03 (Data Structures, Types and Operations) - 1
Lecture-01-03 (Data Structures, Types and Operations) - 1
1
Fair Use Notice
3
Introduction to the Course
• Data: value or a set of values: It represents either the value of a
variable or a constant
Total 100
Marks
Test &
Attendance Mid Term = 20 Final Exam =
Assignments =
Marks =10 Marks 60 Marks
10 Marks
5
What is Data Structure?
8
Overview of Data Structures
• Data structure is a representation of the logical relationship existing
between individual elements of data.
• Data Structure is a way of organizing all data items that considers
not only the elements stored but also their relationship to each
other.
• We can also define data structure as a mathematical or logical model
of a particular organization of data items.
• It is defined as the way of storing and manipulating data in organized
form so that it can be used efficiently.
9
Overview of Data Structures
• Data Structure mainly specifies the following;
• Organization of Data
• Accessing methods
• Algorithm + Data Structure = Program
• Data structure study covers the following points
• Amount of memory require to store.
• Amount of time require to process.
• Representation of data in memory.
• Operations performed on that data.
10
Importance of Data Structures
• Data structure is a process through which data is stored and arranged
in the disk space of the computer or memory storage, in a way that
the data can be easily used and manipulated in the future.
• It is an effective way of performing various operations related
to data management.
• Example: For the same reason we need a librarians and clerks to stack
up the right books in right places and to maintain a log of records for
the same. Just imagine a library with no librarian or clerks and the
books were stacked up by individuals from the public as and when
they visited the library.
11
Example
• Suppose you go in a restaurant and
• there is no proper arrangement of tables and
• people are just scattered and every thing is chaotic.
• But you decided to eat any way…. You ordered three types of food.
• He then bring you one type of food(chapatis) first,
• then half an hour later, he brings another type of food (Gravy)
• and then brings 3rd food after you leave…
• I know imagining this kind of situation is freakishly disturbing.
• The same thing will happen to you if you do not do proper structuring of data.
• The restaurant will be replaced by data storage and the food will be data. It will
really hard to find anything or process in reasonable time and a lot of resources
and time will be wasted
12
Need For Data Structure
13
Classification of Data Structures
• The data structures can also be classified on the basis of the following
characteristics:
1. Linear: In Linear data structures, the data items are arranged in a
linear sequence. Example: Array
2. Non-Linear: In Non-Linear data structures, the data items are not
in sequence. Example: Tree, Graph
3. Homogeneous: In homogeneous data structures, all the elements
are of same type. Example: Array
4. Non-Homogeneous: In Non-Homogeneous data structure, the
elements may or may not be of the same type.
Example: Structures
14
Classification of Data Structures
5. Static: Static data structures are those whose sizes and structures
associated memory locations are fixed, at compile time.
Example: Array
6. Dynamic: Dynamic structures are those which expands or shrinks
depending upon the program need and its execution. Also, their
associated memory locations changes. Example: Linked List
created using pointers
15
Classification of Data Structures
16
17
Classification of Data Structures
• Data Structures are normally classified into two broad categories
• Primitive Data Structure
• Non-primitive data Structure
18
Primitive Data Structures
• Anything that can store data can be called as a data structure.
• Hence Integer, Float, Boolean, Char etc, all are data structures. They are known
as Primitive Data Structures.
• Primitive data structures are basic structures and are directly operated upon by
machine instructions.
• Primitive data structures have different representations on different computers.
• Integers, floats, character and pointers are examples of primitive data structures.
• These data types are available in most programming languages as built in type.
• Integer: It is a data type which allows all values without fraction part. We can
use it for whole numbers.
• Float: It is a data type which use for storing fractional numbers.
• Character: It is a data type which is used for character values.
• Pointer: A variable that holds memory address of another variable are called
pointer.
19
The most commonly used operation on data structure are
broadly categorized into following types:
◦ Create
◦ Selection
◦ Updating
◦ Destroy or Delete
20
Non-Primitive Data Structures
• These are more sophisticated data structures.
• These are derived from primitive data structures.
• The non-primitive data structures emphasize on structuring of a group of
homogeneous or heterogeneous data items.
• Examples of Non-primitive data type are Array, List, and File etc.
• A Non-primitive data type is further divided into Linear and Non-Linear
data structure
• Array: An array is a fixed-size sequenced collection of elements of the same data
type.
• List: An ordered set containing variable number of elements is called as Lists.
• File: A file is a collection of logically related information. It can be viewed as a large
list of records consisting of various fields.
21
Linear Data Structures
• A data structure is said to be Linear, if its elements are connected in
linear fashion by means of logically or in sequence memory locations.
• There are two ways to represent a linear data structure in memory,
• Static memory allocation
• Dynamic memory allocation
• The possible operations on the linear data structure are: Traversal,
Insertion, Deletion, Searching, Sorting and Merging.
• Examples of Linear Data Structure are Stack and Queue.
22
Non-Linear Data Structures
• Nonlinear data structures are those data structure in which data items
are not arranged in a sequence.
• Examples of Non-linear Data Structure are Tree and Graph.
• Tree: A tree can be defined as finite set of data items (nodes) in which
data items are arranged in branches and sub branches according to
requirement.
• Trees represent the hierarchical relationship between various elements.
• Tree consist of nodes connected by edge, the node represented by circle and
edge lives connecting to circle.
23
Non-Linear Data Structures
• Graph: Graph is a collection of nodes (Information) and connecting
edges (Logical relation) between nodes.
• A tree can be viewed as restricted graph.
• Graphs have many types:
• Un-directed Graph
• Directed Graph
• Mixed Graph
• Multi Graph
• Simple Graph
• Null Graph
• Weighted Graph
24
Difference between Linear and Non Linear Data
Structure
25
Dynamic Data Structures
• A dynamic data structure (DDS) refers to an organization or collection
of data in memory that has the flexibility to grow or shrink in size,
enabling a programmer to control exactly how much memory is
utilized.
• Examples of dynamic data structures include records, pointers, lists.
• Linked list is a dynamic data structure whose length can be increased
or decreased at run time. ... An array is a static data structure. This
means the length of array cannot be altered at run time. While, a
linked list is a dynamic data structure.
26
Records
• Records are used to group a number of (different) fields.
• A person record may group name, age, city, nationality.
• Grouping of fields is a basic and often used technique.
• It is available in all programming languages
27
Records in Java
• In Java a class is used to group fields:
28
Records in C
• In C a struct is used to group fields
29
Pointers
30
Pointers
• Here is an example of
variable, pointer, and
record.
• integer i
• pointer p to an
integer (55)
• record r with integer
components a(17) and
b(24)
• pointer s that points to
r
31
Lists
• A list of integers:
32
Abstract Data Type (ADT)
• ADT is a type (or class) for objects whose behavior is defined by a set
of values and a set of operations.
• So a user only needs to know what a data type can do, but not how it
will be implemented.
• Think of ADT as a black box which hides the inner structures and
design of the data types.
• The ADT is made of with primitive data types, but operation logics are
hidden.
33
Example of ADTs
• We discuss a number of popular ADTs:
– Stacks
– Queues
– Priority Queues
– Ordered Lists
– Dictionaries (realized by Trees)
34
Why ADTs?
• ADTs encapsulate data structures and algorithms
that implement them.
• ADTs allow one to separate the check of correctness and the performance
analysis:
1. Design the algorithm using an ADT
2. Count how often different ADT operations are used
3. Choose suitable implementations of ADT operations
35
Characteristics of ADT
1. It provides a description of
element in term of data types
2. It defines relationship among
individual elements.
3. valid operations and parameters
to be passed.
4. error conditions associated with
the operations.
36
Example of ADT
• An ADT stacks can be defined by packaging together the following
operations.
• -> push(): It is used for inserting an item in stack
• ->pop(): It is used for returning an item.
• ->peek(): It returns top most element from the stack.
• -> size(): It tells the total number of elements in stack.
• -> isempty(): It has two values , if it returns true that means stack is
empty and if it returns false that means stack is not empty.
37
Example of ADT
• -> Set of operations and element of ADT Data Structure or
manipulated through interface.
• -> Implementation of operations and Data Items are hidden from the
application program.
38
Data Structure Operations
• The data in the data structures are processed by certain operations. The
particular data structure chosen largely depends on the frequency of the
operation that needs to be performed on the data structure.
39
Data Structure Operations
5. Sorting- It is used to arrange the data items in some order i.e. in
ascending or descending order in case of numerical data and in
dictionary order in case of alphanumeric data.
6. Merging- It is used to combine the data items of two sorted files
into single file in the sorted form.
40
Traversing
• The original array elements are :
• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• LA[3] = 7
• LA[4] = 8
41