Unit-1 - Data Structures Using C
Unit-1 - Data Structures Using C
Data Data are simply values or set of values. Regno, Name, DOB, Address.
Group Item Data items that are divided into sub-items are called group items. (Not an elementary
items). DOB, Address
Entity An entity is something that has certain attributes or properties which may be
assigned values. Person, Place, organizations.
Attributes Each entity has a set of properties. The attributes can have numeric of alphanumeric
values. Regno, Name, DOB, Address.
Entity Set Entities with similar attributes form an entity set. All the employees in an organization
Fixed–Length All the records contain the same data items with the same amount of space assigned
Records to each data item.
Variable- File records may contain different lengths.
Length Record
File A file is the collection of records of the entities in a given entity set.
DATA STRUCTURES
The study of the data structures, which form the subject matter of this text, includes the following
three steps:
1
Data Structures using C Unit - I
DATA STRUCTURES
character Graphs
Linked List
Boolean Stack
Queue
Advantage of modularity:
When dealing with detail of each module in isolation (ignoring details of other modules).
When dealing with overall characteristics of all modules and their relationships in order to
integrate them into the system.
A system consists of components, which have components of their own. Indeed a system is a
hierarchy of components. The highest level component corresponds to the total system. To
design such hierarchies there are two possible approaches;
2
Data Structures using C Unit - I
1. Top-down approach
2. Bottom-up approach
TOP-DOWN APPROACH
Start with the topmost module and incrementally add modules that it calls.
In each step, design is refined into most concrete level until we reach the level where no
more refinement is needed and the design can be implemented directly.
BOTTOM-UP APPROACH
Starting from the very bottom, the operations that provide a layer of abstraction are
implemented.
The operations of this layer are then used to implement more powerful operations and
still higher layer of abstraction, until the stage is reached where the operations supported
by the layer are those desired by the system.
COMPLEXITY
SPACE COMPLEXITY
When we design an algorithm to solve a problem, it needs some computer memory to complete
its execution. For any algorithm, memory is required for the following purposes...
Generally, when a program is under execution it uses the computer memory for THREE reasons.
They are as follows...
3. Data Space: It is the amount of memory used to store all the variables and constants.
3
Data Structures using C Unit - I
To calculate the space complexity, we must know the memory required to store different data
type values (according to the compiler). For example, the C Programming Language compiler
requires the following...
int square(int a)
return a*a;
In above piece of code, it requires 2 bytes of memory to store variable 'a' and another 2 bytes of
memory is used for return value.
If any algorithm requires a fixed amount of space for all input values then that space
complexity is said to be Constant Space Complexity
int sum = 0, i;
return sum;
That means, totally it requires '2n+8' bytes of memory to complete its execution. Here, the
amount of memory depends on the input value of 'n'.
If the amount of space required by an algorithm is increased with the increase of input
value, then that space complexity is said to be Linear Space Complexity
4
Data Structures using C Unit - I
TIME COMPLEXITY
Every algorithm requires some amount of computer time to execute its instruction to perform the
task. This computer time required is called time complexity.
The time complexity of an algorithm is the total amount of time required by an algorithm
to complete its execution.
4. The time it takes to perform Arithmetic operations, logical operations, return value
and assignment operations etc.,
5. Input data
Calculating Time Complexity of an algorithm based on the system configuration is a very difficult
task because, the configuration changes from one system to another system. To solve this
problem, we must assume a model machine with specific configuration. So that, we can able to
calculate generalized time complexity according to that model machine.
To calculate time complexity of an algorithm, we need to define a model machine. Let us assume
a machine with following configuration...
Now, we calculate the time complexity of following example code by using the above defined
model machine...
Example 1: Consider the following piece of code...
return a+b;
In above sample code, it requires 1 unit of time to calculate a+b and 1 unit of time to return the
value. That means, totally it takes 2 units of time to complete its execution. And it does not
5
Data Structures using C Unit - I
change based on the input values of a and b. That means for all input values, it requires same
amount of time i.e. 2 units.
If any program requires fixed amount of time for all input values then its time complexity
is said to be Constant Time Complexity.
int sum = 0, i;
return sum;
Totally it takes '4n+4' units of time to complete its execution and it are Linear Time Complexity.
If the amount of time required by an algorithm is increased with the increase of input
value then that time complexity is said to be Linear Time Complexity
Big - Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity.
That means Big - Oh notation always indicates the maximum time required by an algorithm for all
input values. That means Big - Oh notation describes the worst case of an algorithm time
complexity.
6
Data Structures using C Unit - I
Consider function f(n) the time complexity of an algorithm and g(n) is the most
significant term. If f(n) <= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can
represent f(n) as O(g(n)).
f(n) = O(g(n))
Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time
required is on Y-Axis
In above graph after a particular input value n0, always C g(n) is greater than f(n) which indicates the
algorithm's upper bound.
Example : Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C x g(n) for all values of C >
0 and n0>= 1
7
Data Structures using C Unit - I
ARRAYS
An array is a collection of similar data type value in a single variable. It is a derived data
type in C, which is constructed from fundamental data type of C language.
Index Each location of an element in an array has a numerical index which is used
to identify the element.
Lower The lowest index of an array is called lower bound.
bound
Upper bound The highest index of an array is called upper bound.
Example:
int array[10]={35, 33, 42, 10, 14, 19, 27, 44, 26, 31};
As per above shown illustration, following are the important points to be considered.
CHARACTERISTICS OF ARRAYS
3) Two-dimensional array elements are stored row by row in subsequent memory locations.
5) Array size should be mentioned in the declaration. Array size must be a constant
expression and not a variable.
ONE-DIMENSIONAL ARRAY
A one-dimensional array is the simplest form of an array. The array is given a name and its
elements are referred by the subscripts or indices. A one-dimensional array is used to store a
large number of items in memory. Ii references the entire item in a uniform manner.
8
Data Structures using C Unit - I
As the elements of the array LA are stored in consecutive memory cells, the computer does not
need to keep track of the address of every element of the array. It only needs to keep track of the
address of the first element of the array, which is denoted by:
BASE (LA)
It is called the base address of the LA. Using this base address BASE (LA), the computer
calculates the address of any element of the array by using the following formula:
The length or number of data elements of the array can be obtained from the index set by the
formula:
Length = UB – LB + 1
Example:
An automobile company uses an array AUTO to record the number of automobiles sold each
year from 1932 through 1984. Rather than beginning the index set with 1, it is more useful to
begin the index set with 1932.
Then LB = 1932 is the lower bound and UB = 1984 is the upper bound of AUTO. By formula,
Length = UB – LB + 1 = 1984 – 1932 + 1 = 53
That is, AUTO contains 55 elements and its index set consists of all integers from 1932 through
1984.
BASE (AUTO [1932]) = 200, and w = 4 words per memory cell for AUTO. Then
9
Data Structures using C Unit - I
ADDRESS (AUTO [1932]) = 200, ADDRESS (AUTO [1933]) = 204, ADDRESS (AUTO [1934]) =
208, …
The address of the array element for the year k = 1965, can be obtained by using:
ADDRESS (LA[k]) = BASE (LA) + w (k – lower bound)
return 0;
}
10
Data Structures using C Unit - I
TWO-DIMENSIONAL ARRAYS
There are two subscripts in the syntax of 2D array in which one specifies the number of
rows and the other the number of columns.
The various operations that can be performed on a matrix are: addition, multiplication.
Transposition and finding the determinant of the matrix.
An example of 2D array can be arr[2][3] containing 2 rows and 3 columns and arr[0][1] is an
element placed at 0 row and 1 column in the array. A two-dimensional array can thus be
represented as given in Figure.
Row 0 arr[0][1]
Row 1
A two-dimensional array differentiates between the logical and physical view of data. A 2-D array
is a logical data structure that is useful in programming and problem solving. For example, such
an array is useful in describing an object that is physically two-dimensional such as a map or
checkerboard.
ROW MAJOR AND COLUMN MAJOR ORDER
All elements of a matrix get stored in the memory in a linear fashion. The two ways in which
elements can be represented in computer‘s memory are—Row Major Order and Column Major
Order.
In row-major representation the first row of the array occupies the first set of memory locations,
second occupies the next set, and so on.
The diagrammatic representation of two-dimensional army arr[2][3] in row major order is given in
Figure.
11
Data Structures using C Unit - I
In Figure the memory locations are first occupied by the first row of the array. Now BASE(arr)
has the address of first element of the array arr[0][0]. We also assume the size of each element
in the array with esize.
int matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 10, 11, 12} };
Let us assume that the base address of the array matrix is 100. Since w=2, LB=1, therefore,
according to the formula, address of (2, 3) th element in the array matrix will be:
= 100 + (4 + 2) * 2
Thus, we see that in the above matrix, address of (2, 3) th which is 7 is 112 (as depicted in the
figure.).
Similarly, the column major order representation, let us consider the same matrix:
To find the address of (j, k)th element of an array of m × n dimension, with w word size of each
element, the following formula can be used:
ADDRESS(i, j) = BASE(A) + w[M(k-LB) + (j-LB)]
12
Data Structures using C Unit - I
Let us assume that the base address of the array matrix is 100. Since w=2, LB=1, therefore,
according to the formula, address of (2, 3) th element in the array matrix will be:
= 100 + (6 + 1) * 2
We see that the address of the same element ((2, 3)th i.e., 7) has changed to 114 instead of 112,
in column major order representation of the array.
This is due to the fact that the element in the column major order representation are stored in a
different order, i.e., all the elements of column 1 are stored first and then all the elements of
column 2 are stored, followed by all the elements in column 3, and so on.
MULTIDIMENSIONAL ARRAYS
The array can also have more than two dimensions. For example, three-dimensional array may
be declared as: int arr[3][2][4];
An element of this is array is referenced by three subscripts. The first specifies the plane number,
the send specifies the row number and third specifies the column number. Such an array in
useful when the value is determined by three inputs.
The number of elements in any array is the product of the ranges all its dimensions. For
example, arr contains 3*2*4=24 elements. The arr[3][2][4] can he represented shown in Figure.
It can be observed that, the last subscript varies most rapidly, and subscript is not increased until
all possible combinations of the subscript to right have been exhausted.
13
Data Structures using C Unit - I
ADVANTAGE OF ARRAY
Code Optimization: Less code is required; one variable can store numbers of value.
Easy to traverse data: By using array easily retrieve the data of array.
Easy to sort data: Easily short the data using swapping technique
Random Access: With the help of array index you can randomly access any elements
from array.
DISADVANTAGE OF ARRAY
Fixed Size: Whatever size, we define at the time of declaration of array, we cannot
change their size, if you need more memory in that time you cannot increase memory
size, and if you need less memory in that case also wastage of memory.
Searching Searching or finding any element with the given value or a record with a given
key.
TRAVERSAL
Let LA be a collection of data elements stored in the computer‘s memory. To print the content of
each element of LA or count the number of elements of LA with a given property, each element
of LA will have to be accessed or processed at least once. This is called traversing.
Let LA be a linear array with lower bound LB and upper limit UB. The following
algorithm traverses LA applying an operation PROCESS to each element of LA.
1. Initialize counter
Set counter := LB
2. Repeat step 3 and 4 while counter <= UB
3. Visit element
Apply PROCESS to LA[counter]
4. Increase counter
Set counter := counter + 1
5. Exit
14
Data Structures using C Unit - I
SEARCHING
Searching refers to the operation of finding the location of any item in the array.
Consider LA is a linear array with N elements and ITEM is the given item of
information. This algorithm finds the location LOC of ITEM in LA or sets
LOC=0 if search is unsuccessful.
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
while( j < n)
{
if( LA[j] == item )
{
break;
}
j = j + 1;
15
Data Structures using C Unit - I
}
printf("Found element %d at position %d\n", item, j+1);
}
Example:
Suppose NAME is an 8-element linear array, and suppose five names are in the array:
(a) Observe that the names are listed alphabetically, and suppose we want to keep the array
names alphabetical at all times.
(b) Suppose Ford is added to the array. Then Johnson, Smith and Wagner must each be
moved downward one Location.
(c) Next suppose Taylor is added to the array; then Wagner must be moved.
(d) Last, suppose Davis is removed from the array. Then the five names Ford, Johnson,
Smith, Taylor and Wagner must each be moved upward one Location.
Clearly such movement of data would be very expensive if thousands of names were in the
array.
1. Initialize counter
Set J := N
2. Repeat step 3 and 4 while J >= K
3. Move Jth element downward
Set LA[J+1] := LA[J]
4. Decrease counter
Set J := J – 1
End of step 2 loop
16
Data Structures using C Unit - I
5. Insert element
Set LA[K] := ITEM
6. Reset N
Set N := N + 1
7. Exit
#include <stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
Let LA be a linear array – delete from the array is DELETE (LA, N, K, ITEM). N
is the number of items. K is the positive integer such that K<=N. the
following algorithm deletes Kth element from the array LA.
17
Data Structures using C Unit - I
#include <stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("LA[%d] = %d \n", i, LA[i]);
}
j = k;
while( j < n)
{
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++)
{
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When an array is declared, the compiler allocates a base address and sufficient amount
of storage to contain all the elements of the array in contiguous memory locations.
The base address is location of the first element (index 0) of the array.
The compiler also defines the array name as a constant pointer to the first element.
Suppose the base address of x is 1000 and assuming that each integer requires two bytes, the
five elements will be stored as follows:
The name x is defined as a constant pointer pointing to the first element, x[0] and therefore the
value of x is 1000, the location where x[0] is stored. That is,
x = &x[0] = 1000
18
Data Structures using C Unit - I
If we declare p as an integer pointer, then we can make the pointer p to point to the array x by
the following assignment:
p = x;
Now, we can access every value of x using p++ to move from one element to another. The
relationship between p and x is shown as:
p = &x[0] (= 1000)
p+1 = &x[1] (= 1002)
p+2 = &x[2] (= 1004)
p+3 = &x[3] (= 1006)
p+4 = &x[4] (= 1008)
The address of an element is calculated using its index and scale factor of the data type. For
instance,
= 1000 + (3 × 2) = 1006
Write a program using pointers to compute the sum of all elements stored in an array.
main()
{
int *p, sum, i;
int x[5] = {5,9,6,3,7};
i=0;
p=x;
printf("Element Value Address");
while(i<5)
{
printf("x[%d] %d %u\n", i, *p, p);
sum = sum + *p;
i++; p++;
}
printf("\n Sum = %d\n", sum);
printf("\n &x[0] = %u\n", &x[0] );
printf("\n p = %u\n", p );
}
*(*(a+i)+j) or *(*(p+i)+j)
19
Data Structures using C Unit - I
Above figure illustrates how this expression represents the element a[i][j]. The base address
of array a is &a[0][0] and starting at this address, the compiler allocates contiguous space for the
all the elements row-wise.
That is, the first element of the second row is placed immediately after the last element of the first
row, and so on.
Example:
};
20
Data Structures using C Unit - I
Strings are treated like character arrays and therefore, they are declared and initialized as
follows:
The compiler automatically inserts the null character ‗\0‘ at the end of the string. C supports an
alternative method to create strings using pointer variable of type char. Example:
This create string for the literal and stores its address in the pointer variable str.
The pointer str now points to the first character of the string ―good‖ as:
We can also use the run-time assignment for given values to a string pointer. Example:
char *string1;
string1 = ‚good‛;
string1 = ‚good‛ is not a string copy, because variable string1 is a pointer, not a string.
We can print the content of the string string1 using either printf or puts function as follows:
printf(‚%s‛, string1);
puts(string1);
21
Data Structures using C Unit - I
ARRAY OF POINTERS
One important use of pointers is in handling of a table of strings. Consider following array of
strings:
char name[3][25];
This say that the name is a table containing three names, each with a minimum length of 25
characters. The total storage requirement for the name table are 75 bytes.
Therefore, instead of making each row fixed number of characters, we can make it a pointer to a
string of varying length. Example:
‚Australia‛,
‚India‛
};
declares name to be an array of three pointers to characters, each pointer pointing to a particular
name as:
this declaration allocates only 28 bytes, sufficient to hold all the characters as shown
The following statement would print out all the three names:
printf(‚%s\n‛, name[i]);
to access the j-th character in the i-th name, we may write as:
*(name[i] + j)
22
Data Structures using C Unit - I
STRINGS
A string is a sequence of characters that is treated as single data item.
Any group of characters defined between double quotation marks is a string constant. Example:
“Well Done!”
Character strings are often used to build meaningful and readable programs. The common
operations performed on character strings include:
String conversion
String manipulation
STRING REPRESENTATIONS
The general form of declaration of a string variable is:
o char string_name[size];
The size determines the number of characters in the string_name. some examples are:
o char city[10];
o char name[30];
o char city[9] = {‘N’, ‘E’, ‘W’, ‘ ‘, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’};
The reason that city had to be 9 elements long is that the string NEW YORK containing 8
characters and one element space is provided for the NULL terminator.
C also permits, to initialize a character array without specifying the number of elements.
For example:
We can also declare the size much larger that the string size in the initializer.
23
Data Structures using C Unit - I
o Char str1[10];
Similarly,
o Char str2[10];
STRING CONVERSION
atoi() function
atoi() function in C language converts string data type to int data type. Syntax for atoi()
function is given below.
int atoi (const char * str);
―stdlib.h‖ header file supports all the type casting functions in C language.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[10] = "100";
int value = atoi(a);
printf("Value = %d\n", value);
return 0;
}
atof() function
atof() function in C language converts string data type to float data type. Syntax for atof()
function is given below.
double atof (const char* string);
―stdlib.h‖ header file supports all the type casting functions in C language.
#include <stdio.h>
#include <stdlib.h>
24
Data Structures using C Unit - I
int main()
{
char a[10] = "3.14";
float pi = atof(a);
printf("Value of pi = %f\n", pi);
return 0;
}
atol() function
atol() function in C language converts string data type to long data type. Syntax for atol()
function is given below.
long int atol ( const char * str );
―stdlib.h‖ header file supports all the type casting functions in C language.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[20] = "100000000000";
long value = atol(a);
printf("Value = %ld\n", value);
return 0;
}
itoa () function
itoa () function in C language converts int data type to string data type. Syntax for
this function is given below.
char * itoa ( int value, char * str, int base );
―stdlib.h‖ header file supports all the type casting functions in C language. But, it is a non
standard function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a=54325;
25
Data Structures using C Unit - I
char buffer[20];
itoa(a,buffer,2); // here 2 means binary
printf("Binary value = %s\n", buffer);
ltoa() function
ltoa() function in C language converts long data type to string data type. Syntax for ltoa()
function is given below.
char *ltoa(long N, char *str, int base);
―stdlib.h‖ header file supports all the type casting functions in C language.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
long a=10000000;
char buffer[50];
ltoa(a,buffer,2); // here 2 means binary
printf("Binary value = %s\n", buffer);
26
Data Structures using C Unit - I
STRING MANIPULATION
String.h header file supports all the string functions in C language.
strcat() function
strcat( ) function in C language concatenates two given strings. It concatenates source
string at the end of destination string. Syntax for strcat( ) function is given below.
Example:
strcat ( str2, str1 ); – str1 is concatenated at the end of str2.
strcat ( str1, str2 ); – str2 is concatenated at the end of str1.
#include <stdio.h>
#include <string.h>
int main( )
strcpy() function
strcpy( ) function copies contents of one string into another string. Syntax for strcpy
function is given below.
Example:
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.
#include <stdio.h>
#include <string.h>
int main( )
27
Data Structures using C Unit - I
return 0;
strlen() function
strlen( ) function in C gives the length of the given string. Syntax for strlen( ) function is
given below.
strlen( ) function counts the number of characters in a given string and returns the
integer value.
It stops counting the character when null character is found. Because, null character
indicates the end of the string in C.
#include <stdio.h>
#include <string.h>
int main( )
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
return 0;
strcmp() function
strcmp( ) function in C compares two given strings and returns zero if they are same.
If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns >
0 value. Syntax for strcmp( ) function is given below.
strcmp( ) function is case sensitive. i.e, ―A‖ and ―a‖ are treated as different characters.
28
Data Structures using C Unit - I
#include <stdio.h>
#include <string.h>
int main( )
int i, j, k ;
return 0;
strlwr() function
strlwr( ) function converts a given string into lowercase. Syntax for strlwr( ) function is
given below.
strlwr( ) function is non standard function which may not available in standard library in
C.
#include<stdio.h>
#include<string.h>
int main()
printf("%s\n",strlwr (str));
return 0;
strupr() function
strupr( ) function converts a given string into uppercase. Syntax for strupr( ) function is
given below.
29
Data Structures using C Unit - I
strupr( ) function is non standard function which may not available in standard library in
C.
#include<stdio.h>
#include<string.h>
int main()
printf("%s\n",strupr(str));
return 0;
strrev() function
strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is
given below.
strrev( ) function is non standard function which may not available in standard library in
C.
#include<stdio.h>
#include<string.h>
int main()
return 0;
30