Data Structures Chapter 1
Data Structures Chapter 1
AND
ALGORITHMS
DCS/CSC -204
www.ubids.edu.gh.com 1
AIM
❖ Introduce students to :
www.ubids.edu.gh.com 2
Course Outline
❑Definition of Data structure
❑Types of Data Structures
❑ Implementations in C++
✓ List
✓ Stacks
✓ Queue
www.ubids.edu.gh.com 3
Reading Materials:
1. Jay Wengrow (2020) A Common-Sense Gide to Data Structures
and Algorithms (Second Edition). The Pragmatic Programmers,
LLC.
www.ubids.edu.gh.com 4
What is Data Structure?
❖Data Structure is a particular way arranging, storing and
organizing data in the memory of the computer so that the data
can easily be retrieved and efficiently utilized in the future when
required.
www.ubids.edu.gh.com 5
What is Data Structure?
❖The choice of data structure for a particular task depends on
the type and amount of data to be processed, the operations
that need to be performed on the data, and the efficiency
requirements of the program.
www.ubids.edu.gh.com 6
Why should we learn Data Structures?
1) Data Structures and Algorithms are two of the key aspects of
Computer Science.
www.ubids.edu.gh.com 8
Significant Features of Data Structures
www.ubids.edu.gh.com 9
Significant Features of Data Structures
www.ubids.edu.gh.com 10
Basic terminologies related to Data Structures
Data Structures are the building blocks of any software or
program. Selecting the suitable data structure for a program is an
extremely challenging task for a programmer.
✓Data: We can define data as an elementary value or a
collection of values. For example, the Employee's name and
ID are the data related to the Employee.
✓Data Items: A single unit of value is known as Data Item.
www.ubids.edu.gh.com 11
Basic terminologies related to Data
Structures
✓ Field: A single elementary unit of information symbolizing the
Attribute of an Entity is known as Field.
✓ Record: A collection of different data items are known as a
Record. For example, if we talk about the employee entity, then
its name, id, address, and job title can be grouped to form the
record for the employee.
✓Elementary Items: Data Items that are unable to divide into sub-
items are known as Elementary Items. For example, the ID of an
Employee.
✓Entity and Attribute: A class of certain objects is represented by
an Entity. It consists of different Attributes. Each Attribute
symbolizes the specific property of that Entity. For example
www.ubids.edu.gh.com 12
✓File: A collection of different records of one entity type is known as a
File. For example, if there are 100 employees, there will be 25 records in
the related file containing data about each employee
www.ubids.edu.gh.com 13
TYPES OF DATA STRUCTURES
Data Structures
Primitive Non-Primitive
Structure Data Structure
Linear Non-linear
int
(Sequential) (Random)
One-Dimensional Non-directional
duoble Siingle-linked list Binary Search Trees
Arrays graphs
Two -Dimensioanl
Pointer Double-Linked List B-trees
Arrays
Multi-dimensional
Arrays Circular-Linked List AVL tree
www.ubids.edu.gh.com 14
Primitive Data Structures
❖Primitive data structures: These are the most basic and
predefined data structures and are usually built into programming
languages.
❖Examples include:
✓Integer
✓Float
✓Character
✓Boolean
✓Double
✓Pointer
✓Void www.ubids.edu.gh.com 15
Non-Primitive data structures
❖Non-primitive data structures: These are complex data structures
that are built using primitive data types.
❖ Non-primitive data structures can be further categorized into the
following types:
✓Arrays: A collection of elements of the same data type, stored in
contiguous memory locations.
✓Linked lists: A collection of elements that are connected by links
or pointers.
✓Stacks: A collection of elements that follow the Last-In-First-Out
(LIFO) principle.
www.ubids.edu.gh.com 16
Non-Primitive data structures
✓Queues: A collection of elements that follow the First-In-
First-Out (FIFO) principle.
✓Trees: A hierarchical data structure consisting of nodes
connected by edges.
✓Graphs: A non-linear data structure consisting of nodes and
edges.
www.ubids.edu.gh.com 17
Differences between
Primitive and Non-Primitive Data Structures
Primitive Data Types Non-Primitive Data Types
1. Primitive data types are 1. Non-primitive data types are
predefined and provided by the
created by the programmer and are
programming language. also called user-defined data types.
2. They are atomic and store a
2. They are composite and store a
single value. collection of values or objects.
3.Primitive data types have a
3. Non-primitive data types can
fixed size in memory. have varying sizes depending on the
number of elements or objects they
contain.
4. They are passed by value 4. They are passed by reference,
when used as arguments in which means a reference to the
methods or functions. original object is passed rather than
the object itself.
www.ubids.edu.gh.com 18
Differences between
Primitive and Non-Primitive Data Structures
5. Primitive data types are stored 1. Non-primitive data types are
on the stack. stored on the heap.
6. They are initialized with default 6. Non-primitive data types need
values automatically (e.g., 0 for to be explicitly initialized by the
integers, false for booleans). programmer.
7. Primitive data types include 7. Non-primitive data types
integers, floating-point numbers, include arrays, strings, classes,
characters, booleans, structures, etc.
etc.
8. They have a limited set of 8. Non-primitive data types can
operations that can be performed have custom operations defined
directly on them (e.g., arithmetic by the programmer through
operations on numbers). methods or functions. 19
www.ubids.edu.gh.com
Differences between
Primitive and Non-Primitive Data Structures
9. Primitive data types are 9. Non-primitive data types may
generally faster to access and require more complex operations
manipulate. and have a higher overhead in
terms of performance.
10. They have a shorter lifespan 10. Non-primitive data types can
as they are short-lived and are have a longer lifespan as they can
destroyed as soon as they go out be dynamically allocated and
of scope. deallocated during program
execution
www.ubids.edu.gh.com 20
www.ubids.edu.gh.com 21
Linear Data Structure
www.ubids.edu.gh.com 22
Linear Data Structure
Arrays Queues
Single-Linked list
Stacks www.ubids.edu.gh.com 23
Non-Linear Data Structure
❖ Non-linear data structure is a type of data structure in which
data elements are not arranged in a sequential order, and each
element may have one or more predecessors and successors.
2. Examples include arrays, linked lists, 2. Examples include trees and graphs.
stacks, and queues.
3. Linear data structures have a simple 3. Non-linear data structures have a
organizational structure. more complex organizational structure.
www.ubids.edu.gh.com 27