topic04_Bags

topic04_Bags - The Bag ADT CS 1037a - Topic 4 Related...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
The Bag ADT CS 1037a -- Topic 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Related materials • Sec. 3.1: The bag class from Main and Savitch from CS1037 labs • Lab 4 4-2
Background image of page 2
Bags Bag : A collection of items in no particular order • Items are retrieved from a bag in random order 4-3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Possible operations on a Bag add Add an item to the bag getOne Retrieve an item from the bag isEmpty Determine whether the bag is empty isFull Determine whether the bag is full currentSize Determine the number of items in the bag capacity Determine the bag’s maximum capacity 4-4
Background image of page 4
UML Diagram: Bag of Integers BagOfIntegers -m_size: integer -m_array: array of integers +add( integer value) +getOne( ): integer +isEmpty( ): boolean {query} +isFull( ): boolean {query} +currentSize( ): unsigned integer {query} +capacity( ): unsigned integer {query} 4-5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bag Implementations in C++ • We will start by implementing a bag of int values with a fixed upper limit on the number of items that can be stored • Later, we will examine ways to implement more general bag classes, where the capacity of the bag can vary, and where the elements can be something other than int s 4-6
Background image of page 6
BagOfIntegers.h #define CAPACITY 50 class BagOfIntegers { public: BagOfIntegers( ); // constructor for an empty bag void add(int value); // adds a new integer into the bag int getOne( ); // extracts an integer from the bag bool isEmpty( ) const; bool isFull( ) const; unsigned int currentSize( ) const; // returns # of items now in bag unsigned int capacity( ) const; // returns bag’s upper capacity // continued on next slide 4-7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BagOfIntegers.h private: unsigned int m_size; // number of elements currently in the bag int m_container[CAPACITY]; // internal storage array of elements // - statically allocated array }; // end of BagOfIntegers.h 4-8
Background image of page 8
BagOfIntegers.cpp #include “BagOfIntegers.h” #include <stdlib.h> // for rand(), see slide 4-11 // constructor builds a bag with the ability to hold // CAPACITY ints; the bag is initially empty BagOfIntegers :: BagOfIntegers( ) { m_size = 0; } // continued on next slide 4-9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// Add the int value to the bag. // PRECONDITION: bag should not be full void BagOfIntegers :: add(int value) { m_container[m_size] = value; m_size++; } // continued on next slide NOTE: programmers using such Bags will be responsible for not applying this function to full bags! 4-10 PROGRAMMING HINT: It is possible to add a call to function assert(!isFull()); as a first line of code for add(. ..) above. Standard function assert(expression) stops the program and outputs an error message if expression is false. Function assert does nothing if expression is true. Function assert(. ..) is used for debugging (see p.788 of the textbook for more details). assert(!isFull());
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 46

topic04_Bags - The Bag ADT CS 1037a - Topic 4 Related...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online