Home » Data Structure » Unit 1: Introduction to Data Structure » Data Structure, its need and its types

Data Structure, its need and its types

Last updated:

Support this post with a reaction:

3,483 views

A data structure is a way of organizing, storing, and managing data in a computer so that it can be accessed, modified, and processed efficiently. It provides a systematic method for arranging data based on the requirements of a program or application. Data structures play an important role in computer science because they help manage large amounts of data, improve program performance, reduce processing time, and optimize memory usage. Different types of data structures, such as arrays, linked lists, stacks, queues, trees, and graphs, are used to solve different computational problems.

What Is a Data Structure?

A Data Structure is a method of organizing, storing, and managing data in a computer so that it can be accessed, modified, and processed efficiently.

It defines how data elements are arranged in memory and how different operations, such as insertion, deletion, searching, sorting, and updating, are performed.

Data structures are fundamental concepts in computer science because they help programmers manage large amounts of information and develop efficient algorithms.

Example:

Consider a college management system that stores information about thousands of students, including their names, roll numbers, marks, and departments.

If all student records are stored without proper organization, searching for a particular student’s information can become difficult and time-consuming.

By using appropriate data structures such as arrays, linked lists, trees, or hash tables, the system can organize student information and perform operations more efficiently.

Therefore, Data Structures like Arrays, Lists, or Files are useful for both small and large amounts of data, but they are most important when handling large data sets.

Characteristics of a Data Structure

A data structure has several important characteristics that determine how data is organized and processed.

  1. Organization: Defines how data elements are arranged and related to each other.
  2. Access: Determines how individual elements can be retrieved.
  3. Modification: Supports operations such as insertion, deletion, and updating.
  4. Memory Management: Determines how memory is allocated and utilized.
  5. Efficiency: Influences the time and space required to perform operations.
  6. Relationships: Defines how different data elements are connected.

Types of Data Structures

Data structures are classified into different categories based on how data is stored, organized, and accessed.

The two major categories are:

  1. Primitive Data Structures
  2. Non-Primitive Data Structures

1. Primitive Data Structures

Primitive Data Structures are the basic data types provided by a programming language. They store simple values and are used as building blocks for creating more complex data structures.

These data types generally store a single value at a time.

Examples of Primitive Data Structures:

Data TypeDescriptionExample
IntegerStores whole numbersint age = 20;
FloatStores decimal numbersfloat price = 25.5;
CharacterStores a single characterchar grade = 'A';
BooleanStores true or false valuesbool isValid = true;
PointerStores a memory addressint *ptr;

Example in C:

#include <stdio.h>

int main() {
    int age = 20;
    float percentage = 85.5;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Percentage: %.2f\n", percentage);
    printf("Grade: %c\n", grade);

    return 0;
}

Output:

Age: 20
Percentage: 85.50
Grade: A

In this example, integer, float, and character variables store individual values.

Note: Primitive data types differ between programming languages. For example, C uses int, float, and char, while Java also provides boolean as a primitive type. Pointers are supported in C but not as a general-purpose primitive data type in Java.

2. Non-Primitive Data Structures

Non-Primitive Data Structures are complex structures that organize multiple data elements and establish relationships between them.

They are generally constructed using primitive data types and other programming language features.

1. Arrays

An Array is a linear data structure that stores multiple elements of the same data type in contiguous memory locations.

Each element is identified using an index, which generally starts from 0.

Arrays allow direct access to elements using their index, making element retrieval efficient..

Example:

Array

Consider an array [2, 4, 10, 5, 15, 3]. Here, 2 is at index 0, 4 at index 1, and so on. Accessing index 2 returns 10.

Types of Arrays

  1. One-Dimensional Array: Stores elements in a single sequence.
  2. Two-Dimensional Array: Stores elements in rows and columns, similar to a matrix.
  3. Multidimensional Array: Stores data across three or more dimensions.

2. Stacks

A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle.

This means the element inserted last is the first element removed.

Insertion and deletion operations are performed at only one end, called the Top of the stack.

Example:

Consider a stack in which elements 10, 20, 30, and 40 are inserted one after another.

The element 40 is at the top because it was inserted last.

When a deletion operation is performed, 40 is removed first.

Basic Operations on Stack

  1. Push: Inserts a new element at the top of the stack.
  2. Pop: Removes the top element from the stack.
  3. Peek (Top): Returns the top element without removing it.
  4. isEmpty: Checks whether the stack contains any elements.
  5. isFull: Checks whether an array-based stack has reached its maximum capacity.

Example:

stacks

Initially the stack is empty. After pushing 1, 2, 3, 4, the top element becomes 4. When we perform a pop, 4 is removed first.

3. Queues

A Queue is a linear data structure that follows the FIFO (First In, First Out) principle.

This means the element inserted first is the first element removed.

Insertion takes place at the Rear, while deletion takes place at the Front.

Example:

Consider people standing in a ticket booking line. The person who arrives first gets the ticket first, while newly arriving people join at the end of the line.

