0% found this document useful (0 votes)
7 views23 pages

Data Structure Module 1

The document provides an overview of data structures, explaining their significance in organizing and processing data effectively. It categorizes data structures into primitive and non-primitive types, detailing linear structures like arrays, linked lists, stacks, and queues, as well as non-linear structures such as trees and graphs. Key features, objectives, and applications of these data structures are also discussed to highlight their importance in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Data Structure Module 1

The document provides an overview of data structures, explaining their significance in organizing and processing data effectively. It categorizes data structures into primitive and non-primitive types, detailing linear structures like arrays, linked lists, stacks, and queues, as well as non-linear structures such as trees and graphs. Key features, objectives, and applications of these data structures are also discussed to highlight their importance in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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.

Why should we learn Data Structures?

1. Data Structures and Algorithms are two of the key aspects of


Computer Science.
2. Data Structures allow us to organize and store data, whereas
Algorithms allow us to process that data meaningfully.
3. Learning Data Structures and Algorithms will help us become better
Programmers.
4. We will be able to write code that is more effective and reliable.
5. We will also be able to solve problems more quickly and efficiently.
Objectives of Data Structures
Data Structures satisfy two complementary objectives:

1. Correctness: Data Structures are designed to operate correctly for


all kinds of inputs based on the domain of interest. In order words,
correctness forms the primary objective of Data Structure, which
always depends upon the problems that the Data Structure is meant
to solve.
2. Efficiency: Data Structures also requires to be efficient. It should
process the data quickly without utilizing many computer resources
like memory space. In a real-time state, the efficiency of a data
structure is a key factor in determining the success and failure of the
process.

Some Key Features of Data Structures


Some of the Significant Features of Data Structures are:

1. Robustness: Generally, all computer programmers aim to produce


software that yields correct output for every possible input, along
with efficient execution on all hardware platforms. This type of
robust software must manage both valid and invalid inputs.
2. Adaptability: Building software applications like Web Browsers,
Word Processors, and Internet Search Engine include huge software
systems that require correct and efficient working or execution for
many years. Moreover, software evolves due to emerging
technologies or ever-changing market conditions.
3. Reusability: The features like Reusability and Adaptability go hand
in hand. It is known that the programmer needs many resources to
build any software, making it a costly enterprise. However, if the
software is developed in a reusable and adaptable way, then it can be
applied in most future applications. Thus, by executing quality data
structures, it is possible to build reusable software, which appears to
be cost-effective and timesaving.

Classification of Data Structures


A Data Structure delivers a structured set of variables related to each
other in various ways. It forms the basis of a programming tool that
signifies the relationship between the data elements and allows
programmers to process the data efficiently.

We can classify Data Structures into two categories:

1. Primitive Data Structure


2. Non-Primitive Data Structure

The following figure shows the different classifications of Data


Structures.

Primitive Data Structures

1. Primitive Data Structures are the data structures consisting


of the numbers and the characters that come in-built into
programs.
2. These data structures can be manipulated or operated directly
by machine-level instructions.
3. Basic data types like Integer, Float, Character,
and Boolean come under the Primitive Data Structures.
4. These data types are also called Simple data types, as they
contain characters that can't be divided further

Non-Primitive Data Structures

1. Non-Primitive Data Structures are those data structures


derived from Primitive Data Structures.
2. These data structures can't be manipulated or operated
directly by machine-level instructions.
3. The focus of these data structures is on forming a set of data
elements that is either homogeneous (same data type)
or heterogeneous (different data types).
4. Based on the structure and arrangement of data, we can divide
these data structures into two sub-categories -
a. Linear Data Structures
b. Non-Linear Data Structures

Linear Data Structures


A data structure that preserves a linear connection among its data
elements is known as a Linear Data Structure. The arrangement of
the data is done linearly, where each element consists of the
successors and predecessors except the first and the last data
element. However, it is not necessarily true in the case of memory,
as the arrangement may not be sequential.

