1148 Data Structures and Collections Chapter 23 15 10 Fig. 23.2 Fig. 23.Fig. 23.FF2ig. 23.2ig.2Two self-referential class objects linked together. 23. Creating and maintaining dynamic data structures requires dynamic memory allocation a program s ability to obtain more memory space at execution time to hold new nodes and to release space no longer needed. As we have already learned, C# programs do not explicitly release dynamically allocated memory. Rather, C# performs automatic garbage collection. The limit for dynamic memory allocation can be as large as the amount of available disk space in a virtual-memory system. Often, the limits are much smaller, because the computer s available memory must be shared among many users. Operator new 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 a newly created object of that type. For example, the statement Node nodeToAdd = new Node( 10 ); allocates the appropriate amount of memory to store a Nodeand stores a reference to this object in nodeToAdd. If no memory is available, new throws an OutOfMemoryException. The 10 is the Nodeobject 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 23.1 When creating a very large number of objects, test for an OutOfMemoryException. Perform appropriate error processing if the requested memory is not allocated. 23.3 Linked Lists A linked list is a linear collection (i.e., a sequence) of self-referential class objects, called nodes, connected by reference links hence, the term linked list. A program accesses a linked list via a reference to the first node of the list. Each subsequent node is accessed via the link-reference member stored in the previous node. By convention, the link reference in the last node of a list is set to nullto mark the end of the list. Data are stored in a linked list dynamically that is, each node is created as necessary. A node can contain data of any type, including objects of other classes. Stacks and queues are also linear data structures, and they are constrained versions of linked lists. Trees are nonlinear data structures. Lists of data can be stored in arrays, but linked lists provide several advantages. A linked list is appropriate when the number of data elements to be represented in the data structure is unpredictable. Unlike a linked list, the size of a conventional C# array cannot be altered, because the array size is fixed at creation time. Conventional arrays can become full, but linked lists become full only when the system has insufficient memory to satisfy dynamic storage allocation requests.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services