0% found this document useful (0 votes)
5 views24 pages

Class 01 DataT Ypes and Structures

The document provides an overview of data structures, including data types, abstract data types, and the importance of selecting appropriate data structures for efficient programming. It discusses various types of data structures, such as linear and non-linear, as well as core operations like adding, accessing, and removing data. Additionally, it covers arrays, including one-dimensional and two-dimensional arrays, and their significance in organizing data efficiently.

Uploaded by

26alomari2001
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)
5 views24 pages

Class 01 DataT Ypes and Structures

The document provides an overview of data structures, including data types, abstract data types, and the importance of selecting appropriate data structures for efficient programming. It discusses various types of data structures, such as linear and non-linear, as well as core operations like adding, accessing, and removing data. Additionally, it covers arrays, including one-dimensional and two-dimensional arrays, and their significance in organizing data efficiently.

Uploaded by

26alomari2001
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/ 24

Data Structures

Chapter 1

Data types and structures

Data Structures Slide 1 of 24


Data types and structures :
When we programming in any programming language, we need
to use variables to store various information.

Variables are reserved memory locations to store values. It


means that when you create a variable you reserve some space
in the computer memory.

We may need to store data of various data types such as


character, integer, floating point, double floating point,
boolean,… etc.

Based on the data type of a variable, the operating system


allocates memory and decides what can be stored in the
reserved memory.
Data Structures Slide 2 of 24
Data types
Table lists down five basic C++ data types:

Data type Keyword


Integer int
Character char
Floating point float
Double floating double
point
Boolean bool

Data Structures Slide 3 of 24


Abstract data type (ADT)
Sometimes while programming, a situation occurs when built-in
data types are not enough to handle the complex data
structures.
It is the programmer's responsibility to create this special kind
of data type.

Such custom data types are called Abstract data type.

In C++ struct keywords is used to create abstract data type.

For example, if the we need to define a student record


data type ,which is not available in C++.
So it is possible create such a data type using struct.

Data Structures Slide 4 of 24


Abstract data type (example)
#include<iostream.h>

struct record{
char name[20] ;
int age;
float avg;
};
main(){
record student;
cout<<"Enter your name :";
cin>> student.name;
cout<<"Enter your age :";
cin>> student.age;
cout <<"\n your name is : " << student.name;
cout <<"\n your age is : " << student.age;}
Data Structures Slide 5 of 24
Data Structure
• Data structure is a particular way of storing and organizing
data in a computer so that it can be (searched, processed,
or modified) efficiently.

• The choice of data structure and algorithm can make the


difference between a program running in a few seconds or
many days.

Suitable Suitable Efficient


Data structure + Algorithm = Program

Data Structures Slide 6 of 24


Efficiency
 A solution is said to be efficient if it solves
the problem within its resource constraints.
 Space
 Time

 The cost of a solution is the amount of


resources that the solution consumes.

Data Structures Slide 7 of 24


Selecting a Data Structure
Some criterion used to select a data structure:

 Analyze the problem to determine the


resource constraints a solution must meet.
 Determine the basic operations that must be
supported.
 Select the data structure that best meets
these requirements.

Data Structures Slide 8 of 24


Selecting a Data Structure (cont)
 Each data structure has its costs and
benefits.
 There is no global data structure
(its the best and efficient for all situations).
 Data structure requires:
 space for each data item it stores,
 time to perform each basic operation, and
 programming effort.

Data Structures Slide 9 of 24


Types Of Data Structures
Data structures have been classified in several ways :
Linear

In linear data structures, values are arranged in linear fashion.


Arrays, linked lists, stacks and queues are examples of linear
data structures in which values are stored in a sequence.

Non-Linear

This type is opposite to linear. The data values in this structure


are not arranged in order. Tree, graph, table and sets are
examples of non-linear data structures.

Data Structures Slide 10 of 24


Types Of Data Structures (cont)

Homogenous

In this type of data structures, values of the same types of data


are stored, as in an array.

Non-homogenous

In this type of data structures, data values of different types are


grouped, as in structures and classes.

Data Structures Slide 11 of 24


Types Of Data Structures (cont)

Dynamic

In dynamic data structures such as references and pointers,


size and memory locations can be changed during
program execution.

Static

The value of a static variables are persists .

Data Structures Slide 12 of 24


Types Of Data Structures (cont)

Linear Data Structure


Data Structures Slide 13 of 24
Types Of Data Structures (cont)

Non-Linear Data Structure


Data Structures Slide 14 of 24
Types Of Data Structures (cont)

A data structure is a scheme for organizing data in


computer’s memory.

Some of the more commonly used data structures include


lists, arrays, stacks, queues, heaps, trees, and graphs
The way in which the data is organized affects the
performance of a program for different tasks.

Computer programmers decide which data structures


to use based on the nature of the data and the
processes that need to be performed on that data.

Data Structures Slide 15 of 24


Core Operations

Data Structures have 3 core operations :

- a way to add things.


- a way to access things.
- a way to remove things.
Details of these operations depend on the data structure

More operations added depending on what data


structure is designed to do .

Data Structures Slide 16 of 24


(Array) data structure
is the Most commonly used
Imagine that we have 100 scores. We need to read them, process them
and print them. We must also keep these 100 scores in memory for the
duration of the program. We can define a hundred variables,
each with a different name, as shown in below

Data Structures Slide 17 of 24


Array ( cont)
But having 100 different names creates other problems. We need 100
references to read them, 100 references to process them and
100 references to write them. The Figure below shows a diagram that
illustrates this problem.

Data Structures Slide 18 of 24


Array ( cont)

An array is a sequenced collection of elements, normally of the same


data type, We can refer to the elements in the array as the first element,
the second element and so forth, until we get to the last element.

We can use loops to read and write the elements in an array

Now it does not matter if there are 100, 1000 or 10,000 elements !!!

Data Structures Slide 19 of 24


Array ( cont)

One Dimension Array


 Structured collection of components
(All of the same type)
 Structure given a single name
 Individual elements accessed by index
(Direct access )
 Type of elements stored in an array can
be anything
 Index of an array must be an integer

Data Structures Slide 20 of 24


Data Structures Slide 21 of 24
Array ( cont)

Two Dimensional Array


A two-dimensional array consists of rows and
columns of elements. It is essentially a matrix.

To declare a two-dimensional array, we merely use


two sets of square brackets.

 The first contains the number of rows


 The second contains the number of columns

//declaring 2D array with 3 rows and 4 columns in C++


int vals[3][4];

Data Structures Slide 22 of 24


Initializing 2D arrays

You can use additional braces to indicate when rows start and
end:

int val[3][4] = { {8,16,9,52},// first row in the array


{3,15,27,6}, // second row in the array
{14,25,2,10} // third row in the array
};

val Col 0 Col 1 Col 2 Col 3


Row 0 8 16 9 52
Row 1 3 15 27 6
Row 2 14 25 2 10

Data Structures Slide 23 of 24


Indices in 2D array
 Suppose we have two dimensional array called
val which is declared as follows:

val Col 0 Col 1 Col 2 Col 3

Row 0 8 16 9 52

Row 1 3 15 27 6
6

Row 2 14 25 2 10

 To access the cell containing 6, we reference


val[1][3] that is, row 1, column 3.

Data Structures Slide 24 of 24

You might also like