You are on page 1of 8

Data Structure: Array and Stack

Linear data Structures


Introduction:
Based on how data are stored in memory, data structures can be classified in to two types.

 Linear data structures


 Non-linear data structures.
In this unit we will see various linear data structures.
In linear data Structure, data are stored in contiguous (may not physically) memory locations.
Array, stack, queue, and list are example of linear data structures.

2.1 Array
An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by adding an index to a unique identifier. It is static, random
access data structure and elements are stored in physical contiguous locations.
Array can be 1-D, 2-D or n-D. Depending on language and platform, representation and
storage may vary. Let‟s see 1-D array and 2-D array in c language.

1-D array representation


In c language 1-D array can be declared by many ways.
1) Simple Declaration:
datatype name [size];
eg. int array[10];
Here, in this example, array of type integers is declared statically. Here it can store maximum
10 int value. Here all element of array will be zero or garbage as shown below.

Array 0 -111 0 245 0 -134 0 0 0 0

2) Value can be assigned along with array declaration as follow.


datatype name[size]={Elements separated by comma };
eg. int array[5]={1,2,3};
Here, values at first three locations are 1, 2 and 3 respectively. And at remaining two
locations, values are zero, as shown below.

array 1 2 3 0 0

By K.P. Kandoriya Page 1


Data Structure: Array and Stack

3) Array also can be declared without specifying size, as follow.


datatype name[]={Elements separated by comma }
Eg. int array[]={1,2,3,4,5};
Array 1 2 3 4 5

Here, for this array, size is n, it is number of value in between braces ({}). For above example
size is 5.

Operations on 1-D array


Following operations can be performed on 1-D array:
1. Insertion: In array, value at any location can be inserted using index.
For example one can insert element x at index i as in array A (must be declared first)
follow.
A[i]= X;

One can use iteration to insert all elements in sequence as follow.


Here n is size of array.
for(i=0;i<n; i++)
{
Read (x);
A[i]=x;
}
2. Traversal: Traversal of an array means, visit all index and display value in it.
One can use iteration to display all elements in sequence as follow. Here n is size of
array.
for (i=0;i<n; i++)
{
Write (A[i]);
}

3. Searching: In Searching, its checking that weather the given element „e‟ is exist in
array or not. Searching techniques are discussed in unit-6.
4. Merging: Merging means combining two arrays of size n and m, in to one array of
size n + m.
For example a[3]={1,2,3}, b[5]={4,5,6,7,8} can be merge in array c, of size 3+4=7 as
follow.
C[7]={1,2,3,4,5,6,7,8}, red elements are from a, and black are from b.
5. Deletion: Process of removal of element from index i, from the array is called
deletion. Element cannot be deleted physically from array because array is static data
structure. But you can use rare value to indicate deletion, i.e. it can be just ignored.
For example use -999 to indicate deletion. If value is -999, ignore it. But here if value
-999 is data then also, it will be ignored. So use a value that never going to be stored.
All above operations can be performed on 2-D array.

By K.P. Kandoriya Page 2


Data Structure: Array and Stack

2-D Array representation and storing


2-d array can be represented in tabular (matrix) form. And it can be declared as follow in c
language.
datatype name[row][colomn]
For example, int a[2][4]={1,2,3,4,5,6,7,8};

#of column=4

# of row=2 1 2 3 4
5 6 7 8

In This example, elements will be stored in contiguous physical locations as follows, let‟s
assume that base address is 1048.

Address 1048 1050 1052 1054 1054 1056 1058 1060


Value 1 2 3 4 5 6 7 8
Index a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]

Now let‟s, discuss platform independent concepts of array element storing


There are two ways to store elements in 2-D array.
1) Row major order
2) Column major order
In row major order elements of array are stored row by row. All the elements of first row
stored and then second row and so on. In example below, array is stored in row major order.

Address 1048 1050 1052 1054 1054 1056 1058 1060


Value 1 2 3 4 5 6 7 8
Index a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]

In column major order elements of array are stored column by column. All elements of first
column and then, all elements of second column, so on. For the same array, column major
order is shown below.

Address 1048 1050 1052 1054 1054 1056 1058 1060