Based on memory allocation, the Linear Data Structures are


further classified into two types:

1. Static Data Structures: The data structures having a fixed size are
known as Static Data Structures. The memory for these data
structures is allocated at the compiler time, and their size cannot be
changed by the user after being compiled; however, the data stored
in them can be altered. The Array is the best example of the Static
Data Structure as they have a fixed size, and its data can be modified
later.
2. Dynamic Data Structures: The data structures having a dynamic
size are known as Dynamic Data Structures. The memory of these
data structures is allocated at the run time, and their size varies
during the run time of the code. Moreover, the user can change the
size as well as the data elements stored in these data structures at the
run time of the code Linked Lists, Stacks, and Queues are common
examples of dynamic data structures

Types of Linear Data Structures

The following is the list of Linear Data Structures that we generally use:

1. Arrays

An Array is a data structure used to collect multiple data elements of


the same data type into one variable. Instead of storing multiple
values of the same data types in separate variable names, we could
store all of them together into one variable. This statement doesn't
imply that we will have to unite all the values of the same data type in
any program into one array of that data type. But there will often be
times when some specific variables of the same data types are all
related to one another in a way appropriate for an array.

An Array is a list of elements where each element has a unique place


in the list. The data elements of the array share the same variable
name; however, each carries a different index number called a
subscript. We can access any data element from the list with the help
of its location in the list. Thus, the key feature of the arrays to
understand is that the data is stored in contiguous memory locations,
making it possible for the users to traverse through the data
elements of the array using their respective indexes.
Arrays can be classified into different types:

a. One-Dimensional Array: An Array with only one row of data


elements is known as a One-Dimensional Array. It is stored in ascending
storage location.
b. Two-Dimensional Array: An Array consisting of multiple rows and
columns of data elements is called a Two-Dimensional Array. It is
also known as a Matrix.
c. Multidimensional Array: We can define Multidimensional Array as
an Array of Arrays. Multidimensional Arrays are not bounded to two
indices or two dimensions as they can include as many indices are
per the need.

Some Applications of Array:

a. We can store a list of data elements belonging to the same data type.
b. Array acts as an auxiliary storage for other data structures.
c. The array also helps store data elements of a binary tree of the fixed
count.
d. Array also acts as a storage of matrices.

2. Linked Lists

A Linked List is another example of a linear data structure used to store a


collection of data elements dynamically. Data elements in this data
structure are represented by the Nodes, connected using links or pointers.
Each node contains two fields, the information field consists of the actual
data, and the pointer field consists of the address of the subsequent nodes
in the list. The pointer of the last node of the linked list consists of a null
pointer, as it points to nothing. Unlike the Arrays, the user can dynamically
adjust the size of a Linked List as per the requirements.

Linked Lists can be classified into different types:

a. Singly Linked List: A Singly Linked List is the most common type of
Linked List. Each node has data and a pointer field containing an address to
the next node.
b. Doubly Linked List: A Doubly Linked List consists of an information
field and two pointer fields. The information field contains the data.
The first pointer field contains an address of the previous node,
whereas another pointer field contains a reference to the next node.
Thus, we can go in both directions (backward as well as forward).
c. Circular Linked List: The Circular Linked List is similar to the Singly
Linked List. The only key difference is that the last node contains the
address of the first node, forming a circular loop in the Circular
Linked List.

Some Applications of Linked Lists:

a. The Linked Lists help us implement stacks, queues, binary trees, and
graphs of predefined size.
b. We can also implement Operating System's function for dynamic
memory management.
c. Linked Lists also allow polynomial implementation for mathematical
operations.
d. We can use Circular Linked List to implement Operating Systems or
application functions that Round Robin execution of tasks.
e. Circular Linked List is also helpful in a Slide Show where a user
requires to go back to the first slide after the last slide is presented.
f. Doubly Linked List is utilized to implement forward and backward
buttons in a browser to move forward and backward in the opened
pages of a website.

