+ All Categories
Home > Documents > Cpt S 122 Data Structures Templatized...

Cpt S 122 Data Structures Templatized...

Date post: 02-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
22
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 Data Structures Templatized Stack
Transcript
Page 1: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Nirmalya Roy

School of Electrical Engineering and Computer ScienceWashington State University

Cpt S 122 – Data Structures

Templatized Stack

Page 2: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Test # 2

Jia XU (Lab Section 3)

Xiaoyu Zhang (Lab Section 3)

Anh Nguyen (Lab Section 6)

Page 3: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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)

Page 4: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Topics

Templated Stack push

pop

isStackEmpty

printStack

Page 5: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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

Page 6: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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.)

Page 7: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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.)

Page 8: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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.)

Page 9: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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 isEmptyand

printStack calls print this is referred to as delegation.

Stacks (cont.)

Page 10: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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.

Page 11: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Option1: Templated Stack derived from class List

Page 12: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Templated Stack (cont.)

Page 13: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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.)

Page 14: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

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.)

Page 15: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

The stack class template is used in main to instantiate

integer stack intStack of type Stack< int > .

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< double >.

Values 1.1, 2.2 and 3.3 are pushed onto

doubleStack, then popped off doubleStack.

Templated Stack (cont.)

Page 16: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Templated Stack

Page 17: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack
Page 18: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack
Page 19: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack
Page 20: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Stack class template

reuse the List class template through composition.

Stack class template contains a

List< STACKTYPE > 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

Page 21: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Option2: Templated Stack with List class composition

Page 22: Cpt S 122 Data Structures Templatized Stacknroy/courses/cpts122/notes/Lecture15_TemplatedStack.pdfStack data structure nodes to be added only at the top and removed from the stack

Option2: Templated Stack with List class composition


Recommended