Every program works with data. How the data is arranged affects how easily and how quickly the program can use it.

Definition

A data structure is a particular way of storing and organising data in a computer so that it can be accessed and modified efficiently. It defines both the arrangement of the data and the operations allowed on it. For example, a list of students may be kept in an array, in which any student can be reached directly by position, or in a linked list, in which students are added and removed easily. The study of data structures asks which arrangement suits a given problem. Niklaus Wirth summed up the subject as: Algorithms + Data Structures = Programs.

Definition: Data structure. A way of organising and storing data in memory, together with the operations defined on it, so that the data can be used efficiently.

Classification of Data Structures

Primitive data structures are the basic types built into a language, such as int, float, char and pointers. Non-primitive data structures are built from primitive ones. They are divided into linear structures, in which elements form a sequence, each with a single next element, such as arrays, linked lists, stacks and queues, and non-linear structures, in which an element may be linked to several others, such as trees and graphs. Data structures are also static, with a fixed size set at compile time, like an array, or dynamic, growing and shrinking at run time, like a linked list. They may be homogeneous, holding one type, or heterogeneous, holding several types, like a structure in C.

Operations on Data Structures

The common operations are traversing, visiting every element once, searching, finding an element with a given value, inserting, adding a new element, deleting, removing an element, sorting, arranging elements in order, and merging, combining two sorted structures into one. The cost of each operation differs from one structure to another, which is the main reason for choosing one structure over another.

Abstract Data Types

An abstract data type (ADT) describes a data structure by its operations and their behaviour, without saying how it is implemented. A stack is an ADT with operations push and pop. It can be implemented with an array or a linked list, and the user need not know which.

Table 1.1: Classification of data structures

Class Feature Examples
Primitive Built into the language int, float, char
Linear Elements in sequence Array, linked list, stack, queue
Non-linear Hierarchical or networked Tree, graph
Static / dynamic Fixed / changing size Array / linked list

Example 1.1: Choosing a data structure

Problem: Name a suitable structure for (a) the marks of 60 students accessed by roll number, (b) the undo history of an editor, (c) the people waiting at a ticket counter, (d) the folders on a disk.

Solution:

  1. Access by number suits an array.
  2. Undo takes back the latest action first, which is a stack.
  3. People served in order of arrival form a queue.
  4. Folders inside folders form a tree.

Result: (a) array, (b) stack, (c) queue, (d) tree.

#include <stdio.h>
struct student { int roll; char name[20]; float marks; };
int main(void) {
    int a[5] = {10, 20, 30, 40, 50};
    struct student s = {1, "Asha", 88.5f};
    printf("Array element a[2] = %d\n", a[2]);
    printf("Record: %d %s %.1f\n", s.roll, s.name, s.marks);
    return 0;
}

Output

Array element a[2] = 30
Record: 1 Asha 88.5

Common mistakes

  • Thinking that one data structure is best for every problem.
  • Confusing an abstract data type with its implementation.
  • Calling a stack a non-linear structure. It is linear.

Key takeaways

  • A data structure organises data and defines operations on it.
  • Linear structures form a sequence, non-linear ones form trees and graphs.
  • The operations are traverse, search, insert, delete, sort and merge.
  • An ADT gives behaviour without implementation.

Practice questions

  1. Define a data structure.
  2. Give two linear and two non-linear data structures.
  3. What is an abstract data type?

Answers to the practice questions

  1. A way of organising and storing data in memory, with operations on it, so that it can be used efficiently.
  2. Linear: array, stack. Non-linear: tree, graph.
  3. A description of a data type by its operations and behaviour, without its implementation.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit I