0% found this document useful (0 votes)
4 views36 pages

Data Structure Using C Notes

This document provides an introduction to data structures in C, focusing on structures, unions, and pointers. It explains how to define and use structures and unions, including their syntax, initialization, and differences. Additionally, it covers pointers, including declaration, initialization, and dereferencing, with examples to illustrate their usage.

Uploaded by

Roopa Sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views36 pages

Data Structure Using C Notes

This document provides an introduction to data structures in C, focusing on structures, unions, and pointers. It explains how to define and use structures and unions, including their syntax, initialization, and differences. Additionally, it covers pointers, including declaration, initialization, and dereferencing, with examples to illustrate their usage.

Uploaded by

Roopa Sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

UNIT I

Introduction to Data Structure

Structure in C Programming
In C, a structure is a user-defined data type that can be used to group items of possibly
different types into a single type. The struct keyword is used to define a structure. The
items in the structure are called its member and they can be of any valid data type.

#include <stdio.h>
// Defining a structure
struct A {
int x;
};
int main() {
// Creating a structure variable
struct A a;
// Initializing member
a.x = 11;
printf("%d", a.x);
return 0;
}

Output
11
In this example, a structure A is defined to hold an integer member x. A variable a of
type struct A is created and its member x is initialized to 11 by accessing it using dot
operator. The value of a.x is then printed to the console.

Structures are used when you want to store a collection of different data types, such as
integers, floats, or even other structures under a single name.
Syntax of Structure
There are two steps of creating a structure in C:
1. Structure Definition
2. Creating Structure Variables

Structure Definition
A structure is defined using the struct keyword followed by the structure name and its
members. It is also called a structure template or structure prototype, and no memory is
allocated to the structure in the declaration.

struct structure_name {
data_type1 member1;
data_type2 member2;

};
Creating Structure Variable
After structure definition, we have to create variable of that structure to use it. It is similar
to the any other type of variable declaration:
struct strcuture_name var;
We can also declare structure variables with structure definition.
Structructure_name {

}var1, var2….;

Basic Operations of Structure

1. Access Structure Members


To access or modify members of a structure, we use the ( . ) dot operator . This is
applicable when we are using structure variables directly.
structure_name . member1;
strcuture_name . member2;
In the case where we have a pointer to the structure, we can also use the arrow operator to
access the members.
structure_ptr -> member1
structure_ptr -> member2
2. Initialize Structure Members
Structure members cannot be initialized with the declaration. For example, the following C
program fails in the compilation.
struct structure_name {
data_type1 member1 = value1; // COMPILER ERROR: cannot initialize members here
data_type2 member2 = value2; // COMPILER ERROR: cannot initialize members here

};
The reason for the above error is simple. When a datatype is declared, no memory is
allocated for it. Memory is allocated only when variables are created. So there is no space
to store the value assigned.
We can initialize structure members in 4 ways which are as follows:

Default Initialization
By default, structure members are not automatically initialized to 0 or NULL. Uninitialized
structure members will contain garbage values. However, when a structure variable is
declared with an initializer, all members not explicitly initialized are zero-initialized.
struct structure_name = {0}; // Both x and y are initialized to 0

Array of Structures in C
An array of structures is simply an array where each element is a structure. It
allows you to store several structures of the same type in a single array.

#include <stdio.h>
#include <string.h>

// Structure definition
struct A {
int var;
};

int main() {

// Declare an array of structures


struct A arr[2];

arr[0].var = 10;
arr[1].var = 20;

for (int i = 0; i < 2; i++)


printf("%d\n", arr[i].var);

return 0;
}

Output
10
20
We define a Person structure with name and age as members. An array of 2 Person
structures is declared, and each element is populated with different people’s details. A for
loop is used to iterate through the array and print each person's information.
Array of Structure Declaration
Once you have already defined structure, the array of structure can be defined in a similar
way as any other variable.
struct struct_name arr_name [size];
Need for Array of Structures
Suppose we have 50 employees, and we need to store the data of 50 employees. So for that,
we need to define 50 variables of struct Employee type and store the data within that.
However, declaring and handling the 50 variables is not an easy task. Let's imagine a bigger
scenario, like 1000 employees.
So, if we declare the variable this way, it's not possible to handle this.
struct Employee emp1, emp2, emp3, .. . ... . .. ... emp1000;
For that, we can define an array whose data type will be struct Employee soo that will be
easily manageable.
 Arrays of structures allow you to group related data together and handle multiple
instances of that data more efficiently.
 It is particularly useful when you want to manage large datasets (such as a list of
employees, students, or products) where each element has a consistent structure.

Example of Array of Structure


