Lesson 1 +25 XP

Overview of Data Structures & Abstract Data Types (ADT)

Overview of Data Structures & Abstract Data Types (ADT)

A Data Structure is a systematic way of organizing, storing, and managing data in computer memory so that operations such as searching, insertion, deletion, sorting, and traversal can be performed efficiently in both time and space.

Classification of Data StructuresClassification of Data Structures

1. Classification of Data Structures

CategoryExamplesKey Characteristic
Primitive (Built-in)int, float, char, double, pointerDirectly supported by hardware / compiler; atomic values
Linear (Non-Primitive)Arrays, Strings, Linked Lists, Stacks, QueuesElements arranged sequentially; each element has at most one predecessor and one successor
Non-Linear (Non-Primitive)Trees (BT, BST, AVL, Heap, B-Tree), GraphsHierarchical or many-to-many network relationships

Static vs Dynamic Data Structures

  • Static: Memory size is fixed at compile time (e.g., standard arrays int a[100]). Fast O(1) indexing, but risks overflow or wasted space.
  • Dynamic: Memory grows and shrinks at runtime on the heap using malloc() / calloc() / free() (e.g., linked lists, trees).

Physical vs Logical Representation

  • Logical Representation: The conceptual model seen by the programmer or algorithm (e.g., a FIFO ticket counter queue, or a directory hierarchy).
  • Physical Representation: How bytes are actually laid out in RAM-either contiguous blocks (arrays) or non-contiguous heap nodes connected via pointers.

2. Core Operations on Data Structures

  1. Traversing: Visiting every element exactly once to process or print it.
  2. Searching: Locating the position/node of a given key value.
  3. Inserting: Adding a new element at the beginning, end, or a specified position.
  4. Deleting: Removing an existing element and reclaiming its memory.
  5. Sorting: Arranging elements in ascending or descending order.
  6. Merging: Combining two sorted structures into a single sorted structure.

3. Abstract Data Types (ADT)

An Abstract Data Type (ADT) is a mathematical/logical model of a data type defined purely by its behavior (operations) from the user's point of view, encapsulating the underlying memory representation:

  • Data Declaration: Logical properties and invariants of the stored items.
  • Operations: Public interface functions with well-defined preconditions and postconditions.
  • Information Hiding: The caller cannot directly manipulate internal pointers or array indices.

Generic ADT in C using void* and Function Pointers

In C (CIC-209), generic ADTs store arbitrary data types using void *dataPtr and pass behavior (like comparison or printing) via function pointers:

C
typedef struct node {
    void *dataPtr;
    struct node *link;
} NODE;

NODE *createNode(void *itemPtr) {
    NODE *nodePtr = (NODE *)malloc(sizeof(NODE));
    if (!nodePtr) return NULL;
    nodePtr->dataPtr = itemPtr;
    nodePtr->link = NULL;
    return nodePtr;
}
💡 Tip

Separating the ADT interface from its physical implementation lets you swap an array-backed list for a linked list without rewriting application logic.

Why Do We Need Data Structures? As applications grow more complex and data-rich, three problems show up:

ProblemWhat goes wrongHow a data structure helps
Data searchSearching 1 million (10⁶) unsorted items one by one makes every lookup slowSorted arrays, BSTs and hash tables cut a search to O(log n) or O(1)
Processor speedEven a fast CPU is overwhelmed when data grows to billions of recordsA good organization reduces the number of steps, which no CPU upgrade can do
Multiple requestsA web server receives thousands of simultaneous searchesIndexed structures (B-trees, hash tables) answer each request without scanning everything

Data Type vs Data Structure

  • Data type = a set of values + the operations allowed on them. int is the values {…, −1, 0, 1, …} with + − * / %.
  • Built-in (primitive) types: int, float, char, double, bool.
  • Derived (user-defined) types: arrays, struct, union, pointers, enum, built from primitive types.
  • Data structure = a derived organization of data plus the algorithms that keep it consistent (insert, delete, search, traverse).

DATA TYPE = Set of values + Set of operations - the same idea, raised one level of abstraction, gives the ADT.

Worked Example: The Ordered List ADT, Two Implementations

An ordered list stores items in sorted order with the operations insert(x), delete(x), find(x), printAll().

OperationArray implementationPointer (linked list) implementation
find(x)Binary search: O(log n)Walk node by node: O(n)
insert(x)Find position, shift all larger items right: O(n)Find position, relink 2 pointers: O(n) search + O(1) link
delete(x)Shift items left to fill the gap: O(n)Relink predecessor: O(1) after search
MemoryFixed capacity declared in advanceGrows node by node; extra pointer per node

The user of the ADT calls the same four functions either way-that is the whole point of separating the interface from the implementation.

Pointers to Functions

A pointer can hold the address of a function, not just of data. This lets one generic routine (a sort, a traversal) call a different comparison or visit function each time.

C
#include <stdio.h>
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int apply(int (*op)(int, int), int x, int y) {
    return op(x, y);           /* call through the pointer */
}

int main(void) {
    int (*fp)(int, int) = add; /* fp points to add */
    printf("%d\n", fp(3, 4));          /* 7  */
    printf("%d\n", apply(mul, 3, 4));  /* 12 */
    return 0;
}
  • Declaration syntax: returnType (*name)(parameterTypes); - the parentheses around *name are required, otherwise it declares a function returning a pointer.
  • Used in the C library's qsort() (comparison callback) and in generic ADTs that store void* data plus a compare function pointer.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Define Data Structure and Abstract Data Type (ADT). Differentiate between Linear and Non-Linear Data Structures. (5 Marks)
  • Write: Definition of DS (organizing/storing data for efficient access), classification diagram, ADT = Data + Operations + Encapsulation, and a 4-point comparison table (order, traversal, memory, examples: Array/Stack/Queue vs Tree/Graph).
  • Q2. Explain Physical vs Logical representation of Data Structures with examples. (5 Marks)
  • Write: Logical = user's conceptual view; Physical = contiguous RAM (arrays) vs pointer-linked heap nodes (linked lists), plus static vs dynamic memory allocation.