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

Topic 1 Part 2

This document discusses data structures and arrays. It covers topics such as introduction to data structures, lists and linked lists, stacks, queues, and trees. It also discusses arrays in detail as a basic data structure. The key points are that an array can store multiple values of the same data type, arrays have a fixed size that must be defined, and inserting or deleting elements from an array is slow. Advantages of arrays include efficient storage of similar data, but disadvantages include a fixed size and slow insertion and deletion. Exercises are provided to demonstrate working with arrays and structures.

Uploaded by

Arif Iskandar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Topic 1 Part 2

This document discusses data structures and arrays. It covers topics such as introduction to data structures, lists and linked lists, stacks, queues, and trees. It also discusses arrays in detail as a basic data structure. The key points are that an array can store multiple values of the same data type, arrays have a fixed size that must be defined, and inserting or deleting elements from an array is slow. Advantages of arrays include efficient storage of similar data, but disadvantages include a fixed size and slow insertion and deletion. Exercises are provided to demonstrate working with arrays and structures.

Uploaded by

Arif Iskandar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

DFC 30233

DATA STRUCTURES

DFC 30233
DATA STRUCTURES
TOPICS

Topic 1
Introduction to
data structures
Topic 6 Topic 2
Sorting &
List & Linked List
Searching
DFC 30233
Topic 5 Topic 3
Trees Stack
Topic 4
Queues
CONTENT

Data structure

Types of data structures


Data
Structures
Abstract Data Type (ADT)

Topic 1 Structures

Array as basic data structure


Array Data
Structures
Disadvantages of arrays
COURSE LEARNING OUTCOME

Data structure

Types of data structures


Data
Structures
Abstract Data Type (ADT)

Topic 1 Structures

Array as basic data structure


Array Data
Structures
Disadvantages of arrays
ARRAY DATA STRUCTURES
❑ An array is a variable which can store multiple values of same data type at a time.
❑ Collection of similar data items stored in continuous memory locations with single name.
❑ Example array declaration : int a[3];

❑ Other examples:

int A[5];
int A[3] = {100, 1000, 10000};

Where int – is the data type of Integer


A[5] – Array A of size 5
A[3] – Array A of size 3
ARRAY DATA STRUCTURES
❑ Compiler not only allocates memory, but also assigns a numerical value to each individual element
of an array.
❑ This numerical value is called as "Index".
ARRAY DATA STRUCTURES
❑ If we want to assign a value to any of these memory locations (array elements), we can assign as follows
a[1] = 100;
ARRAY DATA STRUCTURES
❑ Important concepts of an array

✓ Array always fixed size of data elements (Declaration)


✓ Array can be considered as a static data
✓ Array elements are placed in sequential order thus eliminating
the gap in the memory allocation
✓ The index value starts with 0
✓ The data elements should be of same data type
✓ Array can be used in search algorithms (Binary search)
✓ Array can be used in sorting algorithms (Bubble sort)
✓ An array can be declared, initialized an referred by value
ARRAY OF STRUCTURE
❑ Given is declaration for an array of structure:

struct product
{ Array of Structure
int weight;
float price;
} fruits[3];

weight weight weight


price price price

fruits[0] fruits[1] fruits[2]


ARRAY OF STRUCTURE
❑ Accept input from user for an array of structure:

for(int x = 0; x < 3; x++)


{
cin>>fruits[x].weight;
cin>>fruits[x].price;
}

2.5 weight 4.5 weight 3.5 weight

5.00 price 7.20 price 3.60 price

fruits[0] fruits[1] fruits[2]


ARRAY OF STRUCTURE
❑ Write the statement used to display all the items.

for(int x = 0; x < 3; x++)


{
cout<<“Weight (kg):”<<fruits[x].weight<<endl;
cout<<“Price (RM) :”<<fruits[x].price<<endl;
}

Weight (kg) : 2.5


Price (RM) : 5.00
Weight (kg) : 4.5 Output
Price (RM) : 7.20
Weight (kg) : 3.5
Price (RM) : 3.60
NESTED STRUCTURE
❑ Structures can also be nested so that a valid element of a structure can also be on its turn another
structure
struct movies name title
{
char title[20]; year
int year;
favMovie
};
charlie
struct friends
{
name title
char name[20];
movies favMovie; year
} charlie, maria;
favMovie

maria
NESTED STRUCTURE

name title

charlie.name; year

favMovie
maria.favMovie.title;
charlie
charlie.favMovie.year;
name title

year

favMovie

maria
CONTENT

Data structure

Types of data structures


Data
Structures
Abstract Data Type (ADT)

Topic 1 Structures

Array as basic data structure


Array Data
Structures
Disadvantages of arrays
DATA STRUCTURES
Advantages of using arrays:
❑ It is better and convenient way of storing the data of same data type with same size.
❑ It allows us to store known number of elements in it.
❑ It allocates memory in contiguous memory locations for its elements.
❑ Iterating the arrays using their index is faster compared to any other methods like linked list etc.
❑ It allows to store the elements in any dimensional array - supports multidimensional array.
DATA STRUCTURES
Disadvantages of using arrays:
❑ It allows us to enter only fixed number of elements into it. We cannot alter the size of the array once
array is declared. Hence if we need to insert more number of records than declared then it is not
possible. We should know array size at the compile time itself.
❑ Deleting the records from the array would be slow since we need to traverse contiguously the index of
the elements to be deleted and manage memory space too.
❑ It does not verify the indexes while compiling the array. In case there is any indexes pointed which is
more than the dimension specified, then we will get run time errors rather than identifying them at
compile time.
ARRAY DATA STRUCTURES

Advantages Disadvantages

It is used to represent multiple data items of We must know in advance that how many
same type by using only single name elements are to be stored in array

Array is static structure. It means that array is of


It can be used to implement other data structures
fixed size. The memory which is allocated to
like linked lists, stacks, queues, trees, graphs
array can not be increased or reduced

Since array is of fixed size, if we allocate more


2D arrays are used to represent matrices memory that equipment, 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
EXERCISE

1. Redraw the complete diagram for an array below:


int Number [6] = {27,99,70,70,87,81}
EXERCISE 2
2. Consider struct definition and declaration below:

struct Car
{
char platNum[10];
char owner[25];
int day, month, year;
};
struct Car C[100];

Write statement to display all the value for the struct variable C.
EXERCISE 3
3. Consider struct definition and declaration below:

struct Date
{
int day, month, year;
};
struct Date D[70];

Write statement to display all the value for the struct variable D.
CONTENT

Data structure

Types of data structures


Data
Structures
Abstract Data Type (ADT)

Topic 1 Structures

Array as basic data structure


Array Data
Structures
Disadvantages of arrays
THANKS!
Any questions?

You might also like