The below code demonstrates the application of array of structure inside a C program:

Store Student Information


#include <stdio.h>
#include <string.h>

// Structure definition
struct Student {
char name[50];
int age;
float marks;
};

int main() {

// Declaration and initialization of an array of structures


struct Student students[3] = {
{"Nikhil", 20, 85.5},
{"Shubham", 22, 90.0},
{"Vivek", 25, 78.0}
};

// Traversing through the array of structures and displaying the data


for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i+1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Marks: %.2f\n\n", students[i].marks);
}

return 0;
}

Output
Student 1:
Name: Nikhil
Age: 20
Marks: 85.50

Student 2:
Name: Shubham
Age: 22
Marks: 90.00

Student 3:
Name: Vivek
Age: 25
Marks: 78.00
Array within Structure in C

An array can be declared inside a structure as a member when we need to store multiple
members of the same type.
For example, suppose we have an employee and we need to store the data of his/her weekly
attendance. So for that, we need to define a structure of type Employee and store the data
within that. However, declaring and handling the 7 variables for each day of the week is not
an easy task.

struct Employee {
int day1, day2, day3, day4, day5, day6, day7;
};

For that, we can define an array within the structure whose data type will be int so that will be
easily manageable.

Syntax to Declare Array Within Structure

The below syntax is to declare array within structure in C.


struct StructureName {
// Other members
dataType arrayName[arraySize];
};

Note: It is recommended to define the array as the last member function so that if it
overflows, it does not overwrite all the other members.

Initialize Array Within Structure in C


We can initialize the array within structures using the below syntax:
struct StructureName variableName = { ...
{element1_value1, element1_value2 ...} };

Accessing Elements of an Array within a Structure


We can access the elements of the array within the structure using the dot (.) operator along
with the array index inside array subscript operator.
structureName.arrayName[index]

Example of Array within Structure in C


The below example demonstrates how we can initialize and use the array within structure in
C.
C
// C program to demonstrate the array within structures

#include <string.h>
#include <stdio.h>

// Defining array within structure


struct Employee {
// character array to store name of the employee
char Name[20];
int employeeID;
// integer array to maintain the record of attendanc eof
// the employee
int WeekAttendence[7];
};

int main()
{
// defining structure of type Employee
struct Employee emp;

// adding data
emp.employeeID = 1;
strcpy(emp.Name, "Rohit");
int week;
for (week = 0; week < 7; week++) {
int attendence;
emp.WeekAttendence[week] = week;
}
printf("\n");

// printing the data


printf("Emplyee ID: %d - Employee Name: %s\n",
emp.employeeID, emp.Name);
printf("Attendence\n");
for (week = 0; week < 7; week++) {
printf("%d ", emp.WeekAttendence[week]);
}
printf("\n");

return 0;
}

Output
Emplyee ID: 1 - Employee Name: Rohit
Attendence
0123456
Array of structures vs array within structure in c
Array within a Structure Array of Structures

A structure contains an array as its member An array in which each element is of type
variable. structure.

struct class { int ar[10]; } a1, a2, a3; struct class { int a, b, c; } students[10];

Can be accessed using the dot operator just Can be accessed by indexing just as we
as we access other elements of the structure. access an array.

Access elements syntax Access elements syntax


structure.array[index] array[index].member

Array within the structure will be stored in There will be some empty space between
sequential memory and structure padding is structure elements due to structure padding.
not dependent on the size of the array.

UNIONS

In C, union is a user-defined data type that can contain elements of the different data types
just like structure. But unlike structures, all the members in the C union are stored in the
same memory location. Due to this, only one member can store data at the given point in
time.

Syntax of Union in C
The syntax of union can be defined into two parts:

C Union Declaration
In this part, we only declare the template of the union, i.e., we only declare the members’
names and data types along with the name of the union. No memory is allocated to the
union in the declaration.

union name {
type1 member1;
type2 member2;
.
.
};

Create a Union Variable


We need to define a variable of the union type to start using union members. There are two
methods using which we can define a union variable:

Creating Union Variable with Declaration


union name{
type member1;
type member2;

} var1, var2, …;
Creating Union Variable after Declaration
union name var1, var2, var3…;

where name is the name of an already declared union.

Access Union Members


We can access the members of a union by using the ( . ) dot operator just like structures.
var1.member1;
where var1 is the union variable and member1 is the member of the union.

Initialize Union
The initialization of a union is the initialization of its members by simply assigning the
value to it.
var1.member1 = val;
One important thing to note here is that only one member can contain some value at a
given instance of time.

C program to display the usage of Union


#include <stdio.h>
#include <string.h>
// Union definition
union A {
int i;
float f;
char s[20];
};

