DS Module- 2 (1)
DS Module- 2 (1)
BCA DS –
MODULE 2
MAJLIS ARTS AND SCIENCE COLLEGE, PURAMANNUR
Traversal
Insertion
Deletion
Traversal
It prints all the array elements one after another.
Algorithm
LA is a linear array with lower bound LB and upper bound UB.
step 1: set k=LB
step 2: Repeat steps 3 and 4 while k<=UB
step 3: Apply visit to LA[k]
step 4: set k=k+1
step 5: exit
Insertion
It adds an element at given index.
Algorithm
INSERT(LA,N,K,ITEM)
LA is a linear array with N elements and K is a positive integer K<=N. This algorithm inserts an element ITEM into
the Kth position in LA.
Step 1: set J=N
step 2: Repeat steps 3 and 4 while J>=K
step 3: set LA[J+1]=LA[J]
step 4: set J=J-1
step 5: set LA[K]=ITEM
step 6: set N=N+1
step 7: exit
Deletion
Operation of removing one of the elements from LA.
Algorithm
DELETE(LA,N,K,ITEM)
LA is a linear array with N elements and K is a positive integer ,K<=N. This algorithm deletes the Kth element
from LA.
step 1: set ITEM=LA[K]
step 2: Repeat for J=K to N-1
step 3: set LA[J]=LA[J+1]
step 4: set N=N-1
step 5: exit
11. What is the main difference between array and linked list?
Ans:
An array is a list of finite number of n homogeneous data elements.(i.e. data elements of the same type) such that
The elements of an array are referenced by an index.
Elements of array stored at contiguous memory locations.
This makes it easier to calculate the position of each element by simply adding an offset to a base value.
Linked List is an ordered collection of elements of same type, which are connected to each other using pointers.
Linked List supports Sequential Access, which means to access any element/node in a linked list, we have to
sequentially traverse the complete linked list, upto that element.
Linked list can be Linear(Singly), Doubly or Circular linked list.
12. What are the different ways to allocate memory for arrays? Explain.
Ans:
Two possible arrangements
1. row major order
2. column major order
row major order
elements of an array are arranged sequentially row by row. Thus elements of first row occupies first set of
memory locations reserved for the array, elements of second row occupies the next set of memory and so on.
Eg: row major order A[2][3] 1 2 3
4 5 6
[0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
22fdfsva
av , m
1 2 3 4 5 6
Elements
Memory location 200 202 204 206 208 210
1 4 2 5 3 6
Elements
Memory location 200 202 204 206 208 210
Given a 2D array of size M x N.
Row major address = B + w * (N * (i-L1) + j-L2)
Column major address = B + w * (M * (j-L2) + i-L1)
B : Base address of the array
w : Size of each element of the array
L1 : lower bound of row
L2 : lower bound of column
Eg:
find location of A[4]=A[1][0]
Row major address = B + w * (N * (i-L1) + j-L2)
here,
B=200
w =2
L1=L2=0 N=3
A[1 ][0] location=200+2[3*(1-0)+(0-0)]
=200+ 6=206
Eg:
find location of A[4]=A[1][0]
Column major address = B + w * (M * (j-L2) + (i-L1))
here,
B=200
w =2
L1=L2=0 M=2
A[1 ][0] location=200+2[2(0-0)+(1-0)]
=200+ 2=202
Example : A+B
(A+B) * (C-D)
Prefix :
An expression is called the prefix expression if the operator appears in the expression before the operands.
Prefix notation is also known as Polish Notation.
Example :
infix prefix
A+B +AB
(A+B) * (C-D) *+AB-CD
Postfix :
An expression is called the postfix expression if the operator appears in the expression after the operands.
Postfix notation is also known as Reverse Polish Notation or Suffix.
Example :
infix postfix
A+B AB+
(A+B) * (C-D) AB+CD-*
MIDDLE
LAST
Insert first
2) Insert middle