Cpt S 122 Data Structures Templatized Stack Nirmalya

  • Slides: 22
Download presentation
Cpt S 122 – Data Structures Templatized Stack Nirmalya Roy School of Electrical Engineering

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

Test # 2 n Jia XU (Lab Section 3) n Xiaoyu Zhang (Lab Section

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

Test # 2 Statistics n n n n Overall Highest 100 Overall average of

Test # 2 Statistics n n n n 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)

Topics n Templated Stack ¡ ¡ push pop is. Stack. Empty print. Stack

Topics n Templated Stack ¡ ¡ push pop is. Stack. Empty print. Stack

Stacks n Stack data structure ¡ ¡ nodes to be added only at the

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

Stacks (cont. ) n Implement a stack as a constrained version of a linked

Stacks (cont. ) n Implement a stack as a constrained version of a linked list. ¡ n n n 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. ) n Option 1: Implement a stack class primarily by reusing a

Stacks (cont. ) n Option 1: Implement a stack class primarily by reusing a list class. ¡ n Option 2: Implement an identically performing stack class through composition ¡ n private inheritance of the list class. 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. ) n n Create a Stack class template primarily through private inheritance

Stacks (cont. ) n n Create a Stack class template primarily through private inheritance of the List class template. We want the Stack to have member functions ¡ ¡ push, pop, is. Stack. Empty, print. Stack These are essentially the n n insert. At. Front, remove. From. Front, is. Empty and print functions of the List class template.

Stacks (cont. ) n n n List class template contains other member functions ¡

Stacks (cont. ) n n n List class template contains other member functions ¡ insert. At. Back and remove. From. Back ¡ Don’t make those accessible through the public interface to the 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 insert. At. Front, ¡ pop calls remove. From. Front, ¡ is. Stack. Empty calls is. Emptyand ¡ print. Stack calls print ¡ this is referred to as delegation.

Inheritance Accessibility n A base class is privately inherited ¡ ¡ ¡ n public

Inheritance Accessibility n A base class is privately inherited ¡ ¡ ¡ n 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 n The private members of a base class will never become the members of its derived class.

Option 1: Templated Stack derived from class List

Option 1: Templated Stack derived from class List

Templated Stack (cont. )

Templated Stack (cont. )

Templated Stack (cont. ) n A dependent name is an identifier that depends on

Templated Stack (cont. ) n A dependent name is an identifier that depends on a template parameter. ¡ For example, the call to remove. From. Front 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. ) n n n The identifier for a function that takes

Templated Stack (cont. ) n n n The identifier for a function that takes no arguments like is. Empty 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, ¡ ¡ n 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. ) n n The stack class template is used in main

Templated Stack (cont. ) n n The stack class template is used in main to instantiate integer stack int. Stack of type Stack< int >. Integers 0 through 2 are pushed onto int. Stack then popped off int. Stack. The program uses the Stack class template to create double. Stack of type Stack< double >. Values 1. 1, 2. 2 and 3. 3 are pushed onto double. Stack, then popped off double. Stack.

Templated Stack

Templated Stack

Option 2: Templated Stack with List class composition n Stack class template ¡ n

Option 2: Templated Stack with List class composition n Stack class template ¡ n Stack class template contains a ¡ n List< STACKTYPE > object called stack. List. To test this class, use the driver program, but include the new header ¡ n reuse the List class template through composition. 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

Option 2: Templated Stack with List class composition

Option 2: Templated Stack with List class composition

Option 2: Templated Stack with List class composition