Data Structures With C
Table of Content
INTRODUCTION TO DATA STRUCTURES
Data Structures
Data structure is a data organization, management, and storage format that
enables efficient access and modification. More precisely, a data structure is a
collection of data values, the relationships among them, and the functions or
operations that can be applied to the data. It is an algebraic structure about data.
Let us consider a training program, Computer Engineering. Student belong
to different semesters. Each semester has multiple sections. Each section has
students – girls and boys. Each student is described using parameters or attributes
such as name, age, marks in various subjects, grade etc. The training program has
subjects semester wise, each subject has trainers. There are questions, answers,
exam results which are maintained subject wise. There is a relation between
student and the subject.
All these entities involve adding, deleting, searching, analysing, modifying of
data at every level. How easy are these activities depends on how the data is
structured and organized. It also depends on how the relationship between these
entities is defined in computer.
Data structure is a method of storing data in a computer, so that it can be
used efficiently. Data structure is a study of how the data is stored and organized
in computer memory. It mainly deals with:
- How efficiently the data can be stored in computer memory.
- How efficiently data can be retrieved and manipulated.
- The possible ways in which different data items are logically related.
Primitive data structures are the data structures that can be manipulated
directly by machine instructions. Primitive data structure is the basic orfundamental data types of any programming language.
For example, integers, floating point numbers (real), characters, pointers
etc. In C language int, float, char, double are basic data types - Non Primitive Data Structures
Non primitive data structures are the data structures that cannot be
manipulated directly by machine instructions. Arrays, stacks, queues, linked lists, files etc. cannot be manipulated by direct machine instructions. These are generally classified under non-primitive data types. Non primitive data structures
classified into two categories, Linear and Non Linear.
a. Linear data structures – The data structures which show the relationship
of logical adjacency between the data items is stored and retrieved.
(Arrays, Stacks, Queues, Linked Lists, files)
b. Non Linear data structures – The data structures which show the
relationship of logical adjacency between the data items is stored and
retrieved.
(Trees, Graphs) - Functions, Implementations of Functions
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main (), and all the most trivial
programs can define additional functions.
You can divide up your code into separate functions. How you divide up
your code among different functions is up to you, but logically the division is such
that each function performs a specific task.
A function declaration tells the compiler about a function's name, return
type, and parameters. A function definition provides the actual body of the
function.
Defining a Function
The general form of a function definition in C programming language is as
follows
return_type function_name( parameter list )
{
body of the function
}
A function definition in C programming consists of a function header and a
function body. Here are all the parts of a function −
Return Type− A function may return a value. The return_type is the data
type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the
keyword void.
Function Name− This is the actual name of the function. The function name
and the parameter list together constitute the function signature. - Parameters − A parameter is like a placeholder. When a function is invoked,
you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters. - Function Body− The function body contains a collection of statements that
define what the function does.
Example:
Given below is the source code for a function called max(). This function
takes two parameters num1 and num2 and returns the maximum value between
the two:
/* function returning the max between two numbers */
int max(int num1,int num2)
{
int result; /* local variable declaration */
if(num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declarations
A function is declared before it is defined.
A function declaration tells the compiler about a function name and how to
call the function. The actual body of the function can be defined separately.
A function declaration has the following parts −
return_type function_name (parameter list );
For the above defined function max(), the function declaration is as follows −
int max (int num1, int num2) - The parameter names are not important in function declaration only their
type is required, so the following is also a valid declaration −
int max(int, int);
Function declaration is required as extern when you define a function in one
source file and you call that function in another file. In such case, you should
declare the function at the top of the file calling the function.
Calling a Function
While creating a C function, you give a definition of what the function has to
do. To use a function, you will have to call that function to perform the defined
task.
When a program calls a function, the program control is transferred to the
called function. A called function performs a defined task and when its return
statement is executed or when its function-ending closing brace is reached, it
returns the program control back to the main program.
Function Arguments
If a function uses arguments, it must declare variables that accept the values
of the arguments. These variables are called the formal parameters of the
function.
Formal parameters behave like other local variables inside the function and
are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be
passed to a function:
Call by value: This method copies the
actual value of an argument into the formal
parameter of the function. In this case,
changes made to the parameter inside the
function have no effect on the argument.
Call by reference: This method copies the
address of an argument into the formal
parameter. Inside the function, the address
is used to access the actual argument used
in the call. This means that changes made to
the parameter affect the argument.
By default, C uses call by value to pass arguments. In general, it means the
code within a function cannot alter the arguments used to call the function. - 2 Structures, Structure Variables, Pointer to Structure Variables
Arrays allow to define type of variables that can hold several data items of
the same kind. Similarly structure is another user defined data type available in C
that allows to combine data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of
your books in a library. You might want to track the following attributes about
each book – - Title
- Author
- Subject
- Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member. The format of
the struct statement is as follows:
struct[structure tag]
{
member definition;
member definition;
...
member definition;
}[one or more structure variables];
The structure tag is optional and each member definition is a normal
variable definition, such as int i; or float f; or any other valid variable definition. At
the end of the structure's definition, before the final semicolon, you can specify
one or more structure variables but it is optional. Here is the way you would
declare the Book structure:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book - Accessing Structure Members
To access any member of a structure, we use the member access operator
(.). The member access operator is coded as a period between the structure
variable name and the structure member that we wish to access. You would use
the keyword struct to define variables of structure type. The following example
shows how to use a structure in a program:
#include<stdio.h>
#include<string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main()
{
struct Books Book1; /* Declare Book1 of type Book */
structBooksBook2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy(Book1.title,"C Programming");
strcpy(Book1.author,"Nuha Ali");
strcpy(Book1.subject,"C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy(Book2.title,"Telecom Billing");
strcpy(Book2.author,"Zara Ali");
strcpy(Book2.subject,"Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf("Book 1 title : %s\n",Book1.title);
printf("Book 1 author : %s\n",Book1.author);
printf("Book 1 subject : %s\n",Book1.subject);
printf("Book 1 book_id : %d\n",Book1.book_id); - printf("Book 2 title : %s\n",Book2.title);
printf("Book 2 author : %s\n",Book2.author);
printf("Book 2 subject : %s\n",Book2.subject);
printf("Book 2 book_id : %d\n",Book2.book_id);
return0;
}
When the above code is compiled and executed, it produces the following result:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Structures as Function Arguments
You can pass a structure as a function argument in the same way as you
pass any other variable or pointer.
#include<stdio.h>
#include<string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook(struct Books book );
int main()
{
struct Books Book1; /* Declare Book1 of type Book */
structBooksBook2; /* Declare Book2 of type Book */
/* book 1 specification */
`