Vous êtes sur la page 1sur 17

DFC 3033 – DATA

STRUCTURES
Learning Outcomes:

1.1 Understand data structure


- Define data structure
- Describe with example the types of data structures
- Describe the Abstract Data Type (ADT)
- Define and declare structures
- Illustrate structure in memories
1.2 Understand Array Data Structure
- Describe array as basic data structure.
- Identify the disadvantages of arrays.
Definition

▪In computer science, a data structure is a particular way


of organizing data in a computer so that it can be
used efficiently.
▪Data structure also can be defined as an arrangement of
data in the computer’s memory.
Types of data structures
PRIMITIVE NON-PRIMITIVE
that you have a value stored in memory, The data types that are derived from
this value has no methods or primary data types.
internal structure. A primitive can These datatypes are used to store group
only be operated on by external of values.
operations.

E.g: Boolean, true or false E.g: Arrays


character Structure
float Union
integer linked list
double Stacks
enumerated types Queue
Types of data structures
LINEAR NON-LINEAR

A data structure is said to be linear And the data structure where there is
if its elements from a sequence no such sequence are called non
linear.
E.g: array or linked list. E.g: tree and graph
Types of data structures
STATIC DYNAMIC

The size of the structure is fixed The number of items to be stored is not known
before hand.
Static data structures are very good for In this case the programmer will consider using
storing a well-defined number of data items. a dynamic data structure. This means the data
structure is allowed to grow and shrink as the
demand for storage arises. The programmer
should also set a maximum size to help avoid
memory collision.
For example a programmer might be coding For example, programmer coding a print spooler
an ‘UNDO’ function where the last 10 user will have to maintain a data structure to store
actions are kept in case they want to undo print jobs, but cannot know before hand how
their actions. In this case the maximum many jobs there will be.
allowed is 10 steps and so he decides to form
a 10 item data structure.
Types of data structures
STATIC DYNAMIC

Memory is allocated to the data structure Memory is allocated at compile time. Fixed size
dynamically i.e as the program executes

Advantage: makes the most efficient use of Advantage: the memory allocation is fixed and so
memory as the data structure only uses as much there will be no problem with adding and removing
memory as it needs. data items.
Disadvantage: harder to program as the software Advantage: Easier to program as there is no need to
needs to keep track of its size and data item check on data structure size at any point.
locations at all times.
Disadvantage: because the memory allocation is Disadvantage: can be very inefficient as the memory
dynamic, it is possible for the structure to for the data structure has been set aside regardless of
‘overflow’ should it exceed its allowed limit. It can whether it is needed or not whilst the program is
also ‘underflow’ should it become empty. executing.
Data types

 A data type in a programming language is a set of data


with the values having predefined characteristics.
 System/ compiler defined data type are called
primitive data type.
 Structure in C/C++ are the means to create our own
data type known as user defined data type.
Abstract Data types (ADT)
 All primitive data types supports basic operations like
addition, subtraction, etc.
 The system is providing the implementation for the
primitive data types.
 For non primitive data types we also need to define
operations.
 Combination of data structure and their operations
are known as Abstract Data Type.
Abstract Data types (ADT)
 So data structure is all about creating abstract data
type.
 Any piece of information can be handled by defining
appropriate data type and set of possible operations.
Structure
 is a group of data elements grouped together under one name.
These data elements, known as members, can have different types
and different lengths.
Example:
 Syntax :
struct type_name { struct StudentInfo {
member_type1 member_name1; char name[50];
member_type2 member_name2; char matric_no[12];
member_type3 member_name3; int sem;
. float hpnm;
. } DDT2B;
} object_names;
StudentInfo DDT2A;
Illustration Structure in Memories.
Arrays
 Arrays are the collection of a finite number of
homogeneous data elements.
 The collection forms a data structure where objects
are stored linearly.
 Elements of the array are referenced respectively by
an index set consisting of n consecutive numbers and
are stored respectively in successive memory
locations.
45 56
Arrays
The position of the element in the array is called index.
The array element can be accessed in sequential as well as
in the random order with the help of the index.
1 3 5 7 9
Array elements
0 1 2 3 4
Index
Arrays
• Array is a container which can hold fix number of items and these item should be of
same type.
• Most of the data structure make use of array to implement their algorithms.
• Following are important terms to understand the concepts of Array
Element – each item stored in an array is called an element.
Index – Each location of an element ia an array has a numerical
index which is used to identify the element.

Array Representation
• Array can be declared in various ways in different languages. For illustration, let’s
take C array declaration.
Arrays

• As per above shown illustration, following are the important points to be considered.
- index start with 0.
- array length is 10 which means it can store 10 elements.
- each element can be accessed via its index. For example, we can fetch element
at index 6 as 27.
Disadvantages of Arrays
▪ We must know in advance that 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 can not be increased or reduced.
▪ Since array is of fixed size, if we allocate more memory than
requirement then the memory space will be wasted. And if we
allocate less memory than requirement, then it will create problem.
▪ The elements of array are stored in consecutive memory locations.
So insertions and deletions are very difficult and time consuming.

Vous aimerez peut-être aussi