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

Slides:



Advertisements
Similar presentations
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Advertisements

Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
 2006 Pearson Education, Inc. All rights reserved Generics.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Helpful C++ Transitions
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Templates Zhen Jiang West Chester University
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Course Review FINAL.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Queues.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
 2003 Prentice Hall, Inc. All rights reserved Stacks Upcoming program –Create stack from list insertAtFront, removeFromFront –Software reusability.
Chapter 22 - C++ Templates Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Queue.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
Chapter 20 Custom Templatized Data Structures
Stacks II David Lillis School of Computer Science and Informatics
Data Structures Using C++ 2E
Cpt S 122 – Data Structures Abstract Data Types
Chapter 22 - C++ Templates
5. Stacks and Queues.
Copy Constructor / Destructors Stacks and Queues
Chapter 18 Introduction to Custom Templates
Chapter 22 Custom Generic Data Structures
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
8-1.
8-1.
Helpful C++ Transitions
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
20.5 Stacks Upcoming program Create stack from list
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Abstract Data Types Stacks CSCI 240
Presentation transcript:

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

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

Test # 2 Statistics Overall Highest 100 Overall average of the class is Section 1 Average: (Highest 98) Section 2 Average: (Highest 96) Section 3 Average: (Highest 100) Section 4 Average: (Highest 94) Section 5 Average: (Highest 96) Section 6 Average: (Highest 100)

Topics Templated Stack  push  pop  isStackEmpty  printStack

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

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

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

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

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

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.

Option1: Templated Stack derived from class List

Templated Stack (cont.)

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

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

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

Templated Stack

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