Data Structures Using C
Data Structure
⚫ A data structure is a particular way of storing and
organizing data in a computer so that it can be used
efficiently.
⚫ Data Structure is a group of memory locations used to represent the information used by the algorithm.
⚫ It is a way to store and organize data in the structured manner. Computer's memory is divided into small parts, we use different data structures to store data in those small blocks. Example: Arrays, Linked list, Trees ,etc.
Types of Data Structures
⚫ Linear Data Structures: A data structure whose
elements form a sequence, and every element in the structures has a unique predecessor and unique
successor.
⚫ Linear data structure is linear if element is adjacent to each other. It has exactly two neighbors elements to which it is connected as its previous and next member.
⚫
⚫
Non Linear Data Structures: A data
structure whose elements do not form a
sequence, and there is no predecessor
and unique successor.
⚫
Non-Linear data structure is that if one
element can be connected to more than
two adjacent element then it is known as
non-linear data structure.
Arrays
⚫ Array is basic data structure in which we store data in continues memory locations.
⚫ Array is variable which holds multiple elements of same type.
⚫ Generic form of arrays:
⚫ data type array_name[size];
⚫ Data type: what type of data(int, float, char...) Size: number elements you want to store.
⚫ Every element will have index value
⚫ If you want to access any element we must use index value of the element.
Stack
⦿ New nodes can be added and removed only at the top ⦿ Similar to a pile of dishes
⦿ Last-in, first-out (LIFO) ⦿ Eg: A stack of books
⦿ When a person wear bangles the last bangle worn is the first one to be removed and the first bangle would be the last to be removed.
Queue
⚫ First-in, first-out (FIFO).
⚫ Similar to a supermarket checkout line ⚫ Nodes are removed only from the head. ⚫ Nodes are inserted only at the tail.
⚫ The bullet in a machine gun..(you cannot fire 2 bullets at the same time).
Linked Lists
⚫
It is a list or chain of items where each
item points to the next one in the list.
⚫
Each item in a linked list is called a node.
⚫
Each node contains the data and also the
location to the next item.
START
bat • cat • sat • vat NULL
Trees
The data structure which reflects a hierarchical
relationship between various elements is
Graph
⚫ A graph G = (V,E) is composed of: V: set of vertices
E: set of edges connecting the vertices in V ⚫ An edge e = (u,v) is a pair of vertices
⚫ Example: V= {a,b,c,d,e} ⚫ E= {(a,b),(a,c),(a,d), ⚫ (b,e),(c,d),(c,e), ⚫ (d,e)} a b d e c
Operations on data structures
⚫
Following operations can be performed
on the data structures:
⚫
1. Traversing
⚫2. Searching
⚫3. Inserting
⚫4. Deleting
⚫5. Sorting
⚫6. Merging
⚫
1. Traversing- It is used to access
each data item exactly once so that it
can be processed.
⚫
2. Searching- It is used to find out the
location of the data item if it exists in
the given collection of data items.
⚫
3. Inserting- It is used to add a new
data item in the given collection of
data items.
⚫
4. Deleting- It is used to delete an
existing data item from the given
collection of data items.
⚫
5. Sorting- It is used to arrange the data
items in some order i.e. in ascending or
descending order in case of numerical
data and in dictionary order in case of
alphanumeric data.
⚫
6. Merging- It is used to combine the data
items of two
sorted files
into single file in
the
sorted form
.
Address Calculation in Linear
Arrays
⚫
A linear array is a list of a finite number
‘n’ of homogeneous data elements.
⚫
(a) The elements of the array are
referenced respectively by an index set
consisting of ‘n’ consecutive numbers.
⚫
(b) The elements of the array are
referenced respectively by an ‘index set’
consisting of ‘n’ consecutive numbers.
⚫
n- length/ size if index set consists of
1,2,3…n
⚫
Length= UB-LB+1
⚫
UB= largest index
⚫
LB=lowest index
⚫
Let ‘LA’ be liner array
⚫
K- index
⚫
LOC(LA[k])- Address of the element
LA[k] of the array LA.
Eg
⚫
Consider the array AUTO which records
the number of automobiles sold each year
from 1932 through 1984. given
Base(AUTO)=200, w=4. Find the address
of the array element for the year 1965.
⚫ LOC(AUTO[1965]=Base(AUTO)+w(1965-LB)
⚫
Consider a linear array A(5:50),
B(-5,10) and C(18)
⚫
(i) find no of elements in each array
⚫
(ii) Base(A)=300,w=4bytes for A. Find
address of A[15], A[40] and A[55]
⚫
Sol:
⚫Length= ub-lb+1
⚫Length(A)=50-5+1=46
⚫Length(B)=10-(-5)+1=16
⚫Length(C)= 18-1+1=18
⚫(ii)
⚫Loc(A[k])=base(A)+w(k-LB)
⚫loc(A[15) = 300+4(15-5)= 340
⚫
Loc(a[40])= 300+4(40-5)=440
⚫
Loc(A[55])= not an element of A, since
55 exceeds UB=50
Two Dimensional Array
⚫ Column major order- subscript – (1,1)(2,1)(3,1)- 1 column
⚫ Row major order-(1,1)(1,2)(1,3)- 1 row ⚫ m*n array
⚫ row major
⚫ LOC(A[i,j]=Base(A)+w[n(i-lower bound of row index)+(j-lower bound of column index)] column major
⚫ LOC(A[i,j]=Base(A)+w[m(j-lower bound of
column index)+(i- lower bound of row index)] ⚫ Normally we take lower bound index as 1
⚫ Each element of an array data[20][50] requires 4 bytes of storage. Base address is 2000. Determine the location of data [10][10] when the array is
stored as (i) row major
⚫ (ii) column major
⚫ Soln“:
⚫ Here data[20][50] so m=20, n=50
⚫ w=4bytes
⚫ We want to find data[10][10], so i=10 and j=10
⚫ (i) loc(data[10][10])= base+w(n(i-1)+(j-1))
⚫ =2000+4[50*(10-1)+(10-1))=3836
⚫ (ii) loc(data[10][10])= base+w(m(j-1)+(i-1))
⚫ A 2D-array defined as A[4...7][-1...3] requires 2 bytes of storage space. Calculate address of element at location A[6,2]. Given base address is 100.
⚫ (i) row major
⚫ (ii) column major
⚫ (iii) total no of elements in A ⚫ solun
⚫ L1=4, U1=7 ⚫ L2= -1, U2=3
m=No of rows=length of row= u1-l1+1=7-4+1= 4 n=No of column=length of column
(i)loc(a[6,2])=Base+w[n*(i-L1)+ (j- L2)]
⚫
=100+2(5*(6- 4)+(2-(-1)))= 126
⚫
(ii) loc(a[6,2])=Base+w[m*(j-L2)+ (i- L1)]
⚫
=100+2(4*(2-(-1))+(6-4))=128
General Multidimensional Arrays
⚫ Li- length of ith dimension⚫ Li=UB-LB+1 ⚫ Ei=Ki-LB ⚫ Ei=>Effective Index ⚫ Row major ⚫ LOC(C[K1,k2…Kn])=base(C)+w((E1L2+E2)L3+E3)L4+… +En-1)Ln+En) ⚫ Column major ⚫ Base(c)+w((EnLn-1+En-1)Ln-2)+…+E3)L2+E2)L1+E1) ⚫ n is dimension
Example 3D array row major
Q Suppose a 3d array MAZE is declared using
MAZE(2:8, -4:1, 6: 10), base(MAZE)=200, w=4. find loc of MAZE[5,-1,8]
⦿ L1=8-2+1=7, L2=1-(-4)+1=6, L3=10-6+1=5
⦿ Total elements in maze= li*l2*l3=7*6*5=210
⦿ Ei=ki-LBi
⦿ E1=5-2=3, E2=-1-(-4)=3, E3=8-6=2
⦿ Row major = (E1L2+E2)L3+E3
⦿ E1L2=3.6=18
⦿ E1L2+E2=18+3=21
⚫
(E1L2+E2)L3+E3=105+2=107
⚫ LOC(MAZE[5,-1,8])=200+4(107)=200+428
Searching
⚫
There are basically two types of
searching
⚫
Linear Search
⚫
Binary search
⚫
A Linear/sequential search of a
list/array begins at the beginning of the
list/array and continues until the item is
found or the entire list/array has been
searched
Linear Search
void LinSearch(double A[ ], int n, int no) {
for(int i=0;i<n;i++) {
if(A[i]==no)
{ Printf(“number searched %d”,A[i]); Printf(“location is %d”, i);
exit(0); }
else
{ printf(“Number not found”); }
} }
Binary Search
◦ Binary search algorithm assumes that the items in the array being searched are sorted
◦ The algorithm begins at the middle of the array in a binary search
◦ If the item for which we are searching is less than
the item in the middle, we know that the item
won’t be in the second half of the array
◦ Once again we examine the “middle” element
◦ The process continues with each comparison
cutting in half the portion of the array where the item might be
Binary Search
⚫ Start=0, end=n-1;
⚫ Mid=(start+end)/2;
⚫ While(data!=A[mid] && start<=end)
⚫ { ⚫ if(data>arr[mid]) ⚫ {Start=mid+1;} ⚫ Else ⚫ {End=mid-1;} ⚫ Mid=(start+end)/2; ⚫ } ⚫ If(data==A[mid])
⚫ {Printf(“searched element is %d at posn %d”,data,mid+1);}
⚫ If(start>end)
⚫ {Printf(“element not found”);
Complexity Analysis of
Searching
⚫