Read without ads and support Scribd by becoming a Scribd Premium Reader.
19
Data Structures
Objectives
\ue000 To be able to form linked data structures using

references, self-referential classes and recursion.
\u2022 To be able to create and manipulate dynamic data
structures, such as linked lists, queues, stacks and

binary trees.
\u2022 To understand various important applications of
linked data structures.
\u2022 To understand how to create reusable data structures
with classes, inheritance and composition.
Much that I bound, I could not free;
Much that I freed returned to me.
Lee Wilson Dodd
\u2018Will you walk a little faster?\u2019 said a whiting to a snail,
\u2018There\u2019s a porpoise close behind us, and he\u2019s treading on my
tail.\u2019
Lewis Carroll
There is always room at the top.
Daniel Webster
Push on\u2014keep moving.
Thomas Morton
I think that I shall never see
A poem lovely as a tree.
Joyce Kilmer
jhtp4_19.fm Page 1094 Thursday, July 12, 2001 12:41 PM
Chapter 19
Data Structures
1095
19.1 Introduction
We have studied such fixed-size data structures as single- and double-subscripted arrays.
This chapter introduces dynamic data structures that grow and shrink at execution time.
Linked lists are collections of data items\u201clined up in a row\u201d\u2014insertions and deletions can

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.

We encourage you to attempt the major project described in the special section entitled
Building Your Own Compiler. You have been using a Java compiler to translate your Java

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-

Outline
19.1 Introduction
19.2 Self-Referential Classes
19.3 Dynamic Memory Allocation

19.4 Linked Lists
19.5 Stacks
19.6 Queues
19.7 Trees

Summary\u2022 Terminology\u2022 Self-Review Exercises\u2022 Answers to Self-Review Exercises\u2022 Exercises\u2022
Special Section: Building Your Own Compiler
jhtp4_19.fm Page 1095 Thursday, July 12, 2001 12:41 PM
1096
Data Structures
Chapter 19

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.

19.2 Self-Referential Classes
A self-referential class contains an instance variable that refers to another object of the
same class type. For example, the definition

class Node {
private intd a t a;
private Node nextNode;

publicNo d e ( int data )
{/* constructor body */ }
public voidsetData( int data ) { /* method body */ }
public intge t D at a ( )
{/* method body */ }
public void setNext( Node next ) { /* method body */ }
public Node getNext()
{/* method body */ }
}
defines classN od e. This type has twop ri v at e instance variables\u2014integerda t a and
Nodere f er enc e nextNode. Member nextNode references an object of type Node, an

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

getNext method to return the value of member nextNode.

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.

Common Programming Error 19.1
Not setting the link in the last node of a list ton ul l is a logic error.
19.1
19.3 Dynamic Memory Allocation

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.

jhtp4_19.fm Page 1096 Thursday, July 12, 2001 12:41 PM
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more