1st Chap

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 13

Introduction to Data Structures

What is structure with example?


A structure is a collection of variables of same or different datatypes. It is useful in storing or
using informations or databases.

Example: An employee's record must show its salary, position, experience, etc. It all can be
stored in one single variable using structures.

What is an array of structure?


An array of structures is simply an array in which each element is a structure of the same type.
The referencing and subscripting of these arrays (also called structure arrays) follow the same
rules as simple arrays.

cat = REPLICATE(A, 100)

What is an array within a structure?

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;

} [one or more union variables];

· 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.

Figure 1: Representation of Data Items

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.

What is Data Structure?


Data Structure is a branch of Computer Science. The study of data structure allows us to understand the
organization of data and the management of the data flow in order to increase the efficiency of any
process or program.
Data Structure is a particular way of storing and organizing data in the memory of the computer so
that these data can easily be retrieved and efficiently utilized in the future when required.

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.

The scope of a particular data model depends on two factors:

· 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.

Some examples of Data Structures are

Arrays, Linked Lists, Stack, Queue, Trees, etc.

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.

Basic Terminologies related to Data Structures

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.

Data Items: A Single unit of value is known as Data Item.

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,

Attributes ID Name Gender Job Title

Values 1234 Stacey M. Hill Female Software Developer

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.

DECLARATION AND INITILIZATION OF POINTER

Syntax:
<datatype> *variable_name
int section[80];
int *student = section;

int section[80];
int *student = &section[0];

EXAMPLE
int *ptr1 – ptr1 references to a memory location that holds data of int datatype.

int var = 30;

int *ptr1 = &var; // pointer to var

int **ptr2 = & ptr1; // pointer to pointer variable ptr1

print(“%d”, *ptr1) // prints 30

print(“%d”,**ptr2) // prints 30

Accessing the value of a variable using pointer

Steps:

· Declare a normal variable, assign the value

· Declare a pointer variable with the same type as the normal variable

· Initialize the pointer variable with the address of 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 num = 100;

int *ptr;

ptr = &num;
#include <stdio.h>

int main(void)

//normal variable

int num = 100;

//pointer variable

int *ptr;

//pointer initialization

ptr = &num;

//pritning the value

printf("value of num = %d\n", *ptr);

return 0;

Output

value of num = 100

What is static and dynamic memory allocation?


Memory allocation is the process of assigning specific areas in a system’s memory to a
programming language or code written for execution. This may include allocating some portion
or even all of the computer memory. A program’s data and instructions are mapped to such a
virtualized memory as well as physical memories’ addresses.
Memory Allocation Techniques
Memory allocations are generally divided into two categories, as described below in the
following section:
• Allocation of static memory 
• Allocation of dynamic memory 
One main distinction between the two seems to be so that the Static Memory Allocation enables
for a constant memory size following allocation, whereas Dynamic Memory Allocation permits
for memory size alterations following allocation.
Memory Allocation: Memory resource management or allocation is the process of allocating
physical as well as virtual system memory to computer apps and different services. Memory
allocation takes place during, or it may take before program execution. Memory allocations are
generally divided into two categories, as mentioned above.
Static memory allocation
 
Static memory, often called Compile time Memory Allocation, seems to be a technique enabling
allocation of memory when the compilation of data is done with a constant size. Variables inside
the program are allocated memory by a compiler that the programmer uses.
Before executing the said operation, the volume of data necessary for the operation is defined.
The algorithm guesses the Fixed memory size if it really is unspecified. Smaller sizes result in
inefficient processing, whereas larger sizes result in waste.
This Allotment procedure is executed even before the execution of the program begins, instead
of being allocated during the program’s implementation.
This form seems helpful to arrays whenever the static phrase is being used as it helps provide
some specific parameters throughout the C programming language.
Features of static memory allocation:
• Stated static variables are usually allocated either by the compiler, they could be retrieved
through using some specific operation, which would be associated with a variable and this
variable is called a pointer
• Static variables are fixed and can’t be altered
• Memory allocation or allotment is done during the coding phase
• In this case, Stack Memory gets utilised
• Even if the static memory allocation technique seems quick, it’s indeed inefficient
• During the first allotment and after it’s done, memory can never be reused
• During the first allotment and after it’s done, memory can never be changed
• This allotment procedure seems straightforward
 
Dynamic memory allocation
 
Dynamic Memory allotment, frequently referred to as Run time allocation of memory, seems to
be a technique enabling allocation of memory during the execution process. Whenever the code
is executed for the very first instance, it provides allocation of memory.
This allots precise storage for data and memory for the Dynamic content at execution,
minimizing storage waste.
While the execution of the program is done, it generates overhead for each action through the
system, slowing down the overall procedure. This offers more efficiency during the allocation
process by performing tasks on various program areas, thus reducing waste.
Features of dynamic memory:
• The pointer variable seems to be alloted to a value and appears to be later returned by several
functions
• Throughout processing, memory allocation is done
• Memory might get allocated as well as freed at any given moment
• Here, a heap memory seems to be utilized for the process
• Dynamic memory allocation is generally slower than static memory allocation but it is far better
and much more efficient
• The program’s execution seems complex
• Memory may very well be flexibly expanded as well as reclaimed or reused

The concept of dynamic memory allocation in c language enables the C programmer to


allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions
of stdlib.h header file.

1.malloc()

2.calloc()

3.realloc()

4.free()

malloc(
allocates single block of requested memory.
)

calloc() allocates multiple block of requested memory.

realloc(
reallocates the memory occupied by malloc() or calloc() functions.
)

free() frees the dynamically allocated memory.

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)  

Data Structure Classification


Data Structure is classified in basically two main categories -
•Primitive Data Structure
•Non Primitive Data Structure
Primitive and Non Primitive are further classified into many different types.

Primitive Data Structure


The primitive data structure is the basic data structure that directly operates upon the machine
instruction. In programming language, it is used as built in data type. It has a different
representation of different computer machine.

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.

Non-Primitive Data Structure


The non-primitive data structure is derived from the primitive data structure and that is more
complicated to create. It is responsible for structuring of a group of homogeneous and
heterogeneous data type. This is used to process large amount of data.

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


Next, we will look at some of the most common data structure operations.

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

‍ eletion operations remove data elements from a data structure. These


D
operations are typically performed on nodes that are no longer needed.
•‍Search

‍ 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 -

Index starts with 0.

The array's length is 10, which means we can store 10 elements.

Each element in the array can be accessed via its index.

You might also like