19.1 Introduction

We’ve studied fixed-size data structures—such as one- and two-dimensional template-based arrays (Chapter 7) and built-in arrays (Chapter 8)—and various C++ Standard Library dynamic data structures (arrays and vectors in Chapter 7 and other template-based containers in Chapter 15) that can grow and shrink during execution.

In this chapter, we demonstrate how you can create your own custom templatized dynamic data structures. We discuss several popular and important data structures and implement programs that create and manipulate them:

  • Linked lists are collections of data items logically “lined up in a row”—insertions and removals are made anywhere in a linked list.

  • Stacks (which we introduced in Section 6.11 and discussed again in Section 15.7.1) are important in compilers and operating systems: Insertions and removals are made only at one end of a stack—its top.

  • Queues represent waiting lines; insertions are made at the back (also referred to as the tail) of a queue and removals are made from the front (also referred to as the head) of a queue.

  • Binary trees facilitate searching and sorting data, duplicate elimination and compiling expressions into machine code.

Each of these data structures has many other interesting applications. We use class templates, inheritance and composition to create and package these data structures for reusability and maintainability. The programs employ extensive pointer manipulation. The exercises include a rich collection of useful applications.

19.1.1 Always Prefer the Standard Library’s Containers, Iterators and Algorithms, if Possible

The C++ Standard Library’s containers, iterators for traversing those containers and algorithms for processing the containers’ elements meet the needs of most C++ programmers. The Standard Library code is carefully written to be correct, portable, efficient and extensible. Understanding how to build custom templatized data structures will also help you use the Standard Library containers, iterators and algorithms, more effectively.

19.1.2 Special Section: Building Your Own Compiler

We encourage you to attempt the optional project described in the Special Section: Building Your Own Compiler (http://www.deitel.com/books/cpphtp10). You’ve been using a C++ compiler to translate your programs to machine code so that you can execute these programs on your computer. In this project, you’ll actually build your own compiler. It will read a file of statements written in a simple, yet powerful, high-level language similar to early versions of BASIC. Your compiler will translate these statements into a file of Simpletron Machine Language (SML) instructions—SML is the artificial language you learned in the Chapter 8 Special Section: Building Your Own Computer. Your Simpletron Simulator program will then execute the SML program produced by your compiler! The special section discusses the high-level language and the algorithms you’ll need to convert each type of high-level language statement into machine code. We provide compiler-theory exercises and suggest enhancements to both the compiler and the Simpletron Simulator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset