Computer Science · Ch 3 — Stack
Introduction
Introduction
Understanding Data Structures
In Class XI, you learned about Python's built-in data types like strings, lists, tuples, and sets. These are not just random containers — they are examples of data structures. A data structure defines a mechanism to store, organise and access data, along with the operations that can be efficiently performed on it.
For instance, a string is a data structure that stores a sequence of characters. You can reverse it, slice it, count characters, and more. A list is another sequence data structure, but unlike a string, its elements can be of different types — integers, floats, strings, or even other lists. Both strings and lists allow operations like reversal, slicing, and counting of elements.
A data structure organises multiple elements in a way so that certain operations — on each individual element as well as on the collective data unit — can be performed easily.
Linear and Other Data Structures
A data structure in which elements are organised in a sequence is called a linear data structure. Strings, lists, and tuples are all linear.
Other important data structures in Computer Science include Array, Linked List, Binary Trees, Heaps, Graph, Sparse Matrix, etc.
Notice that this wider family is not limited to linear arrangements — Binary Trees, Heaps and Graphs, for example, organise their elements in non-linear relationships. Stack and Queue, the structures this unit builds towards, are linear.
Stack and Queue: Two Essential Structures
Stack and Queue are two other popular data structures used in programming. Although not directly available in Python, it is important to learn these concepts as they are extensively used in a number of programming languages. In this chapter, we will study the stack data structure in detail — how it works, how to implement it using Python, and where it is applied in real-world programming.
Even though Python does not directly provide a stack class, you can implement one using lists or other Python features. Understanding the concept is what matters — the implementation follows naturally.