Question

C++ programming language: In this program you will create a simplified bag that acts like a...

C++ programming language:

In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in any order you like. There is only 1 test case, do not modify main but feel free to comment out portions of main and uncomment as you implement.

PUT ALL YOUR CODE IN bag.cpp

bag();

The default constructor should create an empty bag represented by an empty list

void insertItem(const value_type& entry);

Insert should put the item at the front of the list

std::ostream& operator <<(std::ostream& outs, const bag& source)

prints the bag in order of last one inserted to first one inserted, as follows-- let's say the bag had the numbers 6,7,8,9 inserted in that order

Ordered Bag: 9 8 7 6

If it's empty the bag should print out

Ordered Bag: Empty

~bag(); //destructor

The destructor should release any dynamic memory

value_type removeItem();

Remove item should remove the last item inserted into the bag or return the number 0 if there's no item left in the bag (just for easiness-- if you return NULL you'll get an implicit conversion warning)

size_type bag::howMany(const value_type& entry)

returns the number of items with value equal to entry currently in the bag

void bag::operator =(const bag& source)

makes the current bag equal to the source bag via a deep copy

main.cpp

#include <iostream>
#include "bag.h"
using namespace std;
int main() {
cout<<"Creating bag a"<<endl;
bag a;
cout<<"Coutting a, should be empty"<<endl;
cout<<a<<endl;
cout<<"now adding 5 to a and printing"<<endl;
a.insertItem(5);
cout<<a<<endl;
cout<<"now adding 10 and 15, in that order, to a and printing"<<endl;
a.insertItem(10);
a.insertItem(15);
cout<<a<<endl;
  
cout<<"now removing FOUR items from a"<<endl;
cout<<"First item removed: "<<a.removeItem()<<endl;
cout<<"Second item removed: "<<a.removeItem()<<endl;
cout<<"Third item removed: "<<a.removeItem()<<endl;
cout<<"Fourth item removed: "<<a.removeItem()<<endl;
  
cout<<"now checking the number of 20's in a"<<endl;
cout<<"Number of 20s: "<<a.howMany(20)<<endl;
cout<<"Now adding 3 20's and 2 10's"<<endl;
a.insertItem(20);a.insertItem(20); a.insertItem(20); a.insertItem(10); a.insertItem(10);
cout<<"Number of 20s: "<<a.howMany(20)<<endl;
cout<<"Number of 10s: "<<a.howMany(10)<<endl;
cout<<"Now making bag b and inserting 200 into it"<<endl;
bag b;
b.insertItem(200);
cout<<"now setting b equal to a"<<endl;
b=a;
cout<<"bag a is "<<a<<endl;
cout<<"bag b is "<<b<<endl;
cout<<"now inserting 1000 into a and 200 into b"<<endl;
a.insertItem(1000);
b.insertItem(200);
cout<<"bag a is "<<a<<endl;
cout<<"bag b is "<<b<<endl;
}

bag.h

#ifndef BAG_H_ //macroguard that stops the header from being included twice.
#define BAG_H_ //Including the header twice stops the compilation, Note no Semilcolon
#include <cstdlib>
#include <iostream>
#include "Node.h"
class bag
{
public:
typedef std::size_t size_type;
typedef int value_type; //we define value type to be an int
bag ();
~bag(); //destructor
void insertItem(const value_type& entry); //inserts into your bag
value_type removeItem();//erases the last in item from the bag and returns it
size_type howMany(const value_type& entry); //returns how many of a particular item is in the bag
void operator =(const bag& source);//sets two bags equal
friend std::ostream& operator <<(std::ostream& outs, const bag& source);//couts the bag contents
private:
Node *head;
};

#endif

bag.cpp

#include <iostream>
#include "bag.h"
typedef std::size_t size_type;
typedef int value_type; //we define value type to be an int

bag::bag ()
{
}


bag::~bag()
{

}

void bag::insertItem(const value_type& entry)
{

}

value_type bag::removeItem()
{


}

size_type bag::howMany(const value_type& entry)
{

}

void bag::operator =(const bag& source)
{

}

std::ostream& operator <<(std::ostream& outs, const bag& source)
{

}

Node.h

#ifndef NODE_H
#define NODE_H
#include <cstdlib> // Provides size_t and NULL


class Node //Node is all inlined and implemented
{
public:
typedef int value_type;
  
   // CONSTRUCTOR
   Node(const value_type& init_data = value_type( ),Node* init_link = NULL)
   { data_field = init_data; link_field = init_link; }

   // Member functions to set the data and link fields:
   void set_data(const value_type& new_data) { data_field = new_data; }
   void set_link(Node* new_link) { link_field = new_link; }

   // Constant member function to retrieve the current data:
   value_type data( ) const { return data_field; }

   // Two slightly different member functions to retreive
   // the current link:
   const Node* link( ) const { return link_field; }
   Node* link( ) { return link_field; }
  
private:
   value_type data_field;
   Node* link_field;
};
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Node.h */

#ifndef NODE_H
#define NODE_H
#include <cstdlib> // Provides size_t and NULL


