Data Structures-Bcs304 Module 1 1
Data Structures-Bcs304 Module 1 1
MODULE 1
& Non-Primitive), Data structure Operations Review of pointers and dynamic Memory
Allocation
ARRAYS and STRUCTURES: Arrays, Dynamic Allocated Arrays, Structures and Unions,
STACKS: Stacks, Stacks Using Dynamic Arrays, Evaluation and conversion of Expressions
DATA STRUCTURE
elements of data. A data structure is a way of organizing all data items that considers not only
structure.
1. It must be rich enough in structure to mirror the actual relationships of the data in the real
world.
2. The structure should be simple enough that one can effectively process the data whenever
necessary.
BASIC TERMINOLOGY
Data items: Data items refers to a single unit of values. Data items that are divided into subitems are called
Group items. Ex: An Employee Name may be divided into three subitemsfirst name, middle name, and last
name. Data items that are not able to divide into sub-items
Entity: An entity is something that has certain attributes or properties which may be assigned
values. The values may be either numeric or non-numeric. Ex: Attributes- Names, Age, Sex,
SSN Values- Rohland Gail, 34, F, 134-34-5533 Entities with similar attributes form an entity
set. Each attribute of an entity set has a range of values, the set of all possible values that
could be assigned to the particular attribute. The term “information” is sometimes used for
data with given attributes, of, in other words meaningful or processed data.
vtucode.in
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.
• In fixed-length records, all the records contain the same data items with the same amount of
• 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.
vtucode.in
These are basic data structures and are directly operated upon by the machine instructions.
These data types consists of characters that cannot be divided and hence they also called
These are derived from the primitive data structures. The non-primitive data structures
furtherclassified into
A data structure is said to be linear if its elements form a sequence or a linear list. There are
1. One way is to have the linear relationships between the elements represented by means of
2. The other way is to have the linear relationship between the elements represented by means
The common examples of linear data structure are Arrays, Queues, Stacks, Linked lists
A data structure is said to be non-linear if the data are not arranged in sequence or a linear.
The insertion and deletion of data is not possible in linear fashion. This structure is mainly
used to represent data containing a hierarchical relationship between elements. Trees and
1. Create: The Create operation results in reserving memory for the program elements.
The creation of data structures may take place either during compile time or during
run time.
vtucode.in
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
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
traversing strings, lookup tables, control tables and tree structures. In particular, it is often
much cheaper in time and space to copy and dereference pointers than it is to copy and access
the data to which the pointers point. Pointers are also used to hold the addresses of entry
points for called subroutines in procedural programming and for run-time linking to dynamic
Pointer: A pointer is a special variable which contains address of a memory location. Using
this pointer, the data can be accessed. For example, assume that a program contains four
occurrences of a constant 3.1459. During the compilation process, four copies of 3.1459 can
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
vtucode.in
type* name;
where type represent the type to which pointer thinks it is pointing to.
Intialization:
variable_type *pointer_name = 0;
• If memory allocation succeeds, then address of first byte of allocated space is returned. If
malloc() or calloc().
int i,*pi;
pi=(int*)malloc(sizeof(int));
*pi=1024;
free(pi);
vtucode.in
• 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);
MALLOC(pf,sizeof(float))
DANGLING REFERENCE
• Whenever all pointers to a dynamically allocated area of storage are lost, the storage is lost
1) Set all pointers to NULL when they are not actually pointing to an object. This makes
sure that you will not attempt to access an area of memory that is either
3) Pointers have same size as data type 'int'. Since int is the default type specifier, some
programmers omit return type when defining a function. The return type defaults to
‘int’ which can later be interpreted as a pointer. Therefore, programmer has to define
int temp=*p;
*p=*q;
*q=temp;
of what p points to
//stores what q points to into the location where p
points
vtucode.in
ALGORITHM SPECIFICATION
1. Input: There are zero or more quantities that are externally supplied.
4. Finiteness: If we trace out the instructions of an algorithm, then for all cases, the
by a person using only pencil and paper. It is not enough that each operation be
for(i=0;i<n;i++)
and list[min];
}
Assume that we have n > 1 distinct integers that are already sorted and stored in the array list.
Since the list is sorted we may use the following method to search for the value. Let left and
right, respectively, denote the left and right ends of the list to be searched. Initially, left = 0
vtucode.in
and right = n-l. Let middle = (left+right)/2 be the middle position in the list. If we compare
denote left and right as left and right ends of the list to
searchnum
1) searchnum <
list[middle] set
right to middle-1
2) searchnum =
list[middle] return
middle
3) searchnum >
list[middle] set
left to middle+1
if searchnum has not been found and there are more integers to
possible permutations of
this set
vtucode.in
9
RECURSIVE ALGORITHMS
easy to understand.
turn calls another function and so on and eventually calls the first function.
For example:
1. FACTORIAL
“The product of the positive integers from 1 to n, is called "n factorial" and is denoted by
It is also convenient to define 0! = 1, so that the function is defined for all nonnegative
integers.
a) If n = 0, then n! = 1.
Observe that this definition of n! is recursive, since it refers to itself when it uses (n - 1)!
(a) The value of n! is explicitly given when n = 0 (thus 0 is the base value)
(b) The value of n! for arbitrary n is defined in terms of a smaller value of n which is
Using for loop: This procedure evaluates N! using an iterative loop process
This procedure calculates N! and returns the value in the variable FACT.
{{{
}}}
vtucode.in
10
[End of loop.]
Return.
Using recursive function: This is a recursive procedure, since it contains a call to itself
This procedure calculates N! and returns the value in the variable FACT.
Return.
if(n==0)
return 1;
return n*fact(n-1);
2. BINARY SEARCH:
To transform function into a recursive one, we must
(1) establish boundary conditions that terminate the recursive calls, and
(2) implement the recursive calls so that each call brings us one step closer to a solution
int middle;
if (left<= right)
switch(compare(list[middle], searchnum))
return -1;
return 1;
vtucode.in
3. PERMUTATIONS:
Given a set of n > 1 elements, print out all possible permutations of this set. For example, if
the set is (a, b. c), then the set of permutations is {(a, b, c), (a, c, b), (b, a, c), (b, c, d), (c, a,
b),(c, b, a)}.
It is easy to see that, given n elements, there are n! permutations. We can obtain a simple
algorithm for generating the permutations if we look at the set (a, b, c, d). We can construct
The clue to the recursive solution is the phrase "followed by all permutations." It implies
that we can solve the problem for a set with n elements if we have an algorithm that
works on n - 1 elements. We assume that list is a character array. Notice that it recursively
“);
else
for(j=i;j<=n;j++)
{
SWAP(list[i],list[j],temp); perm(list,i+1,n);
SWAP(list[i],list[j],temp);
vtucode.in
12
4.TOWER OF HANOI
Problem description
Suppose three pegs, labeled A, Band C, are given, and suppose on peg A a finite number
The objective of the game is to move the disks from peg A to peg C using peg B as an
1. Only one disk may be moved at a time. Only the top disk on any peg may be
moved to
We write A→B to denote the instruction "Move top disk from peg A to peg B"
vtucode.in
13
In other words,
The Towers of Hanoi problem for n > 1 disks may be reduced to the following subproblems:
• TOWER (N, BEG, AUX, END) to denote a procedure which moves the top n
disks from the initial peg BEG to the final peg END using the peg AUX as an
auxiliary.
TOWER (1, BEG, AUX, END) consists of the single instruction BEG→END
• When n > 1, the solution may be reduced to the solution of the following
{
if (n > 1)
Hanoi(n-1,x,z,y);
Hanoi(n-1,y,x,z);
else
vtucode.in
14
vtucode.in
15
ARRAYS
• An Array is defined as, an ordered set of similar data items. All the data items of an
• The data items of an array are of same type and each data items can be accessed using
• An array is a set of pairs, such that each index has a value associated with it. It can be
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
{(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for
Functions:
undefined.
else
return error
else
return error
end array
vtucode.in
16
int i;
input[i] = i;
int i;
float tempsum = 0;
tempsum += list[i];
return tempsum;
}
Program to find sum of n numbers
ARRAYS IN C
• Compiler allocates 5 consecutive memory-locations for each of the variables 'list' and
'plist'.
Program to print both address of ith element of given array & the value found at that
address:
vtucode.in
17
*/ int i;
printf(“Address Contents\n”);
printf(“\n”);
void main()
{
int one[] = {0, 1, 2, 3, 4};
print1(&one[0], 5)
vtucode.in
18
• When writing programs, sometimes we cannot reliably determine how large an array must
be.
int i,n,*list;
scanf("%d",&n);
if(n<1)
printf("improper value");
exit(0);
MALLOC(list, n*sizeof(int));
• The above code would allocate an array of exactly the required size and hence would not
vtucode.in
19
**array;
if(array == NULL)
or return
if(array[i] == NULL)
or return
}
Prg: Dynamically create a two-dimensional array
CALLOC
• These functions → allocate user-specified amount of memory & → initialize the allocated
memory to 0.
int *p;
• 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
• When s>oldSize, the additional s-oldSize have an unspecified value and when s
vtucode.in
20
• On successful resizing, it returns a pointer to the start of the new block. On failure, it returns
below:
#define REALLOC(p,s)
if((p=realloc(p,s))==NULL)
printf("insufficient memory");
exit(0);
Structures
Arrays are collections of data of the same type. In C there is an alternate way of grouping data
that permits the data to vary in type. This mechanism is called the struct, short for structure. A
structure (called a record in many other programming languages) is a collection of data items,
struct {
char name[10];
int age;
float salary;
} person;
➢ Creates a variable whose name is person and that has three fields:
strcpy(person.name,"james") ;
person.age
person.salary = 35000;
➢ We can create our own structure data types by using the typedef statement as below:
being {
char name[10];
int age;
float salary;
};
-ORtypedef struct {
char name[10];
int age;
float salary;
} human-being;
vtucode.in
21
humanBeing person1,person2;
➢ Structures cannot be directly checked for equality or inequality. So, we can write a function to
do this.
human—being person2)
return TRUE;
if (humans—equal(personl,person2))
typedef struct {
int month;
int day;
char name[10];
int age;
float salary;
date dob;
};
➢ A person born on February 11, 1944, would have the values for the date struct set as:
personl.dob.month = 2;
vtucode.in
22
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.
} sex;
union {
int children;
int beard ;
} u;
};
char name[10];
int age;
float salary;
date dob;
sex—type sex—info;
};
personl.sex—info.sex = male;
personl.sex—info.u.beard = FALSE;
and
person2.sex—info.sex = female;
person2.sex—info.u.children - 4;
➢ we first place a value in the tag field. This allows us to determine which field in the union
• The size of an object of a struct or union type is the amount of storage necessary to
represent the largest component, including any padding that may be required.
• Structures must begin and end on the same type of memory boundary, for example, an
even byte boundary or an address that is a multiple of 4, 8, or 16.
vtucode.in
23
Self-Referential Structures
itself.
• These require dynamic storage management routines (malloc & free) to explicitly obtain
char data;
};
List item1,item2,item3;
item1.data='a';
item2.data='b';
item3.data='c';
item1.link=item2.link=item3.link=NULL;
item1.link=&item2;
item2.link=&item3;
vtucode.in
24
Structure Polynomial is
objects: p(x)=a1xe + . . anxe ; a set of ordered pairs of <ei,ai> where
functions:
polynomial, p(x) = 0
return FALSE
else
return TRUE
else
return Zero
return error
else
inserted
deleted
else
return error
Polynomial SingleMult(poly, coef, expon) ::=
End Polynomia
, where x=variable,
A(x)= ∑ai x
vtucode.in
25
typedef struct
int degree;
float coef[MAX_DEGREE];
}polynomial;
polynomial a;
d = Zero( )
do
b = Remove(b, Lead_Exp(b));
break;
if (sum)
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
break;
a = Remove(a, Lead_Exp(a));
i
can be represented as:
a.degree=n
a.coeff[i]=an-i
a.coeff[i]=0
vtucode.in
26
a.degree<<MAX_DEGREE and polynomial is sparse, then we will not need most of the
float coef;
int expon;
}polynomial;
polynomial terms[MAX_TERMS];
int avail=0;
• startA & startB give the index of first term of A and B respectively . finishA & finishB give
the index of the last term of A & B respectively avail gives the index of next free location in
the array.
• Any polynomial A that has ‘n’ non-zero terms has startA & finishA such that
finishA=startA+n-1
• Advantage: This representation solves the problem of many 0 terms since A(x)-2x1000+1
uses only 6 units of storage (one for startA, one for finishA, 2 for the coefficients and 2 for
the exponents)
• Disadvantage: However, when all the terms are non-zero, the current representation requires
vtucode.in
27
POLYNOMIAL ADDITION:
void padd (int starta, int finisha, int startb, int finishb,int *
float coefficient;
*startd = avail;
attach(terms[startb].coef,
terms[startb].expon); startb++
break;
terms[startb].coef; if (coefficient)
starta++;
startb++; break;
attach(terms[starta].coef,
terms[starta].expon); starta++;
attach(terms[starta].coef,
terms[starta].expon);
attach(terms[startb].coef,
terms[startb].expon);
terms[avail].coef = coefficient;
terms[avail++].expon = exponent;
vtucode.in
28
ANALYSIS
• 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
vtucode.in
29
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
• We can classify uniquely any element within a matrix by using the triple . Therefore, we
• When a sparse matrix is represented as a 2-dimensional array, we waste space For ex, if
100*100 matrix contains only 100 entries then we waste 9900 out of 10000 memory spaces.
30
int col;
int row;
int value;
} term;
term a[MAX_TERMS];
• We can classify uniquely any element within a matrix by using the triple <row,col,value>.
vtucode.in
31
TRANSPOSING A MATRIX
• Each element a[i][j] in the original matrix becomes element b[j][i] in the transpose matrix.
place element<i,j,value> in
element<i,j,value>
int n, i, j, currentb;
b[0].value = n;
if (n > 0)
currentb = 1;
in a */
current column */
if (a[j].col == i)
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++
}
}
vtucode.in
32
int new_b[MAX_TERMS][3];
if (cols_a != b[0].row)
exit (1);
fast_transpose(b, new_b);
new_b[totalb+1].row = cols_b;
new_b[totalb+1].col = 0;
column = new_b[1].row;
i = row_begin;
column =new_b[j].row
else
and b */
row_begin = i;
row = a[i].row;
d[0].row = rows_a;
d[0].col = cols_b;
d[0].value = totald;
vtucode.in
DATA STRUCTURES-BCS304 MODULE 1
33
void storesum(term d[ ], int *totald, int row, int column, int *sum)
if (*sum)
d[++*totald].row = row;
d[*totald].col = column;
d[*totald].value = *sum;
else
MAX_TERMS); exit(1);
storesum function
vtucode.in
34
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
we represent strings as character arrays terminated with the null character \0.
vtucode.in
35
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
36
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
If pat is in string, t holds a pointer to the start of pat in string. The entire string beginning at
Although strstr seems ideally suited to pattern matching, there are two reasons why we may
• The function strstr is new to ANSI C. Therefore, it may not be available with the
• There are several different methods for implementing a pattern matching function.
The easiest but least efficient method sequentially examines each character of the
string until it finds the pattern or it reaches the end of the string. If pat is not in string,
this method has a computing time of O(n . m) where n is the length of pat and w is the
length of string. We can do much better than this, if we create our own pattern
matching function.