3. Stacks

A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out)
principle that allows operations like insertion and deletion from one end of
the Stack, i.e., Top. Stacks can be implemented with the help of contiguous
memory, an Array, and non-contiguous memory, a Linked List. Real-life
examples of Stacks are piles of books, a deck of cards, piles of money, and
many more.

A Real-life Example of Stack

The above figure represents the real-life example of a Stack where the
operations are performed from one end only, like the insertion and
removal of new books from the top of the Stack. It implies that the insertion
and deletion in the Stack can be done only from the top of the Stack. We can
access only the Stack's tops at any given time.

The primary operations in the Stack are as follows:

a. Push: Operation to insert a new element in the Stack is termed as


Push Operation.
b. Pop: Operation to remove or delete elements from the Stack is
termed as Pop Operation.

Some Applications of Stacks:

a. The Stack is used as a Temporary Storage Structure for recursive


operations.
b. Stack is also utilized as Auxiliary Storage Structure for function calls,
nested operations, and deferred/postponed functions.
c. We can manage function calls using Stacks.
d. Stacks are also utilized to evaluate the arithmetic expressions in
different programming languages.
e. Stacks are also helpful in converting infix expressions to postfix
expressions.
f. Stacks allow us to check the expression's syntax in the programming
environment.
g. We can match parenthesis using Stacks.
h. Stacks can be used to reverse a String.
i. Stacks are helpful in solving problems based on backtracking.
j. We can use Stacks in depth-first search in graph and tree traversal.
k. Stacks are also used in Operating System functions.
l. Stacks are also used in UNDO and REDO functions in an edit.

4. Queues

A Queue is a linear data structure similar to a Stack with some limitations


on the insertion and deletion of the elements. The insertion of an element
in a Queue is done at one end, and the removal is done at another or
opposite end. Thus, we can conclude that the Queue data structure follows
FIFO (First In, First Out) principle to manipulate the data elements.
Implementation of Queues can be done using Arrays, Linked Lists, or
Stacks. Some real-life examples of Queues are a line at the ticket counter, an
escalator, a car wash, and many more.

The above image is a real-life illustration of a movie ticket counter that can
help us understand the Queue where the customer who comes first is
always served first. The customer arriving last will undoubtedly be served
last. Both ends of the Queue are open and can execute different operations.
Another example is a food court line where the customer is inserted from
the rear end while the customer is removed at the front end after providing
the service they asked for.

The following are the primary operations of the Queue:

a. Enqueue: The insertion or Addition of some data elements to the


Queue is called Enqueue. The element insertion is always done with the
help of the rear pointer.
b. Dequeue: Deleting or removing data elements from the Queue is
termed Dequeue. The deletion of the element is always done with the
help of the front pointer.

Some Applications of Queues:

a. Queues are generally used in the breadth search operation in Graphs.


b. Queues are also used in Job Scheduler Operations of Operating
Systems, like a keyboard buffer queue to store the keys pressed by
users and a print buffer queue to store the documents printed by the
printer.
c. Queues are responsible for CPU scheduling, Job scheduling, and Disk
Scheduling.
d. Priority Queues are utilized in file-downloading operations in a
browser.
e. Queues are also used to transfer data between peripheral devices and
the CPU.
f. Queues are also responsible for handling interrupts generated by the
User Applications for the CPU.

Non-Linear Data Structures


Non-Linear Data Structures are data structures where the data elements
are not arranged in sequential order. Here, the insertion and removal of
data are not feasible in a linear manner. There exists a hierarchical
relationship between the individual data items.

Types of Non-Linear Data Structures


The following is the list of Non-Linear Data Structures that we generally
use:

1. Trees

A Tree is a Non-Linear Data Structure and a hierarchy containing a


collection of nodes such that each node of the tree stores a value and a list
of references to other nodes (the "children").

