Data Structures
Data Structures
Data structure
Primitive DS Non-Primitive DS
Non-Primitive DS
Linear Non-Linear
• Eg. Scores[10]
• the name of the array is scores and name of each element is the name of the array
followed by the index, for example, scores[1], scores[2], and so on. In this chapter,
we mostly need the names of the elements, but in some languages, such as C, we also
need to use the name of the array.
Memory layout
• The indexes in a one-dimensional array directly define the
relative positions of the element in actual memory. Figure
shows a two-dimensional array and how it is stored in
memory using row-major or column-major storage. Row-
major storage is more common.
Memory layout
The number of elements that can be stored in an array, that is the
size of array or its length is given by the following equation:
(Upperbound-lowerbound)+1
Memory105layout
101
102
104 107
• Address of A[i][j]=B+W*[N*(i-Lr)+(j-Lc)]
• Column Major System :
• A[2][3]=100+1(3(2-1)+(3-1))=100+5=105
• A[2[[3]=100+(2-1)+3(3-1)=107
Memory layout
• We have stored the two-dimensional array students in
memory. The array is 100 × 4 (100 rows and 4 columns).
Show the address of the element students[5][3] assuming
that the element student[1][1] is stored in the memory
location with address 1000 and each element occupies only
one memory location. The computer uses row-major
storage.
• Overflow
• Underflow
Insertion at the end
Algorithm:
Insend (A is the array, LB is the lower bound ,Ub
is the upper bound, n is the no of elements
present, item is the Value we want to insert )
1 . If n=UB write overflow and exit.
2. n=n+1
3. A[n]=item
4. exit
Insertion at the beginning
Algorithm:
Insbeg(A,LB,UB,n,item)
1 . If n=UB write overflow and exit.
2. For k=n to 1 (-1)
a) A[k+1]=A[k]
(End of for)
3. A[1]=item
4. n=n+1 item 10 20 30
5. exit
example
Deletion from the end
10 20 10
Algorithm:
Delend(A,LB,UB,n,item)
1 . If n=0 write underflow and exit.
2. Item=A[n]
3. n=n-1
4. exit
Deletion from the beginning
20 30 40
Algorithm:
Delbeg (A,LB,UB,n,item)
1 . If n=0 write underflow and exit.
2. Item=A[1]
3. For j=1to n-1 do (+1)
a) A[J] = A[J + 1]
b) J = J+1
6. N = N-1
7. Exit
• Start
• 2. Set J = 0
• 3. Repeat steps 4 and 5 while J < N
• 4. IF LA[J] is equal ITEM THEN GOTO STEP 6
• 5. Set J = J +1
• 6. PRINT J, ITEM
• 7. Stop
Insertion at Specific Location
Algorithm INSSPEC(A,K,N,UB,ITEM)
A is the array , K is the location, N is the no of elements already present.