Data Structures
Data Structures
A data structure is an organization of data items in a particular manner, together with a set of valid operations applied to this organization.
A data structure is a specialized, systematic format for organizing, storing, and managing data in computer memory so that operations can be executed efficiently.
In literature:
Niklaus Wirth formalizes data structures:: Algorithms + Data Structures = Programs
Thomas H. Cormen et al. (Introduction to Algorithms): A data structure serves as the concrete physical implementation of an Abstract Data Type (ADT)
The following three core components describe it:
Data Values: The set of primitive or complex values stored.
Relationships: The logical arrangement (linear, hierarchical, or network) connecting data elements.
Operations: The collection of algorithms permitted to manipulate and retrieve stored data.
Operations on Data Structures
Access / Search-Locates a specific element or value within memory based on a key or index.
Insertion-Adds a new element into a designated location within the structure.
Deletion-Removes an existing element and reorganizes remaining memory references.
Traversal-Accesses and processes every element in the structure sequentially or recursively exactly once.
Sorting-Rearranges stored items into a specific order (e.g., numerical ascending or alphabetical).
Merging-Combines elements from two separate structures into a single structure.
Update-Replaces an existing element's value at a target reference or index.
Types of Data Structures
Primitive vs. Non-Primitive
Primitive Data Structures: Basic building blocks provided directly by programming languages (e.g., Integer, Float, Character, Boolean, Pointer).
Non-Primitive Data Structures: Complex structures built from primitive types to aggregate multiple values (e.g., Arrays, Linked Lists, Trees).
Linear vs. Non-Linear
Linear Data Structures: Elements are linked sequentially in a single sequence, where each element connects directly to its predecessor and successor.
Array: A fixed-size, contiguous block of memory storing elements accessible via index.
Linked List: A sequence of nodes stored in non-contiguous memory connected via pointers (Singly, Doubly, or Circular).
Stack: A Last-In, First-Out (LIFO) structure where insertions (push) and deletions (pop) occur at one end.
Queue: A First-In, First-Out (FIFO) structure where items enter at the rear (enqueue) and exit at the front (dequeue).
Non-Linear Data Structures: Elements are arranged hierarchically or as a complex network rather than sequentially.
Tree: A hierarchical node-based structure consisting of a root and child subtrees (e.g., Binary Search Tree, AVL Tree, B-Tree).
Graph: A non-linear network of vertices (nodes) linked by edges, representing arbitrary relationships.
Hash Table: An associative dictionary mapping key-value pairs using a hash function for $O(1)$ average lookup time.
Heap: A specialized tree maintaining min or max heap properties for priority queue operations.
Static vs. Dynamic
Static: Fixed memory size determined at compile time (e.g., standard Arrays).
Dynamic: Variable memory capacity that grows or shrinks dynamically at runtime via heap allocation (e.g., Dynamic Arrays, Linked Lists).
A data type defines the kind of value that a variable can store, the amount of memory required to store it, the range of possible values, and the operations that can be performed on it
Primitive data types are the basic built-in types a programming language provides.
char
Character / small integer
1 byte
−128 to 127 or 0 to 255
'A'
Characters, symbols, text
_______________________________________________
int
Integer
4 bytes
−2,147,483,648 to 2,147,483,647
100
Counting, indexing, IDs
_______________________________________________
short
Small integer
2 bytes
−32,768 to 32,767
250
Memory-efficient integers
_______________________________________________
long
Large integer
4/8 bytes
Implementation-dependent
100000L
Large integer values
_______________________________________________
float
Decimal/real number
4 bytes
Approx. ±3.4 × 10³⁸
3.14f
Scientific calculations
_______________________________________________
double
High-precision decimal
8 bytes
Approx. ±1.7 × 10³⁰⁸
3.14159
High-precision calculations
_______________________________________________
Bool
Logical value
Implementation-dependent
0 or 1
1
True/false conditions
______________________________________________
void
No value
—
No value
void Functions with no return value
*Sizes and exact ranges can vary by C implementation. The ranges above are common for modern systems.
_______________________________________________
C provides modifiers that change the size or range of integer types:
signed
unsigned
short
long
Examples:
unsigned int age;
short int marks;
long int population;
unsigned char code;
For example, a typical unsigned int has a range: 0 to 4,294,967,295
Non-primitive data types are built from primitive types and are generally used to represent collections or complex data structures.
Array -Collection of elements of the same data type
int marks[10];
Storing multiple values
_______________________________________________
Structure-Collection of different data types under one name
struct Student
Records/entities
_______________________________________________
Union-Different members share the same memory location
union Data
Memory-efficient representation
_______________________________________________
Pointer-Stores the address of another variable
int *p;
Dynamic memory, linked structures
_______________________________________________
String-Sequence of characters terminated by '\0'
char name[20];
Text processing
_______________________________________________
C also supports:
struct
union
enum
typedef
Example:
struct Student {
int rollNo;
char name[50];
float marks;
};
Here, Student represents a complex data entity containing different kinds of values.
_______________________________________________
In semantic analysis, data types help determine whether values and operations are meaningful and valid.
For example:
int age = 20;
float marks = 85.5;
char grade = 'A';
The semantic analyzer determines:
age → integer
marks → floating-point value
grade → character
Whether assignments are type-compatible
Whether operators are applicable
Whether function arguments match parameter types
Whether expressions produce valid results
Example:
int x;
float y;
x = y; /*The semantic analysis phase checks the type compatibility between x and y.*/
Thus, data types provide the semantic meaning and constraints of values, making them fundamental to type checking, expression evaluation, symbol-table construction, and semantic analysis