Lecture-1
Lecture-1
b) Data Structure
• A data structure is a way of organizing and storing data efficiently so that it can be accessed
and manipulated effectively.
• Examples: Arrays, Linked Lists, Stacks, Queues, Trees, Graphs.
c) Data Type
• Defines the type of value a variable can store.
• Primitive Data Types: Integer, Float, Character, Boolean.
Primitive data structures are the fundamental data types which are supported by a programming
language. Basic data types such as integer, real, character and Boolean are known as Primitive data
Structures. These data types consists of characters that cannot be divided and hence they also called
simple data types.
• Non-Primitive Data Types: Arrays, Structures, Classes, Pointers.
Non-primitive data structures are those data structures which are created using primitive data
structures. Examples of non-primitive data structures is the processing of complex numbers, linked
lists, stacks, trees, and graphs.
1. Arrays
• A collection of elements stored in contiguous memory locations.
• Used when elements need to be accessed randomly using an index.
Types of Arrays:
o One-Dimensional Array (1D): int arr[5] = {10, 20, 30, 40, 50};
o Two-Dimensional Array (2D): int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
o Multi-Dimensional Array
• Operations:
o Insertion
o Deletion
o Searching
o Sorting
o Traversal
2. Linked List
• A collection of nodes, where each node contains data and a reference to the next node.
• Unlike arrays, linked lists allow dynamic memory allocation.
Advantages:
o Dynamic size allocation.
o Faster insertion and deletion.
Disadvantages:
o Requires extra memory for pointers.
o Slower access time compared to arrays.
Operations:
o push(x): Insert x at the top.
o pop(): Remove the top element.
o peek(): View the top element.
Applications:
o Undo feature in text editors.
o Function call management in recursion.
Operations:
o enqueue(x): Insert x at the rear.
o dequeue(): Remove the element from the front.
o front(): View the front element.
o rear(): View the rear element.
1. Trees
• A hierarchical data structure consisting of nodes.
• Each node has a parent and child (except the root node).
Types of Trees:
o Binary Tree: Each node has at most two children.
o Binary Search Tree (BST): Left child < Root < Right child.
o AVL Tree: A self-balancing BST.
o Heap: A complete binary tree used in priority-based applications.
Applications:
o File system hierarchy.
o Organization structures.
o Database indexing.
2. Graphs
• A non-linear data structure consisting of a set of nodes (vertices) and edges (connections).
Types of Graphs:
o Directed Graph (Digraph): Edges have direction.
o Undirected Graph: Edges have no direction.
o Weighted Graph: Edges have weights (used in shortest path algorithms).
Graph Representation:
o Adjacency Matrix: An adjacency matrix is a way to represent a graph in a square
matrix form. It is used to show which vertices (or nodes) of a graph are adjacent to
which other vertices.
o Adjacency List: An adjacency list is another way to represent a graph, and it is
particularly efficient for sparse graphs (graphs with relatively few edges compared
to the number of vertices). Instead of using a matrix, an adjacency list uses a
collection of lists or arrays to store the neighbours of each vertex.
Applications:
o Social networks (Facebook, LinkedIn).
o Google Maps (shortest path algorithms).
o Computer networks.