A queue works in the same way.

Basic Operations on Queue

  1. Enqueue: Inserts an element at the rear.
  2. Dequeue: Removes an element from the front.
  3. Front (Peek): Returns the first element without removing it.
  4. Rear: Returns the last element.
  5. isEmpty: Checks whether the queue is empty.
  6. isFull: Checks whether an array-based queue is full.

Example:

Queue

The queue contains elements 2, 3, 4, 5, 6, 7, 8, 9. A new element 10 is added at the rear, and the element 2 is removed from the front.

Types of Queues

A. Simple Queue

A Simple Queue follows the FIFO principle. Insertion occurs at the rear, and deletion occurs at the front.

B. Circular Queue

A Circular Queue connects the last position of the queue to the first position, allowing unused spaces to be reused.

It helps overcome the space wastage problem found in a simple array-based queue.

C. Priority Queue

A Priority Queue assigns a priority to each element. Elements are processed according to their priority rather than strictly following their insertion order.

D. Deque (Double-Ended Queue)

A Deque allows insertion and deletion at both ends: front and rear.

4. Linked Lists

A Linked List is a linear data structure that stores elements in dynamically allocated memory locations.

Each element is called a Node, and every node contains data and one or more links (pointers) that connect it to other nodes.

Unlike arrays, linked list nodes do not need to occupy contiguous memory locations.

A basic singly linked list node contains two parts:

  1. Data: Stores the actual value.
  2. Next Pointer: Stores the address of the next node.

The first node is accessed using a pointer called Head, while the last node points to NULL.

Example:

Linked list

The first node stores 1 and its next pointer refers to the next node storing 2, and so on. The last node points to NULL. The first node is accessed through Head.

Here, each node stores a value and points to the next node.

Types of Linked Lists

A. Singly Linked List

A Singly Linked List is a linear data structure in which each node contains data and a pointer to the next node.

Traversal is possible only in the forward direction.

B. Doubly Linked List

A Doubly Linked List contains two pointers in each node:

  • Previous pointer.
  • Next pointer.

It allows traversal in both forward and backward directions.

C. Circular Linked List

In a Circular Linked List, the last node points back to the first node instead of pointing to NULL.

It creates a circular connection between nodes.

D. Circular Doubly Linked List

A Circular Doubly Linked List combines the properties of doubly linked lists and circular linked lists.

Each node contains previous and next pointers, and the first and last nodes are connected in both directions.

Basic Operations on Linked Lists

  1. Traversal: Visits each node to access its data.
  2. Insertion: Adds a new node at the beginning, end, or a specified position.
  3. Deletion: Removes a node from the list.
  4. Searching: Finds a node containing a particular value.
  5. Updating: Changes the data stored in a node.

5. Trees

A Tree is a non-linear, hierarchical data structure consisting of nodes connected through edges.

It represents parent-child relationships between elements and starts with a special node called the Root.

Unlike linear data structures, trees allow one node to connect to multiple child nodes.

Basic Terminology of Trees

Understanding tree terminology is important before studying different types of trees.

TermDescription
RootThe topmost node of a tree
ParentA node that has one or more children
ChildA node directly connected below another node
Leaf NodeA node that has no children
EdgeA connection between two nodes
SiblingNodes having the same parent
SubtreeA tree formed from a node and its descendants
DepthNumber of edges from the root to a node
HeightNumber of edges in the longest downward path from a node to a leaf

Trees are commonly used to represent structures like file systems, organization charts, and family trees.

Example:

Tree Data structure

The root node has 2 children, and each child has 2 leaf nodes. Nodes with no children are leaf nodes.

Types of Trees

A. General Tree

A General Tree is a tree in which a node can have any number of children.

B. Binary Tree

A Binary Tree is a tree in which each node can have at most two children, commonly called the left child and right child.

C. Binary Search Tree (BST)

A Binary Search Tree is a binary tree in which values in the left subtree are smaller than the node’s value, and values in the right subtree are larger, assuming distinct keys.

D. AVL Tree

An AVL Tree is a self-balancing Binary Search Tree in which the height difference between the left and right subtrees of every node is at most one.

E. Heap

A Heap is a specialized complete binary tree that follows the heap property.

  • Max Heap: Parent values are greater than or equal to their children’s values.
  • Min Heap: Parent values are less than or equal to their children’s values.

F. B-Tree

A B-Tree is a self-balancing search tree designed to store sorted data and support efficient searching, insertion, and deletion. It is commonly used in database and storage systems.

Tree Traversal Techniques

Tree traversal is the process of visiting every node of a tree in a particular order.

The common traversal techniques are:

  1. Preorder: Root → Left → Right.
  2. Inorder: Left → Root → Right.
  3. Postorder: Left → Right → Root.
  4. Level Order: Visits nodes level by level.

6. Graphs

A Graph is a non-linear data structure consisting of a collection of vertices (nodes) and edges (connections).

Vertices represent objects or entities, while edges represent relationships or connections between them.