Value 1 5 2 6 3 7 4 8
Index a[0][0] a[1][0] a[0][1] a[1][1] a[0][2] a[1][2] a[0][3] a[1][3]

By K.P. Kandoriya Page 3


Data Structure: Array and Stack

How to calculate address of particular index of array?


First address of an array is called base address (BA). Address of index in 2-D array depends
on how array is stored? i.e row major order or column major order.

Row major order


In 1-D array,
loc(a[i])=BA(a) + (i-lb)*c
Here a is an array stored in row major order, i is index, loc(a[i]) is address of index i and c is
size of an element and lb is lower bound for i, i.e. first index.
Example: Given a 1-D array a(0:10), stored in row major order with base address 2010, size
of each element is 2 byte. Find the address of a(9).
Here, lb=0, i=9, And c=2, so
Loc(a[9]) =B.A(a)+ (i-lb) * c Simply,

=2010 + (9-0) * 2 1. Multiply i, here it is 9 with c here


it is 2.
=2010 + 9 * 2
=18
=2010 + 18 2. Add it to base address.
2010+18=2028
=2028

In 2-D array,
loc (a[i][j])=BA(a) +{[(i-lb1)*(no of column)+(j-lb2)]*c}
Here a is array stored in row major order, i is row, j is column ([i, j] index), loc(a[i][j]) is
address of index, [ i, j] and c is size of an element, lb1 is lower bound for i, lb2 is lower
bound for j.
No of column = ub2-lb2+1, ub2 is maximum value for j, lb2 is minimum index of j.
Example: Given a 2-D array a(2:6,6:11), stored in row major order with base address 2000,
size of each element is 2 byte. Find the address of a(4,8).
Here, i=4, j=9, And c=2, lb1=2, lb2=6, ub1=6, ub2=9 so
Loc(a[2][3]) = BA(a) +{[(i-lb1)*(no of column)+(j-lb2)]*c}
=2000 +{[(4-2)*(11-6+1)+(9-6)]*2}
=2000 +{[2*6+3]*2}
=2000 +{[12+3]*2}
=2000 +{15*2}
=2000+30 =2030

By K.P. Kandoriya Page 4


Data Structure: Array and Stack

Column major order


In 2-D array,
loc(a[i][j])=BA(a) +{[(j-lb2)*(no of row)+(i-lb1)]*c}
Here a is array, i is row, j is column ([i, j] index), loc(a[i][j]) is address of index, [i, j] and c is
size of element, lb1 is lower bound for i and lb2 is lower bound for j.
No of row = ub1-lb1+1, ub1 is maximum value for i.
Example: Find the location/ address of int a [2,3], B.A(a)=2000, lb1=0, lb2=0, ub1=4, ub2=5.
Here, i=2, j=2, And c=2 (because size of int is 2 byte), so
Loc(a[2][3]) = BA(a) +{[(j-lb2)*(no of row)+(i-lb1)]*c}
=2000 +{[(3-0)*(4-0+1)+(2-0)]*2}
=2000 +{[3*5+2]*2}
=2000 +{[15+2]*2}
=2000 +{17*2}
=2000+34
=2034

Simply,

Draw matrix of size (ub1-lb1+1)*(ub2-lb2+1), Here it is 5*6.

a(0,0) a(0,1) a(0,2) a(0,3) a(0,4) a(0,5)


a(1,0) a(1,1) a(1,2) a(1,3) a(1,4) a(1,5)
a(2,0) a(2,1) a(2,2) a(2,3) a(2,4) a(2,5)
a(3,0) a(3,1) a(3,2) a(3,3) a(3,4) a(3,5)
a(4,0) a(4,1) a(4,2) a(4,3) a(4,4) a(4,5)
Then, count total no of element from first location to given location, in column major order. here
given location is a (2,3) so its 18. Then subtract 1. Multiply it by c. here (18-1)*2=34, add it to base
address, here 2010+30=2034

Operations on 2-d array: all operations for 1-D array are also applicable for 2-d array. In
addition to it 2-d array multiplication can be performed.
Applications of array: There are many application of array, viz. it can be used to represent
list of items. Here we will study one essential application of it that is parse matrix
representation.

By K.P. Kandoriya Page 5


Data Structure: Array and Stack

