Self-Referential Classes

A self-referential class contains a reference member that refers to an object of the same class type. For example, the class declaration in Fig. 25.1 defines the shell of a self-referential class named Node. This type has two private instance variablesinteger data and Node reference next. Member next references an object of type Node, an object of the same type as the one being declared herehence, the term "self-referential class." Member next is referred to as a link (i.e., next can be used to "tie" an object of type Node to another object of the same type). Class Node also has two propertiesone for instance variable data (named Data) and another for instance variable next (named Next).

Figure 25.1. Self-referential Node class declaration.

(This item is displayed on page 1324 in the print version)

 1 // Fig. 25.1: Fig25_01.cs
 2 // A self-referential class.
 3 class Node
 4 {
 5 private int data; // store integer data
 6 private Node next; // store reference to next Node
 7
 8 public Node( int dataValue )
 9 {
10 // constructor body
11 } // end constructor
12
13 public int Data
14 {
15 get
16 {
17 // get body
18 } // end get
19 set
20 {
21 // set body
22 } // end set
23 } // end property Data
24
25 public Node Next
26 {
27 get
28 {
29 // get body
30 } // end get
31 set
32 {
33 // set body
34 } // end set
35 } // end property Next
36 } // end class Node

Self-referential objects can be linked together to form useful data structures, such as lists, queues, stacks and trees. Figure 25.2 illustrates two self-referential objects linked together to form a linked list. A backslash (representing a null reference) is 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 C#. A null reference normally indicates the end of a data structure.

Figure 25.2. Self-referential class objects linked together.

Common Programming Error 25 1

Not setting the link in the last node of a list to null is a logic error.

Creating and maintaining dynamic data structures requires dynamic memory allocationa program's ability to obtain more memory space at execution time to hold new nodes and to release space no longer needed. As you learned in Section 9.9, C# programs do not explicitly release dynamically allocated memoryrather, C# performs automatic garbage collection.

The new operator is essential to dynamic memory allocation. Operator new takes as an operand the type of the object being dynamically allocated and returns a reference to an object of that type. For example, the statement

Node nodeToAdd = new Node( 10 );

allocates the appropriate amount of memory to store a Node and stores a reference to this object in nodeToAdd. If no memory is available, new throws an OutOfMemoryException. The constructor argument 10 specifies the Node object's data.

The following sections discuss lists, stacks, queues and trees. These data structures are created and maintained with dynamic memory allocation and self-referential classes.

Good Programming Practice 25 1

When creating a large number of objects, test for an OutOfMemoryException. Perform appropriate error processing if the requested memory is not allocated.


Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net