Data Structure Classification

COS2611 - Programming: Data Structures · Introduction to Data Structures

Data Structure Classification

Data structures are essential for organizing and managing data in computer science. They can be classified based on various criteria. Understanding these classifications helps in selecting the appropriate data structure for a specific application.

Classification by Structure

Data structures can be classified into two main categories: linear and non-linear data structures.

Linear Data Structures

In linear data structures, data elements are arranged in a sequential manner. Each element is connected to its previous and next element. Common examples include arrays, linked lists, stacks, and queues.

Arrays

An array is a collection of elements identified by index or key. All elements in an array are of the same data type. For example, consider an array of integers:

int numbers[5] = {10, 20, 30, 40, 50};

Here, 'numbers' is an array with five integer elements. You can access the first element using numbers[0], which returns 10.

Remember: The index of an array starts at 0, not 1.

Linked Lists

A linked list consists of nodes where each node contains data and a pointer to the next node. This structure allows for efficient insertion and deletion of elements. Below is an example of a simple linked list:

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

In this example, each node contains an integer data and a pointer to the next node.

Watch out: Ensure that the last node's next pointer is set to NULL to indicate the end of the list.

Non-linear Data Structures

In non-linear data structures, data elements are not arranged sequentially. These structures allow for more complex relationships between data elements. Common examples include trees and graphs.

Trees

A tree is a hierarchical structure consisting of nodes. Each tree has a root node and sub-nodes. A binary tree is a specific type of tree where each node has at most two children. Here is an example of a binary tree:

struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
};

In this structure, each node has a data value and pointers to its left and right children.

Graphs

A graph is a collection of nodes (vertices) and edges connecting pairs of nodes. Graphs can be directed or undirected. An example of a graph representation using an adjacency list is shown below:

struct Graph {
int numVertices;
list *adjLists;
};

In this example, numVertices represents the number of vertices, and adjLists is an array of lists where each list contains the adjacent vertices for each vertex.

Classification by Memory Allocation

Data structures can also be classified based on how memory is allocated: static and dynamic data structures.

Static Data Structures

Static data structures have a fixed size. The size of the data structure is determined at compile time and cannot be changed during program execution. Arrays are a common example of static data structures.

Dynamic Data Structures

Dynamic data structures can grow and shrink in size during program execution. They use dynamic memory allocation to manage memory. Linked lists and trees are examples of dynamic data structures. Memory for these structures is allocated at runtime using functions like malloc in C++.

Tip: Use dynamic data structures when the size of the data is not known in advance.

Classification by Data Type

Data structures can also be classified based on the type of data they store: primitive and non-primitive data structures.

Primitive Data Structures

Primitive data structures are the basic data types provided by programming languages. Examples include integers, floats, characters, and booleans.

Non-primitive Data Structures

Non-primitive data structures are more complex and are derived from primitive data structures. They can be classified into two categories: linear and non-linear. Examples include arrays, linked lists, stacks, queues, trees, and graphs.

Summary of Data Structure Classifications

  • Data structures can be classified into linear and non-linear.
  • Linear data structures include arrays, linked lists, stacks, and queues.
  • Non-linear data structures include trees and graphs.
  • Data structures can be static or dynamic based on memory allocation.
  • Data structures can be primitive or non-primitive based on data type.

Check your understanding

  1. What are the main types of linear data structures?
  2. Explain the difference between static and dynamic data structures.
  3. What is a binary tree, and how does it differ from a general tree?
  4. Provide an example of a graph representation using an adjacency list.