int main() {

union A a;

// Storing an integer
a.i = 10;
printf("data.i = %d
", a.i);

// Storing a float
a.f = 220.5;
printf("data.f = %.2f
", a.f);

// Storing a string
strcpy(a.s, "GfG");
printf("data.s = %s
", a.s);

return 0;
}
Output
data.i = 10
data.f = 220.50
data.s = GfG

Difference between Structure and Union in C

Parameter Structure Union

A structure is a user-defined data A union is a user-defined data type


type that groups different data that allows storing different data
Definition types into a single entity. types at the same memory location.

The keyword struct is used to The keyword union is used to define


Keyword define a structure a union

The size is the sum of the sizes of The size is equal to the size of the
all members, with padding if largest member, with possible
Size necessary. padding.

Each member within a structure is


Memory allocated is shared by
Memory allocated unique storage area of
individual members of union.
Allocation location.

Data No data overlap as members are Full data overlap as members shares
Overlap independent. the same memory.

Accessing Individual member can be Only one member can be accessed at


Members accessed at a time. a time.

Introduction to Pointers in C

A pointer is a variable that stores the memory address of another variable. Instead of
holding a direct value, it holds the address where the value is stored in memory. There are 2
important operators that we will use in pointers concepts i.e.
 Dereferencing operator(*) used to declare pointer variable and access the value stored
in the address.
 Address operator(&) used to returns the address of a variable or to access the address
of a variable to a pointer.
Example:

#include <stdio.h>

int main()
{

// taking an integer variable


int m = 100;

// pointer variable ptr that stores


// the address of variable m
int *ptr = &m;

// printing the value of variable m


printf("The Value of Variable m is: %d\n", m);

// printing the memory address of variable m


// in hexadecimal format
printf("The Memory Address of Variable m is: %p\n", &m);

// printing the value of ptr i.e.


// printing the memory address of variable m
// in hexadecimal format using pointer variable
printf("The Memory Address of Variable m is using ptr: %p\n", ptr);

return 0;
}

Output
The Value of Variable m is: 100
The Memory Address of Variable m is: 0x7ffee1eea79c
The Memory Address of Variable m is using ptr: 0x7ffee1eea79c
A. Pointer Declaration
To declare a pointer, we use the (*) dereference operator before its name. In pointer
declaration, we only declare the pointer but do not initialize it.
B. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer
variable. We use the (&) addressof operator to get the memory address of a variable and
then store it in the pointer variable.
Note: We can also declare and initialize the pointer in a single step. This is called pointer
definition.
C. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory address
specified in the pointer. We use the same (*) dereferencing operator that we used in the
pointer declaration.
Note: It is recommended that the pointers should always be initialized to some value before
starting using it. Otherwise, it may lead to number of errors.
Pointer declaration and initialization

Once you got basics of memory addresses, reference and dereference operator. Let us declare
our first pointer variable.

Pointer variable declaration follows almost similar syntax as of normal variable.

Syntax to declare pointer variable

data-type * pointer-variable-name;

 data-type is a valid C data type.


 * symbol specifies it is a pointer variable. You must prefix * before variable name to
declare it as a pointer.
 pointer-variable-name is a valid C identifier i.e. the name of pointer variable.
Ex: int *p

Initialize pointer variable


There are two ways to initialize a pointer variable. You can use reference operator & to get
memory location of a variable or you can also directly assign one pointer variable to other
pointer variable.

Examples to initialize pointer variable


int num = 10;
int *ptr = &num; // Assign address of num to ptr

// You can also assign a pointer variable to another


int *ptr1 = ptr; // Initialize pointer using another pointer
Write a C program to demonstrate the use of pointers in C programming.
#include <stdio.h>
int main()
{
int num = 1;
int *ptr = &num; // ptr points to num

printf("Value of num = %d \n", num);


printf("Address of num = %x \n\n", &num);

printf("Value of ptr = %x \n", ptr);


printf("Address of ptr = %x \n", &ptr);
printf("Value pointed by ptr = %d \n\n", *ptr);

/* Change the value of num directly */


num = 10;
printf("After changing value of num directly. \n");
printf("Value of num = %d \n", num);
printf("Value pointed by ptr = %d \n\n", *ptr);

/* Assigns 100 at the address pointed by ptr */


*ptr = 100;
printf("After changing value pointed by ptr. \n");
printf("Value of num = %d \n", num);
printf("Value pointed by ptr = %d \n", *ptr);

return 0;
}

Output –

Value of num = 1
Address of num = 60ff0c
Value of ptr = 60ff0c
Address of ptr = 60ff08
Value pointed by ptr = 1
After changing value of num directly.
Value of num = 10
Value pointed by ptr = 10
After changing value pointed by ptr.
Value of num = 100
Value pointed by ptr = 100
Accessing a variable through its pointer

Accessing a variable through its pointer involves dereferencing the pointer, which means
accessing the value stored at the memory address pointed to by the pointer.

Here’s how you can access a variable through its pointer in C:


#include <stdio.h>
int main()
{
int x = 10; // Define an integer variable ‘x’
int *ptr = &x; // Define a pointer ‘ptr’ and initialize it with the address of ‘x’
// Accessing the value of ‘x’ through its pointer ‘ptr’
printf(“Value of x: %d\n”, *ptr);
return 0;
}

In this example:

 We define an integer variable x and initialize it with the value 10.


 We define a pointer variable ptr and initialize it with the address of the variable x using the
“address-of” operator (&).
 To access the value of x through its pointer ptr, we use the dereference operator (*ptr). This
retrieves the value stored at the memory address pointed to by ptr.
 We then print the value of x using *ptr.
When you run this program, it will print the value of x, which is 10, obtained through its
pointer ptr.

It’s important to note that dereferencing a pointer when it’s not pointing to a valid memory
address (e.g., when it’s uninitialized or pointing to NULL) leads to undefined behavior,
which may result in a segmentation fault or other runtime errors. Always ensure that the
pointer is properly initialized and points to a valid memory location before dereferencing it.

Static and Dynamic Memory Allocation in C

Memory is divided into smaller addressable units called bytes. Assume that these are small
boxes as bytes. Each byte has its own address as per the below table.For example: 0, 1, 2,
3, 4, 5, 6, etc.
The Memory is divided into three sections.
 Heap Memory: It is a part of the main memory. It is unorganized and treated as a
resource when you require the use of it if not release. Heap memory can’t be used
directly with the help of a pointer.
 Stack Memory: It stores temporary variables created by a function. In stack, variables
are declared, stored, and initialized during runtime. It follows the First in last out
method that means whatever element is going to store last is going to delete first when
it’s not in use.
 Code Section: Whenever the program is executed it will be brought into the main
memory. This program will get stored under the code section. Based upon the program
it will decide whether to utilize the stack or heap sections.

Below is the image to illustrate how the program uses memory:

Static Memory Allocation

In static memory allocation whenever the program executes it fixes the size that the
program is going to take, and it can’t be changed further. So, the exact memory
requirements must be known before. Allocation and deallocation of memory will be done
by the compiler automatically. When everything is done at compile time (or) before run
time, it is called static memory allocation.

Key Features:
 Allocation and deallocation are done by the compiler.
 It uses a data structures stack for static memory allocation.
 Variables get allocated permanently.
 No reusability.
 Execution is faster than dynamic memory allocation.
 Memory is allocated before runtime.
 It is less efficient.
C program to illustrate the Static Memory Allocation:

// C program to implement
// static memory allocation
#include <stdio.h>
#include <stdlib.h>

// Driver code
int main()
{
int size;
printf("Enter limit of the text: \n");
scanf("%d", &size);
char str[size];
printf("Enter some text: \n");
scanf(" ");
gets(str);
printf("Inputted text is: %s\n", str);
return 0;
}

Input

a Output:

V Advantages:
 Simple usage.
 Allocation and deallocation are done by the compiler.
 Efficient execution time.
 It uses stack data structures .

Disadvantages:
 Memory wastage problem.
 Exact memory requirements must be known.
 Memory can’t be resized once after initialization.

Dynamic Memory Allocation

In Dynamic memory allocation size initialization and allocation are done by the
programmer. It is managed and served with pointers that point to the newly allocated
memory space in an area which we call the heap. Heap memory is unorganized and it is
treated as a resource when you require the use of it if not release it. When everything is
done during run time or execution time it is known as Dynamic memory allocation.
Key Features:
 Dynamic allocated at runtime
 We can also reallocate memory size if needed.
 Dynamic Allocation is done at run time.
 No memory wastage
There are some functions available in the stdlib.h header which will help to allocate
memory dynamically.

 malloc(): The simplest function that allocates memory at runtime is called malloc().
There is a need to specify the number of bytes of memory that are required to be
allocated as the argument returns the address of the first byte of memory that is
allocated because you get an address returned, a pointer is the only place to put it.

Syntax:
int *p = (int*)malloc(No of values*size(int));
The argument to malloc() above clearly indicates that sufficient bytes for accommodating
the number of values of type int should be made available. Also notice the cast (int*),
which converts the address returned by the function to the type pointer to int. malloc()
function returns a pointer with the value NULL.

 calloc(): The calloc() function offers a couple of advantages over malloc() . It allocates
memory as a number of elements of a given size. It initializes the memory that is
allocated so that all bytes are zero. calloc() function requires two argument values:
o The number of data items for which space is required.
o Size of each data item.
It is very similar to using malloc() but the big plus is that you know the memory area will
be initialized to zero.
Syntax:
int *p = (int*)calloc(Number of data items, sizeof(int));
 realloc(): The realloc() function enables you to reuse or extend the memory that you
previously allocated using malloc() or calloc(). A pointer containing an address that was
previously returned by a call to malloc(), calloc(). The size in bytes of the new memory
that needs to be allocated. It allocates the memory specified by the second argument and
transfers the contents of the previously allocated memory referenced by the pointer
passed as the first argument to the newly allocated memory.

Syntax:
int *np = (type cast) realloc (previous pointer type, new number of elements * sizeof(int));

. free(): When memory is allocated dynamically it should always be released when it is no


longer required. Memory allocated on the heap will be automatically released when the
program ends but is always better to explicitly release the memory when done with it, even
if it’s just before exiting from the program. A memory leak occurs memory is allocated
dynamically and reference to it is not retained, due to which unable to release the memory.

Syntax:
free(pointer);

For Example:
// C program to illustrate the concept
// of memory allocation
#include <iostream>
using namespace std;
// Driver Code
void main()
{
int* p; // 2 bytes
P = (int*)malloc(5 * sizeof(int));
}

Examples:
 In the above piece of code, a pointer p is declared. Assume that pointer p will take 2
bytes of memory and again it depends upon the compiler.
 This pointer will store in the stack section and will point to the array address of the first
index which is allocated in the heap. Heap memory cannot be used directly but with the
help of the pointer, it can be accessed.

 When the program is not in use, the memory should be deallocated. Otherwise, it will
cause a memory leak.

 After deallocating the memory allocated in the heap. Below is the image to illustrate the
main memory after deallocation.
Below is the C program to illustrate the Dynamic Memory Allocation:

 C

// C program to illustrate the above

// concepts of memory allocation

#include <stdio.h>

#include <stdlib.h>

// Driver Code

int main()

int size, resize;

char* str = NULL;

printf("Enter limit of the "

"text: \n");
scanf("%d", &size);

str = (char*)malloc(size * sizeof(char));

// If str is not NULL

if (str != NULL) {

printf("Enter some text: \n");

scanf(" ");

gets(str);

printf("Inputted text by allocating"

"memory using malloc() is: "

"%s\n",

str);

// Free the memory

free(str);

str = (char*)calloc(50, sizeof(char));

// If str is not NULL

if (str != NULL) {

printf("Enter some text: \n");

scanf(" ");

gets(str);
printf("Inputted text by allocating "

"memory using calloc() is: "

"%s\n",

str);

printf("Enter the new size: \n");

scanf("%d", &resize);

str = (char*)realloc(str, resize * sizeof(char));

printf("Memory is successfully "

"reallocated by using "

"realloc() \n");

// If str is not NULL

if (str != NULL) {

printf("Enter some text: \n");

scanf(" ");

gets(str);

printf("Inputted text by reallocating"

" memory using realloc()is: "

"%s\n",

str);
}

// Free the memory

free(str);

str = NULL;

return 0;

Advantages:
 Dynamic Allocation is done at run time.
 We can allocate (create) additional storage whenever we need them.
 Memory can be deallocated (free/delete) dynamic space whenever we are done with
them.
 Thus, one can always have exactly the amount of space required – no more, no less.
 Memory size can be reallocated if needed.

Disadvantages:
 As the memory is allocated during runtime, it requires more time.
 Memory needs to be freed by the user when done. This is important as it is more likely
to turn into bugs that are difficult to find.
Differences between Static and Dynamic memory allocation

ACCESSING A VARIABLE THROUGH ITS POINTER INVOLVES DEREFERENCING


THE POINTER, WHICH MEANS ACCESSING THE VALUE STORED AT THE MEMORY
ADDRESS POINTED TO BY THE POINTER.
S.No Static Memory Allocation Dynamic Memory Allocation

In the Dynamic memory allocation, the


In the static memory allocation,
memory is controlled by the programmer. It
variables get allocated
1 gets allocated whenever a malloc() is
permanently, till the program
executed gets deallocated wherever the free()
executes or function call finishes.
is executed.

Static Memory Allocation is done Dynamic Memory Allocation is done during


2
before program execution. program execution.

It uses heap (not heap data structure) of


It uses stack for managing the
3 memory for managing the dynamic allocation
static allocation of memory
of memory

4 It is less efficient It is more efficient

In Dynamic Memory Allocation, there is


In Static Memory Allocation,
5 memory re-usability and memory can be
there is no memory re-usability
freed when not required

In static memory allocation, once In dynamic memory allocation, when


6 the memory is allocated, the memory is allocated the memory size can be
memory size can not change. changed.

This allows reusing the memory. The user can


In this memory allocation scheme,
allocate more memory when required. Also,
7 we cannot reuse the unused
the user can release the memory when the
memory.
user needs it.

In this memory allocation scheme,


In this memory allocation scheme, execution
8 execution is faster than dynamic
is slower than static memory allocation.
memory allocation.

In this memory is allocated at


9 In this memory is allocated at run time.
compile time.

In this allocated memory remains In this allocated memory can be released at


10
from start to end of the program. any time during the program.

Example: This static memory


Example: This dynamic memory allocation is
11 allocation is generally used
generally used for linked list.
INTRODUCTION TO DATA STRUCTURE

The data structure name indicates itself that organizing the data in memory. There are many
ways of organizing the data in the memory as we have already seen one of the data structures,
i.e., array in C language. Array is a collection of memory elements in which data is stored
sequentially, i.e., one after another. In other words, we can say that array stores the elements
in a continuous manner. This organization of data is done with the help of an array of data
structures. There are also other ways to organize the data in memory. Let's see the different
types of data structures.

The data structure is not any programming language like C, C++, java, etc. It is a set of
algorithms that we can use in any programming language to structure the data in the memory.

To structure the data in memory, 'n' number of algorithms were proposed, and all these
algorithms are known as Abstract data types. These abstract data types are the set of rules.

Data Structures are mainly classified into two categories:

1. Primitive Data Structures: They store the data of only one type i.e. built-in data
types. Data types like integer, float, character, and booleans come in this category.
2. Non-Primitive Data Structures: They can store the data of more than one type
i.e. derived data types. Data types like arrays, linked lists, trees, etc. come in this
category. Non-Primitive Data Structures are data structures derived from Primitive
Data Structures.

Based on the structure and arrangement of data, these data structures are further
divided into two categories

1. Linear Data Structures: The data in this data structure is arranged in a


sequence, one after the other i.e. each element appears to be connected
linearly.
Based on the memory allocation, the Linear Data Structures are again
classified into two types:

1. Static Data Structures: They have a fixed size. The memory is


allocated at the compile time, and its size cannot be changed by the
user after being compiled; however, the data can be modified. e.g.
array
2. Dynamic Data Structures: They don't have a fixed size. The memory
is allocated at the run time, and its size varies during the execution of
the program. Moreover, the user can modify the size as well as the data
elements stored at the run time. e.g. linked list, stack, queue
2. Non-Linear Data Structures: The data in this data structure are not arranged
in a sequence. They form a hierarchy i.e. the data elements have multiple ways
to connect to other elements. e.g. tree and graph

Primitive Data Structures


Primitive Data Structures are the most basic types of data structures that serve as the
foundation for creating more complex data structures. These data types are directly supported
by the computer's hardware and have a fixed size and operations that can be performed on
them.

Characteristics of Primitive Data Structures

 Simple: They represent simple values and are not derived from other data structures.
 Predefined: They are predefined in most programming languages and are handled
directly by the machine's CPU.
 Efficient: They offer simple operations and are memory-efficient because they
directly store the data.

Types of Primitive Data Structures

 1. Integer
 2. Float (or Double)
 3. Character (Char)
 4. Boolean
 6. Pointers

1. Integer

An Integer data type that represents whole numbers, both positive and negative, without any
fractional part.

 Example: int a = 5;
 Operations: Arithmetic operations like addition, subtraction, multiplication, division.

2. Float (or Double)

A Float data type is used to represent numbers with fractional parts (decimals).
 Example: float b = 5.75;
 Operations: Arithmetic operations, rounding, and truncation.

3. Character (Char)

Character represents a single character (like a letter, symbol, or digit).

 Example: char c = 'A';


 Operations: Comparison, arithmetic with ASCII values, concatenation.

4. Boolean

Boolean represents one of two values: true or false.

 Example: bool isActive = true;


 Operations: Logical operations like AND, OR, NOT.

5. String

The string is asequence of characters (used to represent text).

 Example: string name = "John";


 Operations: Concatenation, substring extraction, length calculation.

6. Pointers (in languages like C and C++)

Pointers are variables that store the memory address of another variable.

 Example: int *ptr = &a;


 Operations: Dereferencing, address manipulation.

Advantages of Primitive Data Structures

 Efficiency: Since they are simple and directly supported by hardware, they are fast
and efficient in terms of memory and processing power.
 Ease of Use: Simple and easy to understand as they don’t involve complex
relationships between elements.
 Fixed Size: Their size is usually fixed, leading to predictable memory usage.

Disadvantages of Primitive Data Structures

 Limited Functionality: As they represent basic data types, they don’t support
complex structures or operations.
 No Relationships: They do not allow representation of relationships between data,
unlike complex data structures (like arrays or trees).

Applications of Primitive Data Structures

 Memory Storage: Storing basic types like numbers or text.


 Mathematical Operations: Performing basic arithmetic, and logical operations.
 Control Flow: Using Boolean values for decisions in programs (like in if-else
conditions).

Non-Primitive Data Structures

 Linear Data Structures


 Non-linear Data Structures

Types of Linear Data structure

1. Arrays

An array is a collection of elements, all of the same data type, stored in contiguous memory
locations. It has a fixed size, which is specified during its creation.

Characteristics:

 Fixed-size (cannot grow or shrink dynamically).


 Indexed collection, meaning elements are accessed using an index.
 Memory-efficient for storing large datasets of the same type.

Applications: Used to store data in a database, matrices, and images (in terms of pixels).
Operations:

 Access: Direct access to any element using its index.


 Search: Searching an element by value.
 Insert/Delete: Insertions and deletions are inefficient as elements may need to be
shifted.

Advantages: Fast access via index (constant time). Simple implementation.


Disadvantages: Fixed size makes it less flexible. Insertion and deletion operations can be
inefficient.

2. Linked Lists

A linked list is a collection of nodes, where each node stores a data element and a reference
(pointer) to the next node in the sequence. It is a dynamic data structure with no fixed size.
Characteristics:

 Dynamic size (can grow or shrink as needed).


 Each node points to the next, with no contiguous memory allocation.

Applications: Used in memory management systems, and implementation of stacks, queues,


and graphs.
Operations:

 Traversal: Visiting each node from the head to the tail.


 Insertion/Deletion: Inserting or deleting nodes can be done efficiently by updating
pointers.

Advantages: Dynamic size allows for efficient memory usage. Insertions and deletions are
efficient at both ends (front or back).
Disadvantages: Sequential access (no direct access to elements by index). Extra memory is
required for storing pointers

3 Stacks

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, where
the last element added is the first one to be removed.

// ACCESSING THE VALUE OF ‘X’ THROUGH ITS POINTER ‘PTR’

“VALUE OF X: %D\N”, *PTR);


RETU

Characteristics:

 Only two operations are allowed: push (add an element to the top) and pop (remove
the top element).

Applications: Function call management in recursion, expression evaluation (infix, postfix,


and prefix), and undo functionality in applications.
Operations:

 Push: Add an element to the top of the stack.


 Pop: Remove the top element from the stack.
 Peek: View the top element without removing it.

Advantages: Simple and fast for certain tasks like undo operations and parsing expressions.
Disadvantages: Limited access to other elements (only the top element is accessible). Fixed-
size in an array-based implementation.

4. Queues

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, where
the first element added is the first one to be removed.

R
N 0;
 Operates with two main pointers: front (removes elements) and rear (adds elements).
 Efficient for processing tasks in order.

Applications: CPU scheduling, printer task scheduling, handling requests in web servers.
Operations:

 Enqueue: Add an element to the rear of the queue.


 Dequeue: Remove an element from the front of the queue.
 Peek: View the front element.

Advantages: Useful for handling tasks in a predictable order.


Disadvantages: Fixed size in static implementations. Inefficient for random access.

Read More: Queue in Data Structures - Types & Algorithm (With Example)

5. Deque (Double-Ended Queue)

Definition: A deque is a linear data structure that allows elements to be added or removed
from both ends (front and rear).
Characteristics:

 Supports operations at both ends.


 More flexible than a standard queue.

Applications: Sliding window problems, caching algorithms.


Operations:

 Insert Front/Rear: Add elements at either end.


 Delete Front/Rear: Remove elements from either end.

Advantages: More flexibility than queues or stacks.


Disadvantages: Increased complexity in implementation.
Types of Non-Linear Data Structures

1. Trees

A tree is a hierarchical structure that consists of nodes, with each node containing data and
references to its child nodes. The topmost node is called the root, and each node can have
zero or more child nodes.

Characteristics:

 Root nodes, parent nodes,and child nodes.


 No cycles (acyclic).
 Recursive structure, as each child node can also have its own sub-tree.

Applications: File systems (directories and subdirectories), decision-making algorithms


(decision trees), binary search trees for efficient searching and sorting. }
Operations:

 Insertion/Deletion: Insert or delete nodes while maintaining the hierarchical


structure.
 Traversal: Visiting each node (pre-order, in-order, post-order).

Advantages: Efficient searching and sorting (in balanced trees like AVL trees and red-black
trees).
Disadvantages: Complex implementation, especially for balanced trees.

2. Graphs

A graph is a collection of nodes (vertices) and edges, where edges represent the relationships
between nodes. Graphs can be directed or undirected.

Characteristics:

 Vertices (nodes) and edges (connections between nodes).


 It can be cyclic or acyclic, directed or undirected.

Applications: Social networks (users and their connections), shortest path problems (e.g.,
Dijkstra's algorithm), web page linking (web graphs).
Operations:
 Add/Remove Vertices and Edges: Modify the structure of the graph.
 Traversal: Visit all nodes via BFS (Breadth-First Search) or DFS (Depth-First
Search).

Advantages: Can model complex relationships effectively.


Disadvantages: Memory-intensive and complex to implement, especially for large graphs.

Linear Vs Non-linear Data Structures

Characteristics Linear Data Structure Non-linear Data structure

Arrangement of The data is consecutively stored one after the The data is stored in a non-sequential
data other. (hierarchical) manner.

Each data stored is directly linked to its The data is stored in a random manner or
Storage of data
previous and next elements. contiguous memory locations.

Layers of data The data is arranged in a single layer. The data is arranged in multiple layers.

The time required to access all the data in the The time required to access all the data in the
Time
data structure increases as the volume of the data structure remains the same even if the
complexity
data increases. volume of the data increases.

Number of The user can traverse the data structure in a The user cannot traverse the data structure in a
traverses single pass. single pass.

Irrespective of the volume of the data, these Irrespective of the volume of the data, these
Advantages data structures are easy to use where the data structures are easy to use where the
operations are simple. operations are complex.

Memory usage Inefficient memory usage Efficient memory usage

Linear data structures are widely used in Non-linear data structures are widely used in
Uses
software development. Artificial intelligence, image processing, etc.

Array, Stack, Queue, Linked Lists, etc. Each


Trees, Graphs, Tables, Sets, etc. Each of these
Examples of these data structures can be further data structures can be further subdivided into its
subdivided into its types. types.
IN THIS EXAMPLE
ER VARIABLE 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 order.
T
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 operations are
D
typically performed on nodes that are no longer needed.

 ‍Search

‍ earch operations are used to find a specific data element in a data structure. These
S
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 specific
S
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 operation is
M
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 done by
C
copying each element in the original data structure to the new one.
X AND REVIEW OF AN ARRAY

Array in C is one of the most used data structures in C programming. It is a simple and fast
way of storing multiple values under a single name. In this article, we will study the
different aspects of array in C language such as array declaration, definition, initialization,
types of arrays, array syntax, advantages and disadvantages, and many more.
What is Array in C?
An array in C is a fixed-size collection of similar data items stored in contiguous memory
locations. It can be used to store the collection of primitive data types such as int, char,
float, etc., and also derived and user-defined data types such as pointers, structure,

C Array Declaration
In C, we have to declare the array like any other variable before using it. We can declare an
array by specifying its name, the type of its elements, and the size of its dimensions. When
we declare an array in C, the compiler allocates the memory block of the specified size to
the array name.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
where N is the number of dimensions.

The C arrays are static in nature, i.e., they are allocated memory at the compile
time.
Example of Array Declaration
// declaring array of integers
int arr_int[5];

// declaring array of characters


char arr_char[5];
THROUGH ITS
POINTER PTR.
IT’S IMPORTANT TO NOTE THAT DEREFERENCING A POINTER WHEN IT’S NOT
The C arrays are static in nature, i.e., they are allocated memory at the compile time.
Example of Array Declaration
// declaring array of integers
int arr_int[5];

// declaring array of characters


char arr_char[5];
OINTING TO A VALID MEMORY ADDRESS (E.G., WHEN IT’S UNINITIALIZED OR
POIArray Initialization with Declaration
In this method, we initialize the array along with its declaration. We use an initializer list to
initialize multiple elements of the array. An initializer list is the list of values enclosed
within braces { } separated b a comma.
data_type array_name [size] = {value1, value2, ... valueN};
TO NULL) LEADS TO UNDEFINED BEHAVIOR, WHICH MAY RESULT IN A
SEGMENTATION FAULT OR OTHER RUNTIME ERRORS. ALWAYS ENSURE THAT
THE POINTER IS PROPERLY INITIALIZED AND POINTS TO A VALID MEMORY
LOCORE DEREFERENCING IT.

O HOMEPAGE
O CONTACT US
O ABOUT US
O

You might also like