Chapter 23 Data Structures and Collections 1161 We (Free web servers)
Chapter 23 Data Structures and Collections 1161 We take advantage of the close relationship between lists and stacks to implement a stack class by reusing a list class. We demonstrate two different forms of reusability. First, we implement the stack class by inheriting from class Listof Fig. 23.4. Then, we implement an identically performing stack class through composition by including a List object as a private member of a stack class. This chapter implements list, stack and queue data structures to store object references to encourage further reusability. Thus, any object type can be stored in a list, stack or queue. The program of Fig. 23.10 and Fig. 23.11 creates a stack class by inheriting from class Listof Fig. 23.4. We want the stack to have methods Push, Pop, IsEmptyand Print. Essentially, these are the methods InsertAtFront, RemoveFromFront, IsEmpty and Print of class List. Of course, class List contains other methods (such as InsertAtBack and RemoveFromBack) that we would rather not make accessible through the publicinterface of the stack. It is important to remember that all methods in the public interface of class List are also public methods of the derived class StackInheritance(Fig. 23.10). When we implement the stack s methods, we have each StackInheritance method call the appropriate List method method Push calls InsertAtFront, method Pop calls RemoveFromFront. Class StackInheritance does not define methods IsEmpty and Print, because StackInheritance inherits these methods from class List into StackInheritance s publicinterface. The methods in class StackInheritancedo not use lockstatements. Each of the methods in this class calls a method from class List that uses lock. If two threads call Push on the same stack object, only one of the threads at a time will be able to call List method InsertAt- Front. Note that class StackInheritanceuses namespace LinkedListLibrary (Fig. 23.4); thus, the solution for the class library that defines StackInheritancemust have a reference to the LinkedListLibraryclass library. StackInheritanceTest s Main method (Fig. 23.11) uses class Stack- Inheritanceto instantiate a stack of objects called stack. Lines 18 21 define four objects that will be pushed onto the stack and popped off the stack. The program pushes onto the stack (lines 24, 26, 28 and 30) a boolcontaining true, a charcontaining $, an intcontaining 34567and a stringcontaining hello. An infinite whileloop (lines 36 41) pops the elements from the stack. When there are no objects left to pop, method Popthrows an EmptyListExceptionand the program displays the exception s stack trace, which shows the program execution stack at the time the exception occurred. The program uses method Print (inherited from class List) to output the contents of the stack after each operation. Note that class StackInheritanceTest uses namespace LinkedListLibrary (Fig. 23.4) and namespace StackInheritanceLibrary (Fig. 23.10); thus, the solution for class StackInheritanceTest must have references to both class libraries. 1 // Fig. 23.10: StackInheritanceLibrary.cs 2 // Implementing a stack by inheriting from class List. 3 4 using System; 5 using LinkedListLibrary; Fig. 23.10 Fig. 23.10Fig. 23FiFi.10g. 23.10g. 23.10StackInheritanceextends class List. (Part 1 of 2.)
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services