Web hosting directory - 1162 Data Structures and Collections Chapter 23 6

1162 Data Structures and Collections Chapter 23 6 7 namespace StackInheritanceLibrary 8 { 9 // class StackInheritance inherits class List’s capabilities 10 public class StackInheritance : List 11 { 12 // pass name “stack” to List constructor 13 public StackInheritance() : base( “stack” ) 14 { 15 } 16 17 // place dataValue at top of stack by inserting 18 // dataValue at front of linked list 19 public void Push( object dataValue ) 20 { 21 InsertAtFront( dataValue ); 22 } 23 24 // remove item from top of stack by removing 25 // item at front of linked list 26 public object Pop() 27 { 28 return RemoveFromFront(); 29 } 30 31 } // end class StackInheritance 32 } Fig. 23.10 Fig. 23.10Fig. 23FiFi.10g. 23.10g. 23.10StackInheritanceextends class List. (Part 2 of 2.) Another way to implement a stack class is by reusing a list class through composition. The class in Fig. 23.12 uses a privateobject of class List(line 12) in the definition of class StackComposition. Composition enables us to hide the methods of class List that should not be in our stack s public interface by providing public interface methods only to the required Listmethods. This class implements each stack method by delegating its work to an appropriate Listmethod. In particular, StackComposition calls List methods InsertAtFront, RemoveFromFront, IsEmpty and Print. In this example, we do not show class StackCompositionTest, because the only difference in this example is that we change the type of the stack from StackInheritance to StackComposition. If you execute the application from the code on the CD that accompanies this book, you will see that the output is identical. 1 // Fig. 23.11: StackInheritanceTest.cs 2 // Testing class StackInheritance. 3 4 using System; 5 using StackInheritanceLibrary; 6 using LinkedListLibrary; 7 Fig. 23.11 Fig. 23.11Fig. 23FiFi.11g. 23.11g. 23.11Using class StackInheritance. (Part 1 of 3.)
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

Leave a Reply