Skip to content

Data Structure

  • Fundamental building blocks used to organize and manipulate data inside programs and algorithms.
  • Different types trade off simplicity and indexed access (arrays) versus dynamic sizing and pointer-based traversal (linked lists).
  • Choice affects efficiency of access, insertion, and deletion operations.

Data structures are the building blocks of computer programs and algorithms that allow for the organization and manipulation of data in a way that is efficient and effective.

Data structures present different approaches to storing and accessing collections of data. Arrays store elements in contiguous memory locations and provide direct access to elements via an index. Linked lists store elements as nodes linked by pointers; each node contains a value and a pointer to the next node. Arrays are simple to implement and enable efficient indexed access, but their size is fixed once created. Linked lists are dynamic and can grow or shrink without copying elements, but they are more complex to implement and do not provide efficient indexed access.

An array of integers stored contiguously:

int numbers[5] = { 1, 2, 3, 4, 5 };

Access by index:

  • numbers[0] returns the first element: 1
  • numbers[4] returns the last element: 5

A simple node-based linked list definition:

struct Node {
int data;
struct Node* next;
};

Each node has a data value and a pointer to the next node. To traverse the list, follow the pointers from one node to the next until reaching the end.

  • Arrays: size is fixed after creation; adding or removing elements requires creating a new array and copying elements, which can be time-consuming and wasteful.
  • Linked lists: dynamic sizing allows easy expansion and contraction without copying, but they are more complex to implement and do not allow efficient indexed access.
  • Array
  • Linked list
  • Node
  • Pointer