Sparse Matrix:
Generally 2-D, array used to represent matrix, so for n*m matrix we need at least enough
memory to store all entries that is m*n 2-d array. But what if there are many of entries are
zero, still we need m*n 2-D array.
Thus, if most of entries are zero in any matrix we call it sparse matrix. Simply in sparse
matrix at least m*n/2+1elements are zero. The basic idea to store sparse matrix is to store
only nonzero elements. So we can use different data structure to store sparse matrix. It yields
huge saving in memory requirements for matrix. One of these is array representation of
sparse matrix.
Upper right, lower left, upper left and lower right triangular matrixes are sparse matrixes.
Again unit matrix is sparse matrix.

0 1 2 0 0 0 1 2 0 0 0 0 1 0 0
0 0 3 1 0 0 3 0 0 0 0 1 0 1 0
0 0 0 2 3 0 0 0 0 0 2 3 0 0 1
Upper right lower left upper left lower right unit
So any sparse matrix can be represented by array.

Array/single linear list Representation of sparse matrix


We need to scan the nonzero elements of the sparse matrix in row-major order (i.e., scan the
rows left to right beginning with row 1 and picking up the nonzero elements) each nonzero
element is represented by a triple (row, column, value)the list of triples is stored in a 1D
array. It is shown in following example. Let‟s represent 12 in following matrix, it is at 2nd
row and 1st column so in the list row=2, column=1, and value=12.

0 0 0 List =
12 0 2 Row 2 2 3 3
0 0 0 Column 1 3 2 3
0 23 9 Value 12 2 23 9
Sparse Matrix
Stack: A stack is a particular kind of abstract data type or collection in which the principal
operations on the collection are the addition of an entity to the collection, known as push and
removal of an entity, known as pop. The relation between the push and pop operations is such
that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data structure, the last
element added to the structure must be the first one to be removed. Stack has only one end
called top of the stack. We can add/remove (push/pop) elements only from stack top. Initially
top=-1. To add element increase top and insert at top. To remove acquire element and
decrease top.

Array representation of stack


Stack can be declared using array representation as follow
data_type stackname [size];
for example,

By K.P. Kandoriya Page 6


Data Structure: Array and Stack

int STACK [4];


lets see diagrammatically adding and removal of element on the stack.
11 T=4
9 T=3 9 9 T=3
6 T=2 6 6 6 6 T=2
5 T=1 5 5 5 5 5 5 T=1
T=-1 add 5 add 6 add 9 add 11(full) pop pop pop pop, t=-1

Once your stack is full, you cannot add element to stack. Similarly when you stack is empty, there bo element
to remove so you cannot pop.

Basic operation on the stack

1.Push: You can insert element on the stack if there is room. First increase top and insert at
top. This operation is called push.

Algorithm push: This algorithm inserts an element (e) to stack(s) of size n, at top (t).
Input: s[],t, n, e
Output: Stack with one addition element and updated top.

if(t>n-1) then
write("Stack is full");
exit;

else
s[++t]=e;
end if

2.Pop: You can remove/delete element from stack top. Acquire element and then decrease
the top. this operation is called pop.

Algorithm pop: This algorithm deletes an element (e) from stack(s) of size n, from the
top (t).
Input: s[],t, n
Output: Stack with one less element and updated top and e.

if(t==-1) then
write(“stack is Empty”);
exit;
else
t=t-1;
return(s[t + 1]);
end if

Algorithm Display: This algorithm displays all element of stack.


Input: s[],t
Output: content of stack

Step 1: [initialization]
Index=0;
step 3:repeat step 3 until index<n-1

By K.P. Kandoriya Page 7


Data Structure: Array and Stack

Step 2:[display data and prepare for next element]


write(s[i]);
index++;

Algorithm peep: This algorithm return element from specified index from the top.
Input: s[],t, index
Output: e from given index

if(t-index)<0 then
write("underflow on peep");
return;

else

return(s[(t-index)]);
end if

Algorithm change: This algorithm update value at specified index from the top.
Input: s[],t, index, e
Output: update index at index
if(t-index<0) then
printf("underflow");
else
s[t-index]=e;
end if

By K.P. Kandoriya Page 8

You might also like