Sei sulla pagina 1di 5

( 1 – ‫حال‬201( ‫ حاسب آلـى‬: ‫المقـرر‬

Lecture # 8
DATA STRUCTURES
(Introduction to Data Structures and Arrays)

1
( 1 – ‫حال‬201( ‫ حاسب آلـى‬: ‫المقـرر‬

INTRODUCTION TO DATA STRUCTURE


1. What is a data structure
- A data structure is an arrangement of data in a computer's memory or even disk
storage.

- A data structure is the logical and mathematical organization of Data.

- They are used to organize data and provide various operations upon data.

2. Types of Data Structures


There are two types of data structures

a. Linear Data Structure:

- 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.

b. Non-Linear Data Structure:

- The data values in this structure are not arranged in order.

- This type is opposite to linear.

- Tree, graph, table and sets are examples of non-linear data structures.

2
( 1 – ‫حال‬201( ‫ حاسب آلـى‬: ‫المقـرر‬

Arrays
1. What is an array
- An array is a finite collection of similar elements.

- Elements are stored in adjacent memory locations.

- An array is referenced by using an index that varies from 0-(n-1)

- [Where n is the number of elements in an array].

- The lowest index of array is known as Lower Bound.

- The highest index of an array is known as Upper Bound.

- The number of elements in an array is known as Range.

2. Declaration of an array
Arrays must be declared before they can be used in the program.

Array declaration is as:

Type name [length of an array];

Example :
int a[10];

Number of elements in an array(Range).


Name of the Array.
Datatype.

- Here the range of array starts from 0-9 ,where 0 is the lower bound and 9 is the
upper bound.

3
( 1 – ‫حال‬201( ‫ حاسب آلـى‬: ‫المقـرر‬

3. Initializing an Array
- Initial values are assigned to each element of an array by enclosing the values in
braces { }.

- For example:

- int a[5]={11,3,5,2,9};

4. Accessing Value of an Array


The index of an array always starts with 0.

A[0] A[1] A[2] A[3] A[4]

11 3 5 2 9

To access 5 we will write as:

A [2] = 5.

5. Multidimensional Arrays
Multidimensional arrays can be defined as "arrays of arrays".

The most commonly used multidimensional array is Two-Dimensional Array.

Two-Dimensional Array consists of rows and columns.

6. Declaration of Two-Dimensional Array

int class [3] [4];

Number of columns
Number of rows
Name of Array
Data type

4
( 1 – ‫حال‬201( ‫ حاسب آلـى‬: ‫المقـرر‬

Example

If the name of this two dimensional array is Class then:

0 1 2 3
0 22 23 4 20

1 12 9 5 2

33 54 19 29
2

To access 20 we write:

Class [0] [3] = 20

Class [0] [0] = 22 Class [0] [1] = 23


Class [0] [2] = 4 Class [0] [3] = 20
Class [1] [0] = 12 Class [1] [1] = 9
Class [1] [2] = 5 Class [1] [3] = 2
Class [2] [1] = 33 Class [2] [2] = 54
Class [2] [3] = 19 Class [2] [3] = 29

Potrebbero piacerti anche