Professional Documents
Culture Documents
1st Chap
1st Chap
1st Chap
Example: An employee's record must show its salary, position, experience, etc. It all can be
stored in one single variable using structures.
An array is a linear data structure that collects elements of the same data type and stores them
in contiguous and adjacent memory locations. Arrays work on an index system starting from 0
to (n-1), where n is the size of the array. It is an array, but there is a reason that arrays came into
the picture.
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.
DATA
Data is a collection of facts and figures or a set of values or values of a specific format that refers to a
single set of item values. The data items are then classified into sub-items, which is the group of items
that are not known as the simple primary form of the item.
Let us consider an example where an employee name can be broken down into three sub-items: First,
Middle, and Last. However, an ID assigned to an employee will generally be considered a single item.
In the example mentioned above, the items such as ID, Age, Gender, First, Middle, Last, Street, Locality,
etc., are elementary data items. In contrast, the Name and the Address are group data items.
The data can be managed in various ways, like the logical or mathematical model for a specific
organization of data is known as a data structure.
· First, it must be loaded enough into the structure to reflect the definite correlation of the data
with a real-world object.
· Second, the formation should be so straightforward that one can adapt to process the data
efficiently whenever necessary.
Data Structures are widely used in almost every aspect of Computer Science, i.e., Compiler Design,
Operating Systems, Graphics, Artificial Intelligence, and many more.
Data Structures are the main part of many Computer Science Algorithms as they allow the programmers
to manage the data in an effective way. It plays a crucial role in improving the performance of a program
or software, as the main objective of the software is to store and retrieve the user's data as fast as
possible.
Data Structures are the building blocks of any software or program. Selecting the suitable data structure
for a program is an extremely challenging task for a programmer.
The following are some fundamental terminologies used whenever the data structures are involved:
Data: We can define data as an elementary value or a collection of values. For example, the Employee's
name and ID are the data related to the Employee.
Group Items: Data Items that have subordinate data items are known as Group Items. For example, an
employee's name can have a first, middle, and last name.
Elementary Items: Data Items that are unable to divide into sub-items are known as Elementary Items.
For example, the ID of an Employee.
Entity and Attribute: A class of certain objects is represented by an Entity. It consists of different
Attributes. Each Attribute symbolizes the specific property of that Entity. For example,
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 specific attribute.
Pointers
Introduction to Pointers in Data Structure. Pointers are the variables that are used to store the location
of value present in the memory. A pointer to a location stores its memory address. The process of
obtaining the value stored at a location being referenced by a pointer is known as dereferencing.
Call_by_value needs the value of arguments to be copied every time any operation needs to be
performed.
Call_by_reference makes this task easier using its memory location to update the value at memory
locations.
Syntax:
<datatype> *variable_name
int section[80];
int *student = section;
int section[80];
int *student = §ion[0];
EXAMPLE
int *ptr1 – ptr1 references to a memory location that holds data of int datatype.
print(“%d”,**ptr2) // prints 30
Steps:
· Declare a pointer variable with the same type as the normal variable
· Access the value of the variable by using asterisk (*) - it is known as dereference operator
Example:
Here, we have declared a normal integer variable num and pointer variable ptr, ptr is being initialized
with the address of num and finally getting the value of num using pointer variable ptr.
int *ptr;
ptr = #
#include <stdio.h>
int main(void)
//normal variable
//pointer variable
int *ptr;
//pointer initialization
ptr = #
return 0;
Output
1.malloc()
2.calloc()
3.realloc()
4.free()
malloc(
allocates single block of requested memory.
)
realloc(
reallocates the memory occupied by malloc() or calloc() functions.
)
malloc() function in C
The malloc() function allocates single block of requested memory.
It doesn't initialize memory at execution time, so it has garbage value initially.
It returns NULL if memory is not sufficient.
The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)
calloc() function in C
The calloc() function allocates multiple block of requested memory.
It initially initialize all bytes to zero.
It returns NULL if memory is not sufficient.
ptr=(cast-type*)calloc(number, byte-size)
realloc() function in C
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc()
function. In short, it changes the memory size.
Let's see the syntax of realloc() function.
ptr=realloc(ptr, new-size)
free() function in C
The memory occupied by malloc() or calloc() functions must be released by calling free()
function. Otherwise, it will consume memory until program exit.
Let's see the syntax of free() function.
free(ptr)
Integer
It represents some range of mathematical integers.
Float
Float stores double precision floating point number.
Character
The character represents a sequence of character data.
Pointer
Pointer holds the memory address of another variable.
Array
An array is a sequential collection of elements of fixed size. We can store more than one item in
only one variable i.e. Array.
Lists
The list is an ordered set of elements.
File
It gives access to the file.
Data structure operations are the methods used to manipulate the data in a data
structure. The most common data structure operations are:
•Traversal
raversal operations are used to visit each node in a data structure in a specific
T
order. This technique is typically employed for printing, searching, displaying,
and reading the data stored in a data structure.
•Insertion
I nsertion operations add new data elements to a data structure. You can do this
at the data structure's beginning, middle, or end.
•Deletion
earch operations are used to find a specific data element in a data structure.
S
These operations typically employ a compare function to determine if two data
elements are equal.
•Sort
ort operations are used to arrange the data elements in a data structure in a
S
specific order. This can be done using various sorting algorithms, such as
insertion sort, bubble sort, merge sort, and quick sort.
•Merge
erge operations are used to combine two data structures into one. This
M
operation is typically used when two data structures need to be combined into a
single structure.
•Copy
opy operations are used to create a duplicate of a data structure. This can be
C
done by copying each element in the original data structure to the new one.
•Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures where
each data element can be randomly accessed by using its index number.
ARRAY
In C programming, they are the derived data types that can store the primitive type of data such
as int, char, double, float, etc. For example, if we want to store the marks of a student in 6
subjects, then we don't need to define a different variable for the marks in different subjects.
Instead, we can define an array that can store the marks in each subject at the contiguous
memory locations.
Properties of array
There are some of the properties of an array that are listed as follows -
Each element in an array is of the same data type and carries the same size that is 4
bytes.
Elements in the array are stored at contiguous memory locations from which the first
element is stored at the smallest memory location.
Elements of the array can be randomly accessed since we can calculate the address of
each element of the array with the given base address and the size of the data element.
Representation of an array
We can represent an array in various ways in different programming languages. As an
illustration, let's see the declaration of array in C language -
As per the above illustration, there are some of the following important points -