The Tree data structure is a specialized method to arrange and collect data
in the computer to be utilized more effectively. It contains a central node,
structural nodes, and sub-nodes connected via edges. We can also say that
the tree data structure consists of roots, branches, and leaves connected.

Trees can be classified into different types:

a. Binary Tree: A Tree data structure where each parent node can have
at most two children is termed a Binary Tree.
b. Binary Search Tree: A Binary Search Tree is a Tree data structure
where we can easily maintain a sorted list of numbers.
c. AVL Tree: An AVL Tree is a self-balancing Binary Search Tree where
each node maintains extra information known as a Balance Factor
whose value is either -1, 0, or +1.
d. B-Tree: A B-Tree is a special type of self-balancing Binary Search
Tree where each node consists of multiple keys and can have more
than two children.

Some Applications of Trees:

a. Trees implement hierarchical structures in computer systems like


directories and file systems.
b. Trees are also used to implement the navigation structure of a
website.
c. We can generate code like Huffman's code using Trees.
d. Trees are also helpful in decision-making in Gaming applications.
e. Trees are responsible for implementing priority queues for priority-
based OS scheduling functions.
f. Trees are also responsible for parsing expressions and statements in
the compilers of different programming languages.
g. We can use Trees to store data keys for indexing for Database
Management System (DBMS).
h. Spanning Trees allows us to route decisions in Computer and
Communications Networks.
i. Trees are also used in the path-finding algorithm implemented in
Artificial Intelligence (AI), Robotics, and Video Games Applications.

2. Graphs

A Graph is another example of a Non-Linear Data Structure comprising a


finite number of nodes or vertices and the edges connecting them. The
Graphs are utilized to address problems of the real world in which it
denotes the problem area as a network such as social networks, circuit
networks, and telephone networks. For instance, the nodes or vertices of a
Graph can represent a single user in a telephone network, while the edges
represent the link between them via telephone.

The Graph data structure, G is considered a mathematical structure


comprised of a set of vertices, V and a set of edges, E as shown below:

G = (V,E)

The above figure represents a Graph having seven vertices A, B, C, D, E, F, G,


and ten edges [A, B], [A, C], [B, C], [B, D], [B, E], [C, D], [D, E], [D, F], [E, F],
and [E, G].

Depending upon the position of the vertices and edges, the Graphs can
be classified into different types:

a. Null Graph: A Graph with an empty set of edges is termed a Null


Graph.
b. Trivial Graph: A Graph having only one vertex is termed a Trivial
Graph.
c. Simple Graph: A Graph with neither self-loops nor multiple edges is
known as a Simple Graph.
d. Multi Graph: A Graph is said to be Multi if it consists of multiple
edges but no self-loops.
e. Pseudo Graph: A Graph with self-loops and multiple edges is termed
a Pseudo Graph.
f. Non-Directed Graph: A Graph consisting of non-directed edges is
known as a Non-Directed Graph.
g. Directed Graph: A Graph consisting of the directed edges between
the vertices is known as a Directed Graph.
h. Connected Graph: A Graph with at least a single path between every
pair of vertices is termed a Connected Graph.
i. Disconnected Graph: A Graph where there does not exist any path
between at least one pair of vertices is termed a Disconnected Graph.
j. Regular Graph: A Graph where all vertices have the same degree is
termed a Regular Graph.
k. Complete Graph: A Graph in which all vertices have an edge
between every pair of vertices is known as a Complete Graph.
l. Cycle Graph: A Graph is said to be a Cycle if it has at least three
vertices and edges that form a cycle.
m. Cyclic Graph: A Graph is said to be Cyclic if and only if at least one
cycle exists.
n. Acyclic Graph: A Graph having zero cycles is termed an Acyclic
Graph.
o. Finite Graph: A Graph with a finite number of vertices and edges is
known as a Finite Graph.
p. Infinite Graph: A Graph with an infinite number of vertices and
edges is known as an Infinite Graph.
q. Bipartite Graph: A Graph where the vertices can be divided into
independent sets A and B, and all the vertices of set A should only be
connected to the vertices present in set B with some edges is termed
a Bipartite Graph.
r. Planar Graph: A Graph is said to be a Planar if we can draw it in a
single plane with two edges intersecting each other.
s. Euler Graph: A Graph is said to be Euler if and only if all the vertices
are even degrees.
t. Hamiltonian Graph: A Connected Graph consisting of a Hamiltonian
circuit is known as a Hamiltonian Graph.

