Module 1 DSA - VTU
Module 1 DSA - VTU
MODULE 1
INTRODUCTION TO DATA STRUCTURES: Data Structures, Classifications (Primitive
& Non-Primitive), Data structure Operations Review of pointers and dynamic Memory
Allocation
ARRAYS and STRUCTURES: Arrays, Dynamic Allocated Arrays, Structures and Unions,
Polynomials, Sparse Matrices, representation of Multidimensional Arrays, Strings
STACKS: Stacks, Stacks Using Dynamic Arrays, Evaluation and conversion of Expressions
DATA STRUCTURE
Data structure is a representation of the logical relationships existing between individual
elements of data. A data structure is a way of organizing all data items that considers not only
the elements stored but also their relationship to each other.
The logical or mathematical model of a particular organization of data is called a data
structure.
VTU, Mysore 1
DATA STRUCTURES-BCS304 MODULE 1
Each record in a file may contain many field items but the value in a certain field may uniquely
determine the record in the file. Such a field K is called a primary key and the values k1, k2,
….. in such a field are called keys or key values.
Records may also be classified according to length.
A file can have fixed-length records or variable-length records.
• In fixed-length records, all the records contain the same data items with the same amount of
space assigned to each data item.
• In variable-length records file records may contain different lengths. Example: Student
records have variable lengths, since different students take different numbers of courses.
Variable-length records have a minimum and a maximum length. The above organization of
data into fields, records and files may not be complex enough to maintain and efficiently
process certain collections of data. For this reason, data are also organized into more complex
types of structures.
VTU, Mysore 2
DATA STRUCTURES-BCS304 MODULE 1
VTU, Mysore 3
DATA STRUCTURES-BCS304 MODULE 1
2. Destroy: The Destroy operation destroys the memory space allocated for the specified
data structure.
3. Selection: The Selection operation deals with accessing a particular data within a data
structure.
4. Updating: The Update operation updates or modifies the data in the data structure.
5. Searching: The Searching operation finds the presence of the desired data item in the
list of data items.
6. Sorting: Sorting is the process of arranging all the data items in the data structure in a
particular order, say for example, either in ascending order or in descending order.
7. Merging: Merging is a process of combing the data items of two different sorted list
into a single list.
However, it is more efficient to use one copy of 3.1459 and three pointers referencing a single
copy, since less space is required for a pointer when compared to floating point number. This
can be represented pictorially as shown below:
VTU, Mysore 4
DATA STRUCTURES-BCS304 MODULE 1
VTU, Mysore 5
DATA STRUCTURES-BCS304 MODULE 1
• If we frequently allocate the memory space, then it is better to define a macro as shown
below:
#define MALLOC(p,s)
if(!((p)==malloc(s)))
} printf("insufficient memory");
exit(0);
}
{
int temp=*p; //declares temp as an int and assigns to it the contents
of what p points to
VTU, Mysore 6
DATA STRUCTURES-BCS304 MODULE 1
ARRAYS
• An Array is defined as, an ordered set of similar data items. All the data items of an
array are stored in consecutive memory locations.
• The data items of an array are of same type and each data items can be accessed using
the same name but different index value.
• An array is a set of pairs, such that each index has a value associated with it. It can be
called as corresponding or a mapping
Ex: <index, value>
< 0 , 25 > list[0]=25
< 1 , 15 > list[1]=15
< 2 , 20 > list[2]=20
< 3 , 17 > list[3]=17
< 4 , 35 > list[4]=35
Here, list is the name of array. By using, list [0] to list [4] the data items in list can be accessed.
Structure Array is
objects: A set of pairs <index, value> where for each value of
index there is a value from the set item. Index is a finite
ordered set of one or more dimensions, for example,
{0, … , n-1} for one dimension,
{(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for
two dimensions, etc.
Functions:
for all A Array, i index, x item, j, size integer
Array Create(j, list) ::= return an array of j dimensions where
list is a j-tuple whose ith element is
the size of the ith dimension. Items are
undefined.
Item Retrieve(A, i) ::= if (i index)
return the item associated with
index value i in array A
else
return error
Array Store(A, i, x) ::= if (i in index)
return an array that is identical
to array A except the new pair
<i, x> has been inserted
else
return error
end array
VTU, Mysore 7
DATA STRUCTURES-BCS304 MODULE 1
ARRAYS IN C
• A one-dimensional array can be declared as follows:
int list[5]; //array of 5 integers
int *plist[5]; //array of 5 pointers to integers
• Compiler allocates 5 consecutive memory-locations for each of the variables 'list' and
'plist'.
• Address of first element list[0] is called base-address.
• Memory-address of list[i] can be computed by compiler as
+i*sizeof(int) where =base address
VTU, Mysore 8
DATA STRUCTURES-BCS304 MODULE 1
Program to print both address of ith element of given array & the value found at that
address:
void print1(int *ptr, int rows)
printf(“Address Contents\n”);
void main()
print1(&one[0], 5)
VTU, Mysore 9
DATA STRUCTURES-BCS304 MODULE 1
• The above code would allocate an array of exactly the required size and hence would not
result in any wastage.
VTU, Mysore 10
DATA STRUCTURES-BCS304 MODULE 1
CALLOC
• These functions → allocate user-specified amount of memory & → initialize the allocated
memory to 0.
• On successful memory-allocation, it returns a pointer to the start of the new block. On
failure, it returns the value NULL.
• Memory can be allocated using calloc as shown below:
int *p;
p=calloc(n, sizeof(int)); //where n=array size
• To create clean and readable programs, a CALLOC macro can be created as shown below:
#define CALLOC(p,n,s)
if((p=calloc(n,s))==NULL)
{
printf("insufficient memory");
exit(1);
}
REALLOC
• These functions resize memory previously allocated by either malloc or calloc. For
example,
realloc(p,s); //this changes the size of memory-block pointed at by p to s < oldSize, the
rightmost oldSize-s bytes of old block are freed..
• When s>oldSize, the additional s-oldSize have an unspecified value and when s
VTU, Mysore 11
DATA STRUCTURES-BCS304 MODULE 1
• On successful resizing, it returns a pointer to the start of the new block. On failure, it returns
the value NULL.
• To create clean and readable programs, the REALLOC macro can be created as shown
below:
#define REALLOC(p,s)
if((p=realloc(p,s))==NULL)
{
printf("insufficient memory");
exit(0);
}
strcpy(person.name,"james") ;
person.age
person.salary = 35000;
➢ We can create our own structure data types by using the typedef statement as below:
typedef struct human— typedef struct {
being { char name[10];
char name[10]; int age;
int age; -OR- float salary;
float salary; } human-being;
};
VTU, Mysore 12
DATA STRUCTURES-BCS304 MODULE 1
if (humans—equal(personl,person2))
printf("The two human beings are the same\n"); else
printf{"The two human beings are not the same\n");
typedef struct {
int month;
int day;
int year; } date;
➢ A person born on February 11, 1944, would have the values for the date struct set as:
personl.dob.month = 2;
personl.dob.day = 11; personl.dob.year = 1944;
VTU, Mysore 13
DATA STRUCTURES-BCS304 MODULE 1
Unions
➢ This is similar to a structure, but the fields of a union must share their memory space.
This means that only one field of the union is "active" at any given time.
union {
int children;
int beard ;
} u;
};
and
➢ we first place a value in the tag field. This allows us to determine which field in the union
is active. We then place a value in the appropriate field of the union.
VTU, Mysore 14
DATA STRUCTURES-BCS304 MODULE 1
Self-Referential Structures
• A self-referential structure is one in which one or more of its components is a pointer to
itself.
• These require dynamic storage management routines (malloc & free) to explicitly obtain
and release memory.
typedef struct list
{
char data;
list *link; //list is a pointer to a list structure
} ;
• Consider three structures and values assigned to their respective fields:
List item1,item2,item3;
item1.data='a';
item2.data='b';
item3.data='c';
item1.link=item2.link=item3.link=NULL;
• We can attach these structures together as follows
item1.link=&item2;
item2.link=&item3;
VTU, Mysore 15
DATA STRUCTURES-BCS304 MODULE 1
• A polynomial is a sum of terms, where each term has a form axe , where x=variable,
a=coefficient and e=exponent.
• For ex, A(x)=3x20+2x5+4 and B(x)=x4+10x3+3x2+1
• The largest(or leading) exponent of a polynomial is called its degree.
• Assume that we have 2 polynomials,
Structure Polynomial is
objects: p(x)=a1xe + . . anxe ; a set of ordered pairs of <ei,ai> where
ai in Coefficients and ei in Exponents, ei are integers >= 0
functions:
for all poly, poly1, poly2 Polynomial, coef Coefficients,
expon Exponents Polynomial Zero( ) ::= return the
polynomial, p(x) = 0
Boolean IsZero(poly) ::= if (poly)
return FALSE
else
return TRUE
VTU, Mysore 16
DATA STRUCTURES-BCS304 MODULE 1
polynomial a;
VTU, Mysore 17
DATA STRUCTURES-BCS304 MODULE 1
vtucode.in 18
DATA STRUCTURES-BCS304 MODULE 1
POLYNOMIAL ADDITION:
void padd (int starta, int finisha, int startb, int finishb,int *
startd, int *finishd)
{
/* add A(x) and B(x) to obtain D(x) */
float coefficient;
*startd = avail;
while (starta <= finisha && startb <= finishb)
{
switch (COMPARE(terms[starta].expon, terms[startb].expon))
{
case -1: /* a expon < b expon */
attach(terms[startb].coef,
terms[startb].expon); startb++
break;
vtucode.in 19
DATA STRUCTURES-BCS304 MODULE 1
ANALYSIS
• Let m and n be the number of non-zero terms in A and B respectively.
• If m>0 and n>0, the while loop is entered. At each iteration, we increment the value of startA or
startB or both.
• Since the iteration terminates when either startA or startB exceeds finishA or finishB respectively,
the number of iterations is bounded by m+n-1. This worst case occurs when A(x)=∑ x2i and
B(x)=∑x2i+1
• The asymptotic computing time of this algorithm is O(n+m)
vtucode.in 20
DATA STRUCTURES-BCS304 MODULE 1
Structure Sparse_Matrix is
objects: a set of triples, <row, column, value>, where row and column are
integers and form a unique combination, andvalue comes from the set
item.
functions:
else
return error
else
return error.
End Sparse_Matrix
vtucode.in 21
DATA STRUCTURES-BCS304 MODULE 1
vtucode.in 22
DATA STRUCTURES-BCS304 MODULE 1
TRANSPOSING A MATRIX
• To transpose a matrix ,we must interchange the rows and columns.
• Each element a[i][j] in the original matrix becomes element b[j][i] in the transpose matrix.
• Algorithm To transpose a matrix:
for each row i
take element<i,j,value> and store it
as element<i,j,value> of the transpose;
int n, i, j, currentb;
n = a[0].value; /* total number of elements */
b[0].row = a[0].col; /* rows in b = columns in a */
b[0].col = a[0].row; /*columns in b = rows in a */
b[0].value = n;
if (n > 0)
{ /*non zero matrix */
currentb = 1;
for (i = 0; i < a[0].col; i++) /* transpose by columns
in a */
for( j = 1; j <= n; j++) /* find elements from the
current column */
if (a[j].col == i)
{/* element is in current column, add it to b */
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++
}
}
}
vtucode.in 23
DATA STRUCTURES-BCS304 MODULE 1
fast_transpose(b, new_b);
vtucode.in 24
DATA STRUCTURES-BCS304 MODULE 1
void storesum(term d[ ], int *totald, int row, int column, int *sum)
{
/* if *sum != 0, then it along with its row and column position is
stored as the *totald+1 entry in d */
if (*sum)
if (*totald < MAX_TERMS)
{
d[++*totald].row = row;
d[*totald].col = column;
d[*totald].value = *sum;
}
else
{
fprintf(stderr, ”Numbers of terms in product exceed %d\n”,
MAX_TERMS); exit(1);
}
}
}
storesum function
vtucode.in 25
DATA STRUCTURES-BCS304 MODULE 1
The string, whose component elements are characters. As an ADT, we define a string to have
the form, S = So, .. . , where Si are characters taken from the character set of the programming
language. If n = 0, then S is an empty or null string.There are several useful operations we
could specify for strings.
we represent strings as character arrays terminated with the null character \0.
vtucode.in 26
DATA STRUCTURES-BCS304 MODULE 1
String insertion:
Assume that we have two strings, say string 1 and string 2, and that we want to insert string 2 into
string 1 starting at the ith position of string 1. We begin with the declarations:
vtucode.in 27
DATA STRUCTURES-BCS304 MODULE 1
Pattern Matching
Assume that we have two strings, string and pat, where pat is a pattern to be searched for in
string. The easiest way to determine if pat is in string is to use the built-in function strstr. If
we have the following declarations:
vtucode.in 28
DATA STRUCTURES-BCS304 MODULE 1
vtucode.in 29
DATA STRUCTURES-BCS304 MODULE 1
SYSTEM STACK
A stack used by a program at run-time to process function-calls is called system-stack.
• When functions are invoked, programs
→ create a stack-frame (or activation-record) &
→ place the stack-frame on top of system-stack
• Initially, stack-frame for invoked-function contains only
→ pointer to previous stack-frame &
→ return-address
• The previous stack-frame pointer points to the stack-frame of the invoking-function while
return-address contains the location of the statement to be executed after the function
terminates.
• If one function invokes another function, local variables and parameters of the invoking-
function are added to its stack-frame.
vtucode.in 30
DATA STRUCTURES-BCS304 MODULE 1
vtucode.in 31
DATA STRUCTURES-BCS304 MODULE 1
Stack ADT
• The following operations make a stack an ADT. For simplicity, assume the data is an integer
type.
vtucode.in 32
DATA STRUCTURES-BCS304 MODULE 1
• Associated with the array is a variable, top, which points to the top element in the stack.
Initially, top is set to -1 to denote an empty stack.
• we have specified that element is a structure that consists of only a key field.
1. CREATE STACK:
The element which is used to insert or delete is specified as a structure that consists of
only a key field.
1. Boolean IsEmpty(Stack)::= top < 0;
2. Boolean IsFull(Stack)::= top >= MAX_STACK_SIZE-1;
The IsEmpty and IsFull operations are simple, and is implemented directly in the
program push and pop functions. Each of these functions assumes that the variables
stack and top are global.
• Function push() checks to see if the stack is full. If it is, it calls stackFull, which prints an
error message and terminates execution.
• When the stack is not full, we increment top and assign item to stack[top].
vtucode.in 33
DATA STRUCTURES-BCS304 MODULE 1
For deletion, the stack-empty function should print an error message and return an item of
type element with a key field that contains an error code.
• Once the stack is full, realloc() function is used to increase the size of array.
• In array-doubling, we double array-capacity whenever it becomes necessary to increase the
capacity of an array.
vtucode.in 34
DATA STRUCTURES-BCS304 MODULE 1
ANALYSIS
• In worst case, the realloc function needs to
→ allocate 2*capacity*sizeof(*stack) bytes of memory and
→ copy capacity*sizeof(*stack) bytes of memory from the old array into the new one.
• The total time spent over all array doublings = O(2k ) where capacity=2k
• Since the total number of pushes is more than 2k-1 , the total time spend in array doubling
is O(n) where n=total number of pushes.
STACK APPLICATIONS: POLISH NOTATION
Expressions: It is sequence of operators and operands that reduces to a single value after
evaluation is called an expression.
X=a/b–c+d*e– a*c
In above expression contains operators (+, –, /, *) operands (a, b, c, d, e).
Expression can be represented in in different format such as
• Prefix Expression or Polish notation
• Infix Expression
• Postfix Expression or Reverse Polish notation
• Infix Expression: In this expression, the binary operator is placed in-between the
operand. The expression can be parenthesized or un- parenthesized.
Example: A + B
Here, A & B are operands and + is operand
• Prefix or Polish Expression: In this expression, the operator appears before its
operand.
Example: + A B
Here, A & B are operands and + is operand
• Postfix or Reverse Polish Expression: In this expression, the operator appears
after its operand.
Example: A B +
Here, A & B are operands and + is operand
vtucode.in 35
DATA STRUCTURES-BCS304 MODULE 1
The first answer is picked most because division is carried out before subtraction, and
multiplication before addition. If we wanted the second answer, write expression differently
using parentheses to change the order of evaluation
X= ((a / ( b – c + d ) ) * ( e – a ) * c
In C, there is a precedence hierarchy that determines the order in which operators are evaluated.
Below figure contains the precedence hierarchy for C.
vtucode.in 36
DATA STRUCTURES-BCS304 MODULE 1
• The operators are arranged from highest precedence to lowest. Operators with
highest precedence are evaluated first.
• The associativity column indicates how to evaluate operators with the same
precedence. For example, the multiplicative operators have left-to-right
associativity. This means that the expression a * b / c % d / e is equivalent to (
(((a*b)/c) %d)/e )
• Parentheses are used to override precedence, and expressions are always evaluated
from the innermost parenthesized expression first
INFIX TO POSTFIX CONVERSION
vtucode.in 37
DATA STRUCTURES-BCS304 MODULE 1
•
The analysis of the examples suggests a precedence-based scheme for stacking and
unstacking operators.
vtucode.in 38
DATA STRUCTURES-BCS304 MODULE 1
void postfix(void)
{
char
symbol;
precede
nce
token;
int n = 0,top = 0; /* place eos on
stack */ stack[0] = eos;
for (token = getToken(&symbol, &n); token != eos; token =
getToken(&symbol,& n ))
{
if (token == operand)
vtucode.in 39
DATA STRUCTURES-BCS304 MODULE 1
printf("%c",
symbol); else if
(token == rparen)
{
while (stack[top] !=
lparen)
printToken(p
op( ));
pop( );
}
else{
while(isp[stack[top]] >=
icp[token])
printToken(pop());
push(token);
}
}
vtucode.in 40
DATA STRUCTURES-BCS304 MODULE 1
vtucode.in 41
DATA STRUCTURES-BCS304 MODULE 1
vtucode.in 42