Technocrats Institute of Technology, Bhopal
Technocrats Institute of Technology, Bhopal
According to the data types, the data structure is divided into two categories,
namely:
▪ Primitive Data Structures
▪ Non-Primitive Data Structures
➢ Primitive Data Structures:
This type of data structure stores the value of a particular data type. For example, an
integer data structure can store the value of only an integer. A primitive data
structure cannot be NULL.
❖ The following are the primitive data structures:
▪ Integer: It represents a whole number data type with no decimal places.
For example, int num = 30
▪ Float: It is a data type with decimal precision.
For example, float num = 2346.9875
▪ Character: It represents a single character.
For example, char name = “M”
▪ Boolean: It returns true or false values for a condition specified.
For example, boolean value = “true”
➢ Non-Primitive Data Structures:
These data structures are capable of storing values of more than one data type. The
size of the non-primitive data structure depends on the type of data it will store. For
example, a list (a non-primitive data structure) can store the values of various data
types. It can contain a NULL value.
➢ The following are some of the non-primitive data structures:
▪ List: A list is a linear type of data structure. This data structure holds an ordered list of
elements.
For example,
list_of_networks [“Airtel”, ”Jio”, “Idea”]
▪ Arrays: An array is also a linear data structure that stores a collection of data having
the same data type. It has a fixed size, which means it stores data in a contagious
memory location of the same data type (int, float, boolean, text, and others).
For example,
int[] numbers = {1, 2, 3, 4, 5}
▪ Queue: A Queue is a data structure that arranges items in a specific order and
follows the First-In-First-Out (FIFO) method for accessing elements. It is commonly
employed in building priority queuing systems and handling threads in multithreading
scenarios.
For example,
String[] names = {"Alice", "Charlie", "David", "Emily"};
▪ Stack: Based on the principle ‘last in, first out’ or LIFO, a stack is an abstract data type
that is composed of homogenous pieces. It is used for push and pop operations that
are applied on top of the stack. While the pop operation is responsible for removing
an element from the top spot, the push operation adds an element to the stack.
For example,
stack = [1,2,3,4,5]
▪ Tree: This is a non-linear and hierarchical data structure. A tree is a hierarchical
structure with nodes connected by edges, with the root being the top node and child
nodes below it.
For example,
tree = Tree(1, Tree(2, 2.1, 2.2), Tree(3, 3.1))
➢ Linear Data Structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and next
adjacent elements, is called a linear data structure.
Example: Array, Stack, Queue, Linked List, etc.
➢ Static Data Structure: Static data structure has a fixed memory size. It is easier to
access the elements in a static data structure.
Example: array.
➢ Dynamic Data Structure: In dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime which may be considered efficient concerning
the memory (space) complexity of the code.
Example: Queue, Stack, etc.
➢ Non-Linear Data Structure: Data structures where data elements are not placed
sequentially or linearly are called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single run only.
Examples: Trees and Graphs.
➢ Need Of Data structure :
1. Data structure modification is easy.
2. It requires less time.
3. Save storage memory space.
4. Data representation is easy.
5. Easy access to the large database.
➢ Advantages & Disadvantages of Data Structure:
➢ Advantages of Data Structure:
Data structures offer a wide range of benefits for creating and maintaining code in
programming languages. Some of these advantages are:
1) Efficient Storage
Data structures provide efficient storage by organizing the data effectively, which facilitates
quick retrieval and maintenance of the data in the system. The memory allocation takes
place according to the data types used in the data structure.
3) Develop Algorithms
Algorithms for data structures help organize and access information in a structured manner.
These algorithms take into account the format of the data as well as any actions that can be
performed on it. Their goal is to find the most efficient way to store and manipulate data
within the structure while also allowing for easy navigation. By utilizing these algorithms,
complex issues can be resolved with efficiency.
4) Reusability of Data
One of the fundamental advantages of data structure is that it offers data reusability. It
enables the creation of data in specific formats, which can then be stored in libraries,
allowing different clients to utilize and access the data as needed. Therefore, data can be
reused in multiple ways and purposes. This makes it easier to create efficient and dynamic
algorithms that can be used for different applications.
Name Expresses
LLONG_MIN The minimum value for an object of type long long int
ULLONG_MAX Maximum value for an object of type Unsigned long long int
The data type is the form of a variable to Data structure is a collection of different
which a value can be assigned. It defines kinds of data. That entire data can be
Data Type Data Structure
that the particular variable will assign the represented using an object and can be
values of the given data type only. used throughout the program.
It can hold value but not data. Therefore, It can hold multiple types of data within a
it is dataless. single object.
There is no time complexity in the case of In data structure objects, time complexity
data types. plays an important role.
Data type examples are int, float, double, Data structure examples are stack, queue,
etc. tree, etc.
• Linear data structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and next
adjacent elements, is called a linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
o Static data structure: Static data structure has a fixed memory size. It is
easier to access the elements in a static data structure.
An example of this data structure is an array.
o Dynamic data structure: In the dynamic data structure, the size is not fixed.
It can be randomly updated during the runtime which may be considered
efficient concerning the memory (space) complexity of the code.
Examples of this data structure are queue, stack, etc.
• Non-linear data structure: Data structures where data elements are not placed
sequentially or linearly are called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single run only.
Examples of non-linear data structures are trees and graphs .
Arrays:
• An array is a linear data structure and it is a collection of items stored at contiguous
memory locations. The idea is to store multiple items of the same type together in
one place. It allows the processing of a large amount of data in a relatively short
period. The first element of the array is indexed by a subscript of 0. There are
different operations possible in an array, like Searching, Sorting, Inserting,
Traversing, Reversing, and Deleting.
➢ Characteristics of an Array:
An array has various characteristics which are as follows:
• Homogeneous Elements: All elements within an array must be of the same data type.
• Contiguous Memory Allocation: In most programming languages, elements in an
array are stored in contiguous (adjacent) memory locations.
• Zero-Based Indexing: In many programming languages, arrays use zero-based
indexing, which means that the first element is accessed with an index of 0, the
second with an index of 1, and so on.
• Random Access: Arrays provide constant-time (O(1)) access to elements. This means
that regardless of the size of the array, it takes the same amount of time to access
any element based on its index.
• Arrays use an index-based data structure which helps to identify each of the elements
in an array easily using the index.
• If a user wants to store multiple values of the same data type, then the array can be
utilized efficiently.
• An array can also handle complex data structures by storing data in a two-dimensional
array.
• An array is also used to implement other data structures like Stacks, Queues, Heaps,
Hash tables, etc.
• The search process in an array can be done very easily.
• Accessing elements: Elements in an array can be accessed by their index, which starts
from 0 and goes up to the size of the array minus one.
• Searching for elements: Arrays can be searched for a specific element using linear
search or binary search algorithms.
• Sorting elements: Elements in an array can be sorted in ascending or descending
order using algorithms like bubble sort, insertion sort, or quick sort.
• Inserting elements: Elements can be inserted into an array at a specific location, but
this operation can be time-consuming because it requires shifting existing elements in
the array.
• Deleting elements: Elements can be deleted from an array by shifting the elements
that come after it to fill the gap.
• Updating elements: Elements in an array can be updated or modified by assigning a
new value to a specific index.
• Traversing elements: The elements in an array can be traversed in order, visiting each
element once.
➢ Applications of Array:
Different applications of an array are as follows:
• An array is used in solving matrix problems.
• Database records are also implemented by an array.
• It helps in implementing a sorting algorithm.
• It is also used to implement other data structures like Stacks, Queues, Heaps, Hash
tables, etc.
• An array can be used for CPU scheduling.
• Can be applied as a lookup table in computers.
• Arrays can be used in speech processing where every speech signal is an array.
• The screen of the computer is also displayed by an array. Here we use a
multidimensional array.
• The array is used in many management systems like a library, students, parliament,
etc.
• The array is used in the online ticket booking system. Contacts on a cell phone are
displayed by this array.
• In games like online chess, where the player can store his past moves as well as
current moves. It indicates a hint of position.
• To save images in a specific dimension in the android Like 360*1200
➢ Real-Life Applications of Array:
• An array is frequently used to store data for mathematical computations.
• It is used in image processing.
• It is also used in record management.
• Book pages are also real-life examples of an array.
• It is used in ordering boxes as well.
➢ Types of arrays:
• One-Dimensional Array: This is the simplest form of an array, which consists of a
single row of elements, all of the same data type. Elements in a 1D array are accessed
using a single index.
• Multi-Dimensional Array: Arrays can have more than two dimensions, leading to
multi-dimensional arrays. These are used when data needs to be organized in a multi-
dimensional grid.
2. Linked List
A Linked List is a linear data structure which looks like a chain of nodes, where each node
contains a data field and a reference(link) to the next node in the list. Unlike Arrays,
Linked List elements are not stored at a contiguous location.
Common Features of Linked List:
• Node: Each element in a linked list is represented by a node, which contains two
components:
o Data: The actual data or value associated with the element.
o Next Pointer(or Link): A reference or pointer to the next node in the linked
list.
• Head: The first node in a linked list is called the “head.” It serves as the starting point
for traversing the list.
• Tail: The last node in a linked list is called the “tail.”
Types of Linked Lists:
• Singly-linked list
• Doubly linked list
• Circular linked list
• Doubly circular linked list
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle,
meaning that the last element added to the stack is the first one to be removed.
➢ Types of Stacks:
• Fixed Size Stack: As the name suggests, a fixed size stack has a fixed size and cannot
grow or shrink dynamically. If the stack is full and an attempt is made to add an
element to it, an overflow error occurs. If the stack is empty and an attempt is made
to remove an element from it, an underflow error occurs.
• Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When the
stack is full, it automatically increases its size to accommodate the new element, and
when the stack is empty, it decreases its size. This type of stack is implemented using
a linked list, as it allows for easy resizing of the stack.
➢ Stack Operations:
• push(): When this operation is performed, an element is inserted into the stack.
• pop(): When this operation is performed, an element is removed from the top of the
stack and is returned.
• top(): This operation will return the last inserted element that is at the top without
removing it.
• size(): This operation will return the size of the stack i.e. the total number of elements
present in the stack.
• isEmpty(): This operation indicates whether the stack is empty or not.
Characteristics of a Stack:
Stack has various different characteristics which are as follows:
• Stack is used in many different algorithms like Tower of Hanoi, tree traversal,
recursion, etc.
• Stack is implemented through an array or linked list.
• It follows the Last In First Out operation i.e., an element that is inserted first will pop
in last and vice versa.
• The insertion and deletion are performed at one end i.e. from the top of the stack.
• In stack, if the allocated space for the stack is full, and still anyone attempts to add
more elements, it will lead to stack overflow.
Applications of Stack:
Different applications of Stack are as follows:
• The stack data structure is used in the evaluation and conversion of arithmetic
expressions.
• It is used for parenthesis checking.
• While reversing a string, the stack is used as well.
• Stack is used in memory management.
• It is also used for processing function calls.
• The stack is used to convert expressions from infix to postfix.
• The stack is used to perform undo as well as redo operations in word processors.
• The stack is used in virtual machines like JVM.
• The stack is used in the media players. Useful to play the next and previous song.
• The stack is used in recursion operations.
➢ Types of Queue:
• Input Restricted Queue: This is a simple queue. In this type of queue, the input can
be taken from only one end but deletion can be done from any of the ends.
• Output Restricted Queue: This is also a simple queue. In this type of queue, the input
can be taken from both ends but deletion can be done from only one end.
• Circular Queue: This is a special type of queue where the last position is connected
back to the first position. Here also the operations are performed in FIFO order. To
know more refer this.
• Double-Ended Queue (Dequeue): In a double-ended queue the insertion and
deletion operations, both can be performed from both ends. To know more refer this.
• Priority Queue: A priority queue is a special queue where the elements are accessed
based on the priority assigned to them. To know more refer this.
➢ Operation performed on queue:
• Enqueue(): Adds (or stores) an element to the end of the queue..
• Dequeue(): Removal of elements from the queue.
• Peek() or front(): Acquires the data element available at the front node of the queue
without deleting it.
• rear(): This operation returns the element at the rear end without removing it.
• isFull(): Validates if the queue is full.
• isNull(): Checks if the queue is empty.
➢ Characteristics of a Queue:
• A tree is a non-linear and hierarchical data structure where the elements are
arranged in a tree-like structure. In a tree, the topmost node is called the root
node. Each node contains some data, and data can be of any type. It consists of a
central node, structural nodes, and sub-nodes which are connected via edges.
Different tree data structures allow quicker and easier access to the data as it is a
non-linear data structure. A tree has various terminologies like Node, Root, Edge,
Height of a tree, Degree of a tree, etc.
➢ There are different types of Tree-like
1) Binary Tree,
2) Binary Search Tree,
3) AVL Tree,
4) B-Tree, etc.
➢ Characteristics of a Tree:
The tree has various different characteristics which are as follows:
• A tree is also known as a Recursive data structure.
• In a tree, the Height of the root can be defined as the longest path from the root
node to the leaf node.
• In a tree, one can also calculate the depth from the top to any node. The root node
has a depth of 0.
➢ Applications of Tree:
Different applications of Tree are as follows:
• Heap is a tree data structure that is implemented using arrays and used to implement
priority queues.
• B-Tree and B+ Tree are used to implement indexing in databases.
• Syntax Tree helps in scanning, parsing, generation of code, and evaluation of
arithmetic expressions in Compiler design.
• K-D Tree is a space partitioning tree used to organize points in K-dimensional space.
• Spanning trees are used in routers in computer networks.
➢ Operation performed on tree:
A tree is a non-linear data structure that consists of nodes connected by edges. Here
are some common operations performed on trees:
• Insertion: New nodes can be added to the tree to create a new branch or to increase
the height of the tree.
• Deletion: Nodes can be removed from the tree by updating the references of the
parent node to remove the reference to the current node.
• Search: Elements can be searched for in a tree by starting from the root node and
traversing the tree based on the value of the current node until the desired node is
found.
• Traversal: The elements in a tree can be traversed in several different ways, including
in-order, pre-order, and post-order traversal.
• Height: The height of the tree can be determined by counting the number of edges
from the root node to the furthest leaf node.
• Depth: The depth of a node can be determined by counting the number of edges
from the root node to the current node.
• Balancing: The tree can be balanced to ensure that the height of the tree is
minimized and the distribution of nodes is as even as possible.
➢ Binary Tree:
• Binary tree is a tree data structure(non-linear) in which each node can
have at most two children which are referred to as the left child and
the right child.
• The topmost node in a binary tree is called the root, and the bottom-most
nodes are called leaves. A binary tree can be visualized as a hierarchical
structure with the root at the top and the leaves at the bottom.