Some Applications of Graphs:

a. Graphs help us represent routes and networks in transportation,


travel, and communication applications.
b. Graphs are used to display routes in GPS.
c. Graphs also help us represent the interconnections in social
networks and other network-based applications.
d. Graphs are utilized in mapping applications.
e. Graphs are responsible for the representation of user preference in e-
commerce applications.
f. Graphs are also used in Utility networks in order to identify the
problems posed to local or municipal corporations.
g. Graphs also help to manage the utilization and availability of
resources in an organization.
h. Graphs are also used to make document link maps of the websites in
order to display the connectivity between the pages through
hyperlinks.
i. Graphs are also used in robotic motions and neural networks.
Basic Operations of Data Structures
In the following section, we will discuss the different types of operations
that we can perform to manipulate data in every data structure:

1. Traversal: Traversing a data structure means accessing each data


element exactly once so it can be administered. For example,
traversing is required while printing the names of all the employees
in a department.
2. Search: Search is another data structure operation which means to
find the location of one or more data elements that meet certain
constraints. Such a data element may or may not be present in the
given set of data elements. For example, we can use the search
operation to find the names of all the employees who have the
experience of more than 5 years.
3. Insertion: Insertion means inserting or adding new data elements to
the collection. For example, we can use the insertion operation to add
the details of a new employee the company has recently hired.
4. Deletion: Deletion means to remove or delete a specific data element
from the given list of data elements. For example, we can use the
deleting operation to delete the name of an employee who has left
the job.
5. Sorting: Sorting means to arrange the data elements in either
Ascending or Descending order depending on the type of application.
For example, we can use the sorting operation to arrange the names
of employees in a department in alphabetical order or estimate the
top three performers of the month by arranging the performance of
the employees in descending order and extracting the details of the
top three.
6. Merge: Merge means to combine data elements of two sorted lists in
order to form a single list of sorted data elements.
7. Create: Create is an operation used to reserve memory for the data
elements of the program. We can perform this operation using a
declaration statement. The creation of data structure can take place
either during the following:
a. Compile-time
b. Run-time
For example, the malloc() function is used in C Language to
create data structure.
8. Selection: Selection means selecting a particular data from the
available data. We can select any particular data by specifying
conditions inside the loop.
9. Update: The Update operation allows us to update or modify the
data in the data structure. We can also update any particular data by
specifying some conditions inside the loop, like the Selection
operation.
10. Splitting: The Splitting operation allows us to divide data into
various subparts decreasing the overall process completion time.
Abstract Data Types
Abstract Data type (ADT) is a type for objects whose behavior
is defined by a set of values and a set of operations. The
definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented.
It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations.
It is called “abstract” because it gives an implementation-
independent view.
The process of providing only the essentials and hiding the
details is known as abstraction.

The user of data type does not need to know how that data type is
implemented, for example, we have been using Primitive values like
int, float, char data types only with the knowledge that these data
type can operate and be performed on without any idea of how they
are implemented.
So a user only needs to know what a data type can do, but not how it
will be implemented. Think of ADT as a black box which hides the
inner structure and design of the data type. Now we’ll define three
ADTs namely List ADT, Stack ADT, Queue ADT.

1. List ADT

Vies of list

 The data is generally stored in key sequence in a list which has a