Graphs are useful for representing complex relationships where one element can be connected to multiple other elements.

Basic Terminology of Graphs

TermDescription
VertexA node representing an entity
EdgeA connection between two vertices
DegreeNumber of edges incident on a vertex in an undirected graph
PathA sequence of vertices connected by edges
CycleA path that starts and ends at the same vertex
Connected GraphA graph in which every pair of vertices has a path between them
Weighted GraphA graph in which edges have associated weights or costs

Example:

Graph

Node 1 is connected to 2 and 3. Node 2 is connected to 3 and 5, and so on. These connections represent relationships.

Types of Graphs

A. Undirected Graph

An Undirected Graph contains edges that do not have a specific direction.

If vertex A is connected to vertex B, the connection can be traversed in both directions.

B. Directed Graph

A Directed Graph contains edges with a specific direction.

An edge from A to B does not necessarily mean that B is connected back to A.

C. Weighted Graph

A Weighted Graph assigns a numerical value or cost to each edge.

Weights may represent distance, time, cost, or other measurable quantities.

D. Unweighted Graph

An Unweighted Graph contains edges without associated numerical weights.

E. Cyclic Graph

A Cyclic Graph contains at least one cycle.

F. Acyclic Graph

An Acyclic Graph does not contain any cycles.

Graph Representation Techniques

Graphs are commonly represented using two methods:

1. Adjacency Matrix

An Adjacency Matrix represents a graph using a two-dimensional array.

For a graph with V vertices, the matrix has V rows and V columns.

The value at position [i][j] indicates whether an edge exists between vertices i and j. In a weighted graph, it may store the edge weight.

2. Adjacency List

An Adjacency List represents a graph by maintaining a list of neighboring vertices for each vertex.

It is generally memory-efficient for sparse graphs.

Graph Traversal Techniques

  • Breadth-First Search (BFS): Visits vertices level by level using a queue.
  • Depth-First Search (DFS): Explores a path as deeply as possible before backtracking, commonly using recursion or a stack.

ches forming the words “on”, “man”, and “in”. Each path forms a complete word.

7. Hash Tables

A Hash Table is a data structure that stores information in key-value pairs and uses a hash function to calculate the index where a value should be stored.

It provides efficient insertion, searching, and deletion operations when a good hash function and collision-handling strategy are used.

Example:

Consider a student database where each student has a unique roll number.

Key (Roll Number)Value (Student Name)
101Ravi
102Priya
103Arun
104Kiran

A hash function converts the roll number into an index for storing or retrieving the corresponding student record.

How Does a Hash Table Work?

  1. A key is provided as input.
  2. A hash function processes the key.
  3. The hash function calculates an array index.
  4. The corresponding value is stored or retrieved from that location.
  5. If multiple keys produce the same index, a collision-handling technique is used.

Example:

hashing

The hash function takes a key, computes a hash value, and stores the corresponding data at that index.

Collision Handling Techniques

A. Separate Chaining

Separate Chaining handles collisions by storing multiple elements at the same hash index using a linked list or another suitable collection.

B. Open Addressing

Open Addressing handles collisions by searching for another available position within the hash table.

Common methods include:

  • Linear Probing.
  • Quadratic Probing.
  • Double Hashing.

Why Do We Need Data Structures?

Data structures are important because they help organize information and perform operations efficiently. Without appropriate data structures, managing large amounts of information can become difficult and time-consuming.

The following points explain why data structures are required in computer science.

1. Efficient Data Organization

Data structures organize information in a systematic manner, making it easier to store, access, and manage data.

For example, an array can store student marks, while a tree can organize files and folders.

2. Faster Searching and Sorting

Appropriate data structures improve searching and sorting performance.

For example, a balanced Binary Search Tree can support efficient searching, while a hash table provides average-case constant-time lookup.

3. Efficient Memory Utilization

Different data structures manage memory in different ways.

For example, linked lists allocate memory dynamically, whereas fixed-size arrays reserve a predetermined amount of storage.

Choosing the appropriate structure helps avoid unnecessary memory usage.

4. Easy Insertion and Deletion

Data structures provide different methods for adding and removing elements.

For example, linked lists allow efficient insertion and deletion when the relevant node or position is known.

5. Handling Large Amounts of Data

Modern applications process large amounts of information. Data structures help organize and manage these datasets efficiently.

For example, databases use trees and hash-based indexing techniques to retrieve records.

6. Improving Algorithm Performance

The choice of data structure directly affects the efficiency of an algorithm.

For example, BFS uses a queue, DFS uses a stack or recursion, and Dijkstra’s algorithm commonly uses a priority queue.

7. Representing Complex Relationships

Some problems involve hierarchical or interconnected information that cannot be represented conveniently using simple linear structures.

For example:

  • Trees represent hierarchical relationships.
  • Graphs represent social networks and transportation systems.
  • Tries represent words and prefixes.

8. Supporting Software Development

Data structures are used in almost every type of software application, including operating systems, databases, compilers, web applications, and artificial intelligence systems.