Академический Документы
Профессиональный Документы
Культура Документы
ACKNOWLEDGEMENT
First I would like to thank BDPS Educational institutions, Vijayawada for giving me an
opportunity as to enroll in the well planned course. I surely have learnt a lot from here. I
encouraged brilliant practical learning through real time testing and coding, which is impossible
to achieve through books. I hope that everything I have learnt here will be helpful in future.
I take this opportunity to express my deep sense of gratitude to Mr. Bala Krishna for his immense
support and guidance. He has helped me throughout and acted as our guide. I would like to thank
him for having immense patience while teaching me about algorithms and giving his precious
time to me.
I would also like to thank all my friends who helped me throughout the course by giving their
valuable time and guidelines to complete the course, and so many other countless people of LVR
Team for helping me during my course.
Tarun Teja
11610476
ECE-7
About BDPS
BDPS is a privately-held company based in Vijayawada, India. Since 2008, we have been helping
IT professionals get better at what they do by providing an extensive range of training services on
emerging technologies. Always pushing the envelope, we are constantly exploring new fields of
knowledge as well as new training methodologies to better serve our clients.The company is led
by a team of experts , with accumulated experience of tens of years of software development,
architectural design and project management. The team has provided most authentic,
comprehensive and high quality training services to good number of companies in recent years,
ranging from small start-ups to large enterprises.
We pride ourselves by standing by our commitment to help IT professionals get to the next level,
by being in tune with our customers' actual needs, and by always delivering on what we promise,
all while having fun doing it.
Always pushing the envelope, we are constantly exploring new fields of knowledge as well as
new training methodologies to better serve our clients.
CONTENT
1. INTRODUCTION
1.1) Introduction of c
1.2) Data Structures and its applications
2. Contents in C
2.6) Strings in c
2.7) Structures in c
2.8) Union
3. BASIC DATA STRUCTURES
3.1) Arrays
3.2) Linked List
3.3) Stacks
3.4) Queue
3.5) Sorting techniques
3.6) Searching techniques
5
1. INTRODUCTION
1.1) INTRODUCTION OF C :
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of
C, now known as the K&R standard.
The UNIX operating system, the C compiler, and essentially all UNIX application programs have
been written in C. C has now become a widely used professional language for various reasons −
• Easy to learn
• Structured language
Facts about C :
• C was invented to write an operating system called UNIX.
• The language was formalized in 1988 by the American National Standard Institute
(ANSI).
• Today C is the most widely used and popular System Programming Language.
Today's most popular Linux OS and RDBMS MySQL have been written in C.
Why to use C ?
C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces code that
runs nearly as fast as the code written in assembly language. Some examples of the use of C might
be −
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
C Programs
A C program can vary from 3 lines to millions of lines and it should be written into one or more
text files with extension ".c"; for example, hello.c.
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
main() { printf("Hello
• The first line of the program #include<stdio.h> is a preprocessor command, which tells a
C compiler to include stdio.h file before going to actual compilation.
• The next line int main() is the main function where the program execution begins.
7
• The next line printf(...) is another function available in C which causes the message "Hello
World" to be displayed on the screen.
• The next line return 0; terminates the main() function and returns the value 0.
Output:Hello World .
1.2) DATA STRUCTURES & THEIR APPLICATIONS :
A data structure is a way to store and organize data in order to facilitate access and
modifications. 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.
No single data structure works well for all purposes, and so it is important to know the
strengths and limitations of several of them.
1.2.1) Usage
Different kinds of data structures are suited to different kinds of applications, and some
are highly specialized to specific tasks. For example, relational databases commonly
use B-tree indexes for data retrieval while compiler implementations usually use hash
tables to look up identifiers.
Data structures provide a means to manage large amounts of data efficiently for
uses such as large databases and internet indexing services. Usually, efficient data
structures are key to designing efficient algorithms. Some formal design methods and
programming languages emphasize data structures, rather than algorithms, as the key
organizing factor in software design. Data structures can be used to organize the storage
and retrieval of information stored in both main memory and secondary memory.
1.2.2) Implementation
Data structures are generally based on the ability of a computer to fetch and store
data at any place in its memory, specified by a pointer—a bit string, representing a
memory address, that can be itself stored in memory and manipulated by the program.
Thus, the array and record data structures are based on computing the addresses of data
items with arithmetic operations; while the linked data structures are based on storing
addresses of data items within the structure itself. Many data structures use both
principles, sometimes combined in non-trivial ways
8
2. DATA TYPES IN C:
Data types in c are used for declaring variables or functions of different types. The type of a variable
determines how much space it occupies in storage and how the bit pattern stored is interpreted.
Basic Types:
They are arithmetic types and are further classified into: (a) integer types and (b)
floating-point types.
2 Enumerated types
They are again arithmetic types and they are used to define variables that can only
assign certain discrete integer values throughout the program.
4 Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types
and (e) Function types.
2.1Integer Types :
The following table provides the details of standard integer types with their storage sizes and
value ranges –
9
To get the exact size of a type or a variable on a particular platform, you can use the sizeof
operator. The expressions size of(type) yields the storage size of the object or type in bytes.
2.2Floating-Point Types :
The following table provide the details of standard floating-point types with storage sizes and
value ranges and their precision –
2.3LOOPS: C
when a block of code needs to be executed several number of times. In general, statements are
executed sequentially: The first statement in a function is executed first, followed by the second,
and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. Given
below is the general form of a loop statement in most of the programming languages −
11
C programming language provides the following types of loops to handle looping requirements.
1 while loop: Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
2 for loop: Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
3 do...while loop: It is more like a while statement, except that it tests the condition at the
end of the loop body.
4 nested loops: You can use one or more loops inside any other while, for, or do..while loop.
1 break statement: Terminates the loop or switch statement and transfers execution
to the statement immediately following the loop or switch.
2 continue statement:Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
int main () {
forever.\n");
} return 0;
When the conditional expression is absent, it is assumed to be true. You may have an initialization
and increment expression, but C programmers more commonly use the for(;;) construct to signify
an infinite loop.
2.4 ARRAYS:
13
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99]
to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Declaring Arrays in C :
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows – type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type. For example, to declare a 10-element array called A
of type int, use this statement –
Int A[10]
Initializing Arrays :
You can initialize an array in C either one by one or using a single statement as follows − int
A[5]={100,20,30,40,50};
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ].
Int b=A[5];
The above statement will take the 5th element from the array and assign the value to b variable.
14
2.5 POINTERS in C:
Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily
with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without
using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let's
start learning them in simple and easy steps.
As you know, every variable is a memory location and every memory location has its address
defined which can be accessed using ampersand (&) operator, which denotes an address in
memory. Consider the following example, which prints the address of the variables defined −
#include <stdio.h> int main () { int var1; char
When the above code is compiled and executed, it produces the following result-
Address of var1 variable: bff5a400
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as a
pointer
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is
the same, a long hexadecimal number that represents a memory address. The only difference
between pointers of different data types is the data type of the variable or constant that the pointer
points to.
NULL Pointers
15
It is always a good practice to assign a NULL value to a pointer variable in case you do not have
an exact address to be assigned. This is done at the time of variable declaration. A pointer that is
assigned NULL is called a nullpointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider
the following program −
When the above code is compiled and executed, it produces the following result –the value of ptr
is 0.
In most of the operating systems, programs are not permitted to access memory at address 0
because that memory is reserved by the operating system. However, the memory address 0 has
special significance; it signals that the pointer is not intended to point to an accessible memory
location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to
nothing.
2.6 STRINGS in C:
Strings are actually one-dimensional array of characters terminated by a nullcharacter '\0'. Thus a
null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold
the null character at the end of the array, the size of the character array containing the string is
one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows −
char greeting[] = "Hello";
Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '\0' at the end of the string when it initializes the array. Let us try to print
the above mentioned string −
#include <stdio.h> int main () { char
return 0; }
When the above code is compiled and executed, it produces the following result Output: Greeting
message: Hello
strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater
than 0 if s1>s2.
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
2.7 STRUCTURES in C:
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.
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] {
definition;
...
member definition;
18
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.
2.8 UNION:
A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can contain
a value at any given time. Unions provide an efficient way of using the same memory location for
multiple-purpose.
Defining a Union :
To define a union, you must use the union statement in the same way as you did while defining
a structure. The union statement defines a new data type with more than one member for your
program. The format of the union statement is as follows − union [union tag] { member
definition; member definition;
...
member definition;
The union 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 union's definition, before the
final semicolon, you can specify one or more union variables but it is optional.
Applications:
3.2) Linked List: Linked list is a linear collection of data elements, in which linear order is
not given by their physical placement in memory. Instead, each element points to the
next. It is a data structure consisting of a group of nodes which together represent a
sequence.
Applications:
3.3) Stack :A stack is an abstract data type that serves as a collection of elements, with two
principal operations:
Applications :
• Expression evaluation
• Backtracking (game playing, finding paths, exhaustive searching)
• Memory management, run-time environment for nested language features.
3.4) Queue : A queue is a particular kind of ADT in which the entities in the collection are
kept in order and the principle (or only) operations on the collection are the addition of
entities to the rear terminal position, known as enqueue, and removal of entities from
the front terminal position, known as dequeue. This makes the queue a FIFO data
structure
o When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
3.5) Sorting techniques ; o Puts elements of a list in an order o Often implemented before search
or merge o One of the oldest data structure in computer science o Internal or external o E.g
;quick sort, selection sort, bubble sort, insertion sort o Practical sorting is based on average
complexity
o 0th element compared with all elements and swapped if its found to be greater
o Also known as sinking sort o 0th element compared with 1st element and
swapped it its found to be greater o Then first element is compare with 2nd
and the process continues o Each iteration fixates highest element at relative
3.6) Searching o To find an item in a collection o Can be a hit or miss,brute force or divide and
conquer o Internet ,data base ,spread sheet,word processor o May need the data to be sorted o
Analysis and efficiency based on no of comparisions
3.6.1) linear search o Simplest search algorithm o Used mostly with smaller or
3.6.2) Binary search o One of the fast and efficient o Requires list to be sorted o
Uses divide and conquer using binary tree o Checks center element in the
list ,if not a match then list is divided into two halves ,left and right o