head structure consisting of count, pointers and address of
compare function needed to compare the data in the list.
 The data node contains the pointer to a data structure and a self-
referential pointer which points to the next node in the list.
 The List ADT Functions is given below:
 get() – Return an element from the list at any given position.
 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element from a
non-empty list.
 removeAt() – Remove the element at a specified location from a
non-empty list.
 replace() – Replace an element at any position by another
element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return
false.
 isFull() – Return true if the list is full, otherwise return false.

2. Stack ADT
View of stack

 In Stack ADT Implementation instead of data being stored in each


node, the pointer to data is stored.
 The program allocates memory for the data and address is passed
to the stack ADT.
 The head node and the data nodes are encapsulated in the ADT.
The calling function can only see the pointer to the stack.
 The stack head structure also contains a pointer
to top and count of number of entries currently in stack.
The Stack ADT Functions is given below:

 push() – Insert an element at one end of the stack called top.


 pop() – Remove and return the element at the top of the stack, if
it is not empty.
 peek() – Return the element at the top of the stack without
removing it, if the stack is not empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise return
false.
 isFull() – Return true if the stack is full, otherwise return false.

3. Queue ADT

View of Queue

 The queue abstract data type (ADT) follows the basic design of
the stack abstract data type.
 Each node contains a void pointer to the data and the link
pointer to the next element in the queue. The program’s
responsibility is to allocate memory for storing the data.
The Queue ADT Functions is given below:

 enqueue() – Insert an element at the end of the queue.


 dequeue() – Remove and return the first element of the queue, if
the queue is not empty.
 peek() – Return the element of the queue without removing it, if
the queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return
false.
 isFull() – Return true if the queue is full, otherwise return false.
Features of ADT:
Abstract data types (ADTs) are a way of encapsulating data and
operations on that data into a single unit. Some of the key features of
ADTs include:
 Abstraction: The user does not need to know the
implementation of the data structure only essentials are
provided.
 Better Conceptualization: ADT gives us a better
conceptualization of the real world.
 Robust: The program is robust and has the ability to catch errors.
 Encapsulation: ADTs hide the internal details of the data and
provide a public interface for users to interact with the data. This
allows for easier maintenance and modification of the data
structure.
 Data Abstraction: ADTs provide a level of abstraction from the
implementation details of the data. Users only need to know the
operations that can be performed on the data, not how those
operations are implemented.
 Data Structure Independence: ADTs can be implemented using
different data structures, such as arrays or linked lists, without
affecting the functionality of the ADT.
 Information Hiding: ADTs can protect the integrity of the data
by allowing access only to authorized users and operations. This
helps prevent errors and misuse of the data.
 Modularity: ADTs can be combined with other ADTs to form
larger, more complex data structures. This allows for greater
flexibility and modularity in programming.

Advantages:

 Encapsulation: ADTs provide a way to encapsulate data and


operations into a single unit, making it easier to manage and
modify the data structure.
 Abstraction: ADTs allow users to work with data structures
without having to know the implementation details, which can
simplify programming and reduce errors.
 Data Structure Independence: ADTs can be implemented using
different data structures, which can make it easier to adapt to
changing needs and requirements.
 Information Hiding: ADTs can protect the integrity of data by
controlling access and preventing unauthorized modifications.
 Modularity: ADTs can be combined with other ADTs to form
more complex data structures, which can increase flexibility and
modularity in programming.

Disadvantages:

 Overhead: Implementing ADTs can add overhead in terms of


memory and processing, which can affect performance.
 Complexity: ADTs can be complex to implement, especially for
large and complex data structures.
 Learning Curve: Using ADTs requires knowledge of their
implementation and usage, which can take time and effort to
learn.
 Limited Flexibility: Some ADTs may be limited in their
functionality or may not be suitable for all types of data
structures.
 Cost: Implementing ADTs may require additional resources and
investment, which can increase the cost of development.

You might also like