Part II: Data Structures in Pascal

In which we learn that choosing how to organize data is the most consequential decision a programmer makes — and that Pascal gives us unusually elegant tools for making it.


In 1976, Niklaus Wirth published a book whose title became one of the most quoted sentences in computer science: Algorithms + Data Structures = Programs. The title is not a metaphor. It is a literal thesis. A program is not a sequence of instructions that happens to manipulate data; a program is the marriage of how you organize information and how you process it. Get the data structure right, and the algorithm often writes itself. Get it wrong, and no amount of clever logic will save you.

Part I gave you algorithms — modest ones, built from if statements, loops, and procedures. You wrote programs that processed one expense at a time, printed results, and forgot everything the moment they exited. PennyWise worked, in a limited sense: it could accept input, perform calculations, and display results. But it could not remember. It could not accumulate. It could not persist.

Part II fixes that. Over the next seven chapters, you will learn to organize data — in memory and on disk — using the structures that Pascal provides. By the time you finish Chapter 15, PennyWise will store hundreds of transactions in typed records, sort them by date or category, search them efficiently, save them to files that survive between sessions, and use pointers to build dynamic structures that grow and shrink as needed. The transformation is not incremental. It is architectural.

The Structures Ahead

Chapter 9 introduces arrays — fixed-size, indexed collections of homogeneous data. Arrays are the workhorse of Part II and of programming in general. You will learn to declare them, fill them, traverse them, and pass them to procedures. Rosa Martinelli converts PennyWise's expense tracking from single-variable entry to a proper array of transactions, and suddenly the program can show her last fifty purchases at a glance. Tomás uses arrays to track his weekly food spending across seven days and compute running averages. GradeBook Pro gets its first real data backbone here.

Chapter 10 tackles strings — Pascal's approach to text. You will learn about the classic ShortString, the modern AnsiString, and the operations that let you search, slice, concatenate, and compare text. PennyWise gains the ability to parse user input intelligently: when Rosa types "coffee 4.50 food," the program can split that into a description, an amount, and a category.

Chapter 11 introduces records — Pascal's compound data type, the ancestor of every struct, class, and object in modern programming. A record lets you bundle related fields into a single named type: a TExpense with a date, a description, an amount, and a category. This is where PennyWise stops being a collection of parallel arrays and becomes a program that models reality. The with statement, record variants, and nested records all appear here, and you will understand why records are the foundation upon which Part III's object-oriented programming is built.

Chapter 12 covers sets and enumerated types — two features that Pascal handles with a clarity that most languages have never matched. Enumerated types let you replace magic numbers with meaningful names; sets let you perform membership tests, unions, and intersections with a single operator. PennyWise's category system gets an elegant upgrade: instead of comparing strings, you define an enumerated TCategory type and use set operations to filter transactions.

Chapter 13 is about files — typed files, text files, and untyped files. This is the chapter where PennyWise becomes a real application, because it can finally save data between runs. You will learn to open, read, write, and close files using Pascal's explicit file-handling model. Tomás saves his budget to disk for the first time and discovers, with visible relief, that his three weeks of expense data will not vanish when he closes the terminal. The chapter also covers error handling for file operations, because the real world is full of missing files, full disks, and permission errors.

Chapter 14 introduces pointers and dynamic memory allocation. This is the chapter many students fear and the chapter that, more than any other, separates Pascal from languages that hide memory management behind garbage collectors. You will learn what a pointer is, how New and Dispose work, why nil matters, and how to think about memory as a resource you borrow and return. The chapter is honest about the difficulty: pointer bugs are real, dangling references are dangerous, and memory leaks are silent. But the understanding you gain here will make you a better programmer in every language — because even in Python and Java, memory still exists, and the programmers who understand what is happening beneath the abstraction are the ones who write software that actually performs.

Chapter 15 builds on Chapter 14 to construct dynamic data structures: linked lists, stacks, and queues. These are not academic exercises. A linked list lets PennyWise maintain a transaction history that grows without limit. A stack lets the Crypts of Pascalia game track the player's navigation history so they can retrace their steps. A queue lets GradeBook Pro process student submissions in arrival order. You will implement each structure from scratch, understand its trade-offs against arrays, and begin to develop the instinct for choosing the right structure for the right problem.

The PennyWise Transformation

The change in PennyWise across Part II is dramatic. At the start of Chapter 9, PennyWise is a stateless console tool — it processes one expense, displays a result, and forgets. By the end of Chapter 15, it is a persistent, data-rich application with:

  • An array-backed transaction table that can hold a full month of expenses
  • Typed records (TExpense, TCategory, TBudget) that model the financial domain
  • File I/O that saves and loads data between sessions
  • A dynamically linked list for unlimited transaction history
  • Set-based category filtering

Rosa can now open PennyWise on Monday morning and see her entire March spending history, sorted by category, with subtotals and a comparison to her budget. Tomás can export his food spending to a text file and email it to his parents as proof that he is not blowing his meal plan on takeout. These are not hypothetical features. You will build them.

Why Data Structures Matter More Than You Think

There is a temptation, especially among self-taught programmers, to treat data structures as a topic you study for interviews and then forget. This is a mistake. The choice of data structure is the most leveraged decision in any program. An array where you need a hash table means your search is a hundred times slower. A linked list where you need an array means your cache performance is catastrophic. A flat file where you need a database means your data will eventually corrupt.

Pascal is an exceptional language for learning data structures because it makes every allocation visible and every structure explicit. You do not call list.append() and trust the runtime to handle the details. You allocate a node, set its fields, adjust the pointers, and verify the invariants. This is more work. It is also more understanding. And understanding is the point.

Wirth knew this. His textbook was not called Algorithms + Libraries = Programs. The data structure is not an implementation detail. It is the program.

Let us build some.

Chapters in This Part