Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.

Similar presentations


Presentation on theme: "Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack."— Presentation transcript:

1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack

2 Test # 2 Jia XU (Lab Section 3) Xiaoyu Zhang (Lab Section 3) Anh Nguyen (Lab Section 6)

3 Test # 2 Statistics Overall Highest 100 Overall average of the class is 76.57 Section 1 Average: 75.23 (Highest 98) Section 2 Average: 79.22 (Highest 96) Section 3 Average: 78.67 (Highest 100) Section 4 Average: 70.48 (Highest 94) Section 5 Average: 78.91 (Highest 96) Section 6 Average: 76.94 (Highest 100)

4 Topics Templated Stack  push  pop  isStackEmpty  printStack

5 Stack data structure  nodes to be added only at the top and  removed from the stack only at the top. A stack is referred to as a last-in, first-out (LIFO) data structure. We have seen a stack class template in Templates Chapter  an array implementation. Now we use an underlying pointer-based linked-list implementation  Standard Template Library (STL). Stacks

6 Implement a stack as a constrained version of a linked list.  the link member in the last node of the stack is set to null (zero) to indicate the bottom of the stack. The primary member functions used to manipulate a stack are push and pop. Function push inserts a new node at the top of the stack. Function pop removes a node from the top of the stack,  stores the popped value in a reference variable that is passed to the calling function  returns true if the pop operation was successful ( false otherwise). Stacks (cont.)

7 Option 1: Implement a stack class primarily by reusing a list class.  private inheritance of the list class. Option 2: Implement an identically performing stack class through composition  a list object as a private member of a stack class. Both the stack classes, are implemented as templates to encourage further reusability. Stacks (cont.)

8 Create a Stack class template primarily through private inheritance of the List class template. We want the Stack to have member functions  push, pop, isStackEmpty, printStack  These are essentially the insertAtFront, removeFromFront, isEmpty and print functions of the List class template. Stacks (cont.)

9 List class template contains other member functions  insertAtBack and removeFromBack  Don’t make those accessible through the public interface to the Stack class. Stack class template inherit from the List class template through private inheritance.  This makes all the List class template’s member functions private in the Stack class template. Call the appropriate member function of the List class  push calls insertAtFront,  pop calls removeFromFront,  isStackEmpty calls isEmpty and  printStack calls print  this is referred to as delegation. Stacks (cont.)

10 Inheritance Accessibility A base class is privately inherited  public members of the base class become private members of the derived class.  public members of the base class can only be accessed by the member functions of the derived class.  They are inaccessible to the objects of the derived class. A base class is publicly inherited  public members of the base class become public members of the derived class.  They are accessible to the objects of the derived class.  In both the cases, the private members are not inherited The private members of a base class will never become the members of its derived class.

11 Option1: Templated Stack derived from class List

12 Templated Stack (cont.)

13 A dependent name is an identifier that depends on a template parameter.  For example, the call to removeFromFront depends on the argument data  It has a type that is dependent on the template parameter STACKTYPE.  Resolution of dependent names occurs when the template is instantiated. Templated Stack (cont.)

14 The identifier for a function that takes no arguments like isEmpty or print in the List superclass is a non-dependent name. Such identifiers are normally resolved at the point where the template is defined. If the template has not yet been instantiated,  the code for the function with the non-dependent name does not yet exist  some compilers will generate compilation errors. Adding the explicit use of this-> makes the calls to the base class’s member functions dependent on the template parameter  ensures that the code will compile properly. Templated Stack (cont.)

15 The stack class template is used in main to instantiate integer stack intStack of type Stack. Integers 0 through 2 are pushed onto intStack then popped off intStack. The program uses the Stack class template to create doubleStack of type Stack. Values 1.1, 2.2 and 3.3 are pushed onto doubleStack, then popped off doubleStack. Templated Stack (cont.)

16 Templated Stack

17

18

19

20 Stack class template  reuse the List class template through composition. Stack class template contains a  List object called stackList. To test this class, use the driver program, but include the new header  Stackcomposition.h of that file. The output of the program is identical for both versions of class Stack. Option 2: Templated Stack with List class composition

21

22


Download ppt "Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack."

Similar presentations


Ads by Google