class Node //Node is all inlined and implemented
{
public:
typedef int value_type;
  
// CONSTRUCTOR
Node(const value_type& init_data = value_type( ),Node* init_link = NULL)
{ data_field = init_data; link_field = init_link; }

// Member functions to set the data and link fields:
void set_data(const value_type& new_data) { data_field = new_data; }
void set_link(Node* new_link) { link_field = new_link; }

// Constant member function to retrieve the current data:
value_type data( ) const { return data_field; }

// Two slightly different member functions to retreive
// the current link:
const Node* link( ) const { return link_field; }
Node* link( ) { return link_field; }
  
private:
value_type data_field;
Node* link_field;
};
#endif

/* bag.h */

#ifndef BAG_H_ //macroguard that stops the header from being included twice.
#define BAG_H_ //Including the header twice stops the compilation, Note no Semilcolon
#include <cstdlib>
#include <iostream>
#include "Node.h"
class bag
{
public:
typedef std::size_t size_type;
typedef int value_type; //we define value type to be an int
bag ();
~bag(); //destructor
void insertItem(const value_type& entry); //inserts into your bag
value_type removeItem();//erases the last in item from the bag and returns it
size_type howMany(const value_type& entry); //returns how many of a particular item is in the bag
void operator =(const bag& source);//sets two bags equal
friend std::ostream& operator <<(std::ostream& outs, const bag& source);//couts the bag contents
private:
Node *head;
};

#endif

/* bag.cpp */

#include <iostream>
#include "bag.h"
typedef std::size_t size_type;
typedef int value_type; //we define value type to be an int

bag::bag()
{
   head = NULL;
   this->howMany(0);
}


bag::~bag()
{
   Node* current = head;
   while( current != NULL) {
   Node* n = current->link();
   delete current;
   current = n;
   }
   head = NULL;
}

void bag::insertItem(const value_type& entry)
{
   Node *p = new Node(entry);
   if(head == NULL){
       head = p;
       return;
   }
   p->set_link(head);
   head = p;
}

value_type bag::removeItem()
{
   if(head == NULL){
       return 0;
   }else{
       value_type d = head->data();
       head = head->link();
       return d;
   }  

}

size_type bag::howMany(const value_type& entry)
{
   value_type count = 0;
   Node *p = head;
   while(p!=NULL){
       if(p->data() == entry)
           count++;
       p = p->link();
   }
   return count;
}

void bag::operator =(const bag& source)
{
   if(this!=&source){
  
       if (source.head == NULL) {
           head = NULL;
           return;
       }
           Node* p = source.head; // points to current node on other
           Node* tmp = new Node(p->data()); // make a copy of the first node
           head = tmp;
           Node* tail = tmp; // points to last node of this list
           while (p->link() != nullptr) {
               p = p->link();
               tmp = new Node(p->data());
               tail->set_link(tmp);
               tail = tmp;
           }
      
   }
}

std::ostream& operator <<(std::ostream& outs, const bag& source)
{
   if(source.head == NULL){
       outs<<"Ordered Bag: Empty\n";
   }else{
       outs<<"Ordered Bag: ";
       Node *cur = source.head;
       while(cur!=NULL){
           outs<<cur->data()<<" ";
           cur = cur->link();
       }
       outs<<std::endl;      
   }
   return outs;  
}

/* main.cpp */

#include <iostream>
#include "bag.h"
using namespace std;
int main() {
cout<<"Creating bag a"<<endl;
bag a;
cout<<"Coutting a, should be empty"<<endl;
cout<<a<<endl;
cout<<"now adding 5 to a and printing"<<endl;
a.insertItem(5);
cout<<a<<endl;
cout<<"now adding 10 and 15, in that order, to a and printing"<<endl;
a.insertItem(10);
a.insertItem(15);
cout<<a<<endl;
  
cout<<"now removing FOUR items from a"<<endl;
cout<<"First item removed: "<<a.removeItem()<<endl;
cout<<"Second item removed: "<<a.removeItem()<<endl;
cout<<"Third item removed: "<<a.removeItem()<<endl;
cout<<"Fourth item removed: "<<a.removeItem()<<endl;
  
cout<<"now checking the number of 20's in a"<<endl;
cout<<"Number of 20s: "<<a.howMany(20)<<endl;
cout<<"Now adding 3 20's and 2 10's"<<endl;
a.insertItem(20);a.insertItem(20); a.insertItem(20); a.insertItem(10); a.insertItem(10);
cout<<"Number of 20s: "<<a.howMany(20)<<endl;
cout<<"Number of 10s: "<<a.howMany(10)<<endl;
cout<<"Now making bag b and inserting 200 into it"<<endl;
bag b;
b.insertItem(200);
cout<<"now setting b equal to a"<<endl;
b=a;
cout<<"bag a is "<<a<<endl;
cout<<"bag b is "<<b<<endl;
cout<<"now inserting 1000 into a and 200 into b"<<endl;
a.insertItem(1000);
b.insertItem(200);
cout<<"bag a is "<<a<<endl;
cout<<"bag b is "<<b<<endl;
}

/* OUTPUT */

/* PLEASE UPVOTE */

Add a comment
Know the answer?
Add Answer to:
C++ programming language: In this program you will create a simplified bag that acts like a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT