+1 (315) 557-6473 

Program To Use Bag ADT And Dictionaries in C++ Language Assignment Solution.


Instructions

Objective
Write a C++ assignment program to use bag ADT and dictionaries.

Requirements and Specifications

Overview
Implement a dictionary using a Bag—Project 4.7 in the text (modified)
Instructions
Use the bag ADT provided to create an array-based implementation for bags. Then use your bag to implement the dictionary ADT provided you. This means you have to implement your dictionary using the bag functions.
Test your bag and dictionary implementations with the bagtestmain.cpp file provided for you. If you are not able to fully implement all the functions of the dictionary and/or bag, you may modify the tests in bagtestmain.cpp to only exercise those functions you were able to complete.
You will only get credit for those methods you test and display, so be sure you don’t leave any out.
Also, do not add any public functions to your bag and dictionary implementations beyond those specified in the ADTs, though you may add private functions if you want.
Source Code and Solution
BAG ADT
/*
* File: bagADT.h -- Bag ADT for CSCI 215 Assignment
* Author: Prof Terri Sipantzi
*
* Created on July 14, 2012, 11:46 AM
* Modified on 9/4/2014 -- Changed "const E& inspectTop() const" to "bool inspectTop(const E& item) const".
* inspectTop now returns false if the bag is empty and true otherwise.
* Modified on 2/4/2015 -- updated the comments for remove(), find(), removeTop(),
* and inspectTop() to make them clearer.
*/
#ifndef BAGADT_H
#define BAGADT_H
#include
#include "book.h"
template
class Bag {
public:
 Bag() {} // base constructor
 virtual ~Bag() {} // base destructor
 // Insert a new item into the bag -- return false if fails and true if
 // successful
 virtual bool addItem(const E& item) = 0;
 // Looks for 'item' in the bag and if found updates 'item' with the
 // bag value and returns true. Otherwise 'item' is left unchanged
 // and the method returns false.
 virtual bool remove(E& item) = 0;
 // Removes the top record from the bag, puts it in returnValue, and
 // returns true if the bag is not empty. If the bag is empty the
 // function returns false and returnValue remains unchanged.
 virtual bool removeTop(E& returnValue) = 0;
 // Finds the record using returnValue and if the record is found updates
 // returnValue based on the contents of the bag and returns true. If the
 // record is not found the function returns false. Works just like remove()
 // except that the found record is not removed from the bag.
 virtual bool find(E& returnValue) const = 0;
 // Inspect the top of the bag. If the bag is empty return
 // false and leave 'item' unchanged; otherwise, return true and update
 // 'item' with the contents of the bag.
 virtual bool inspectTop(E& item) const = 0;
 // empties the bag
 virtual void emptyBag() = 0;
 // use the += operator to add an item to the bag
 virtual bool operator+=(const E& addend) = 0;
 // get the size of the bag
 virtual int size() const = 0;
 // get the capacity of the bag
 virtual int bagCapacity() const = 0;
};
#endif /* BAGADT_H */
DICTIONARY
/*
* File: BDictionary.h -- implement a dictionary use an array-based bag
* Author: Prof Terri Sipantzi
*
* You may use this template to implement your Dictionary
*/
#ifndef BDICTIONARY_H
#define BDICTIONARY_H
#include "ABag.h"
#include "dictionaryADT.h"
#include "kvpair.h"
template
class BDictionary : public Dictionary {
public:
 // constructors/destructor
 BDictionary(int size) {
  dictionary = new ABag>(size);
 }
 ~BDictionary() {
  delete dictionary;
 }
 // methods: clear, insert, remove, removeAny, find, size, etc.
 void clear() {
  dictionary->emptyBag();
 }
 bool insert(const Key& k, const E& e) {
  KVpair pair(k, e);
  return (*dictionary) += pair;
 }
 bool remove(const Key& k, E& rtnVal) {
  KVpair pair(k, rtnVal);
  bool result = dictionary->remove(pair);
  rtnVal = pair.value();
  return result;
 }
 bool removeAny(E& returnValue) {
  Key k;
  KVpair pair(k, returnValue);
  bool result = dictionary->removeTop(pair);
  returnValue = pair.value();
  return result;
 }
 bool find(const Key& k, E& returnValue) const {
  KVpair pair(k, returnValue);
  bool result = dictionary->find(pair);
  returnValue = pair.value();
  return result;
 }
 int size() {
  return dictionary->size();
 }
private:
 //Pointer to a ABag object. You'll need to instantiate the bag in your constructor:
 // dictionary = new ABag>(size) or something similar depending on how
 // you've implemented your ABag constructor(s).
 //This pointer gives you access to the bag which stores your data and provides the
 //functions you need to build your dictionary.
 ABag>* dictionary;
};
#endif /* BDICTIONARY_H */