Sunteți pe pagina 1din 14

Data Structure and Algorithms

Lecture 5
Objectives
By the end of this lecture you will be able to:
Define the types of Data Structures
Define Array
Define Array Operations and Algorithms
Types of Data Structures
Data structures can be classified into two categories:

Linear
Non-Linear

Linear Data Structures:
are the structures which are arranged in a sequence
The elements of such structures can be accessed linearly

Non-Linear Data Structures:
Are the structures which do not have a linear sequence

Contd
Type Data Structure
Linear
Arrays
Stacks
Queues
Simple Queue
Double Ended Queue
Linked Lists
Singly Linked List
Doubly Linked List
Header Linked List
Circular Linked List
Stack and Queues as Linked Lists
Nonlinear
Trees
Simple Trees
Binary Trees
Other Trees
Graphs
Directed Graphs
Undirected Graphs
Arrays
Linear Array:
is a list of a finite number n of homogeneous data elements
Is one of the Linear Data Structure

Each data item in an array is indexed by a number
The array items are always stored on successive memory locations

The number of elements in an array is called the length or size of
the array
The lowest index is called the Lower Bound, LB
The largest index is called the Upper Bound, UB


Contd
The length can be found by the formula:

Length = UB LB + 1

Array elements can be denoted by subscript notation as:

A
1
, A
2
, A
3
, A
4
, A
n

Or by using the C Language style

A[0], A[1], A[2], A[n]

Contd
The Delete operation can be implemented using basic String
operations as:

Delete (T, K, L) =
Substring(T, 1, K - 1) // Substring(T, K + L, Length(T) K L + 1)

For example:
T = ABCDEFG, K = 2, L = 4
S
1
= Substring (T, 1, 2 - 1) = A
S
2
= Substring (T, 2 + 4, 7 2 4 + 1) = FG
Delete(T, K, L) = S
1
// S
2
= AFG

Contd
The computers main memory, RAM, can be considered as a linear
array of millions of bytes
Each byte has it address and can be accessed
The size of each item is 32-bit

Array Representation in Memory
As said earlier, arrays are
stored on success memory
locations in the system
The name of the array always
points to the base address of
the array and is denoted by

Base (LA)


247
56
120
65
9
5
187
14
0
1
2
3
4
5
6
7
200
204
208
212
216
220
224
228
Contd
The Computer calculates the address of any element by

Loc(LA[k]) = Base(LA) + w(k - LB)

Where w is the size of a single element
Traversing Arrays
Let LA be a collection of data elements stored in the memory of
the computer
Suppose we want to print the elements or count the number of
elements
These operations can be done with the help of Traversing
Traversing is the method of accessing and processing (called
visiting) each element exactly once
Traversal Algorithms
Algo 4.1: (Traversing a Linear Array): Here LA is a linear array with
lower bound LB and upper bound UB. This algo traverses LA by
applying the PROCESS operation

1. [Initialize counter] Set K := LB
2. Repeat step 3 and 4 while K <= UB
3. [Visit Element] Apply PROCESS to LA[K]
4. [Increase counter] Set K := K + 1
[End of loop]
5. Exit

Traversal Algorithms
Algo 4.1: (Traversing a Linear Array): Here LA is a linear array with
lower bound LB and upper bound UB. This algo traverses LA with
LB and UB

1. Repeat for K = LB to UB
2. Apply PROCESS to LA[K]
[End of loop]
3. Exit

Lecture 6
Insertion and Deletion for Arrays taught from book

S-ar putea să vă placă și