Professional Documents
Culture Documents
Lec 1
Lec 1
Insertion
Deletion
2
INTRODUCTION
Data Structure can be classified as:
linear
non-linear
Linear (elements arranged in sequential in memory location) i.e.
array & linear link-list
Non-linear such as a tree and graph.
Operations:
Traversing, Searching, Inserting, Deleting, Sorting, Merging
Arrayis used to store a fix size for data and a link-list the data can
be varies in size.
3
INTRODUCTION
Advantages of an Array:
Very simple
Economy – if full use of memory
Random accessed at the same time
Disadvantage of an Array:
wasting memory if not fully used
4
LINEAR ARRAY
Homogeneous data:
a) Elements are represented through indexes.
b) Elements are saved in sequential in memory locations.
Number of elements, N –> length or size of an array.
If:
UB : upper bound ( the largest index)
LB : lower bound (the smallest index)
Then: N = UB – LB + 1
Length = N = UB when LB = 1 5
Travesing linear array
Let LA is a linear Array with lower bound LB and Upper bound UB. This Algorithm will apply
PROCESS to each element of LA.
Here LA is
LA 12 42 34 25
0 1 2 3
LB UB
LA
12 42 34 67 70
25
0 1 2 3 4 5
LB UB
STEPS
(1) START
K=K+1
(6) STOP
Step:
1. START
N=5
2. Initialize Counter (Assign Counter)
Set J=N-1 25 30
LA
3. Repeat 4th and 5th while (J >= K)
4. Shift (Move) to the right all element from J 12 42 34
0 1 2 360 4 5
LA[J+1]=LA[J] LB
LB ITEM=70, K=2
5. Decrement Counter
J=J-1
6. Insert ITEM
LA[K]=ITEM
7. Reset N
N = N+1
8. STOP
Step:
1. START N=3
2. Set ITEM = LA[K] 12
LA 42 25 45 67 56
3. Initialize counter
set J=K; 0 1 2 3 4
4. Repeat 5th and 6th step while J<=N-2
LB
5. Move J+1th element shift to left LB
LA[J]=LA[J+1]
6. Increment Counter
J=J+1
7. Reset N=N-1