references, self-referential classes and recursion.
\u2022 To be able to create and manipulate dynamic data
structures, such as linked lists, queues, stacks and
be made anywhere in a linked list.S tack s are important in compilers and operating systems; insertions and deletions are made only at one end of a stack\u2014its top. Queues represent waiting lines; insertions are made at the back (also referred to as thetail) of a queue and deletions are made from the front (also referred to as thehe ad) of a queue. Binary trees fa- cilitate high-speed searching and sorting of data, eliminating of duplicate data items effi- ciently, representing file system directories, compiling expressions into machine language and many other interesting applications.
We will discuss each of the major types of data structures and implement programs that create and manipulate them. We use classes, inheritance and composition to create and package these data structures for reusability and maintainability. In Chapter 20,\u201cJava Util- ities Package and Bit Manipulation,\u201d and Chapter 21,\u201cCollections,\u201d we discuss Java\u2019s pre- defined classes that implement the data structures discussed in this chapter.
The chapter examples are practical programs that can be used in more advanced courses and in industrial applications. The exercises include a rich collection of useful applications.
programs to bytecodes so that you could execute these programs on your computer. In this project, you will 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 the popular lan- guage Basic. Your compiler will translate these statements into a file of Simpletron Machine Language (SML) instructions\u2014SML is the language you learned in the Chapter 7 special section, Building Your Own Computer. Your Simpletron Simulator program will then execute the SML program produced by your compiler! Implementing this project by using an object-oriented approach will give you a wonderful opportunity to exercise most of what you have learned in this book. The special section carefully walks you through the specifications of the high-level language and describes the algorithms you will need to con-
19.4 Linked Lists
19.5 Stacks
19.6 Queues
19.7 Trees
vert each type of high-level language statement into machine language instructions. If you enjoy being challenged, you might attempt the many enhancements to both the compiler and the Simpletron Simulator suggested in the exercises.
class Node {
private intd a t a;
private Node nextNode;
object of the same type as the one being declared here\u2014hence, the term\u201cself-referential class.\u201d Membern ex t No d e is al in k\u2014nextNode \u201clinks\u201d an object of typeNo d e to an- other object of the same type. TypeNo d e also has five methods: a constructor that receives an integer to initialized a ta, as et D at a method to set the valued a ta, ag et D at a meth- od to return the value ofd a ta, as et Ne x t method to set the value ofne x tN o de and a
Programs can link self-referential objects together to form such useful data structures as lists, queues, stacks and trees. Figure 19.1 illustrates two self-referential objects linked together to form a list. A backslash\u2014representing anul l reference\u2014is placed in the link member of the second self-referential object to indicate that the link does not refer to another object. The backslash is for illustration purposes; it does not correspond to the backslash character in Java. Normally, anul l reference indicates the end of a data structure.
Creating and maintaining dynamic data structures requires dynamic memory allocation\u2014 the ability for a program to obtain more memory space at execution time to hold new nodes and to release space no longer needed. As we have already learned, Java programs do not explicitly release dynamically allocated memory. Rather, Java performs automatic garbage collection on objects that are no longer referenced in a program.
The limit for dynamic memory allocation can be as large as the amount of available physical memory in the computer or the amount of available disk space in a virtual-memory system. Often, the limits are much smaller, because the computer\u2019s available memory must be shared among many applications.