Question

In: Computer Science

Write a member function that deletes all repetitions from the bag. In your implementation, assume that...

Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using ==. Use the following header for the function:

void 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

This is what I have so far, the issue being it doesn't delete properly and does not print out the list. Thanks in advance!

template <class Item>
void bag<Item>::remove_repetitions(){
    std::cout<<"Working progress"<<std::endl;
    node<Item> *p;
    //node<Item> *temp;
    p=head_ptr;
    while(p!=NULL && p->link()!=NULL){
        node<Item> *q;
        node<Item> *target;
        for(q=p->link(); q!=NULL; q=q->link()){
            if(p->data()==q->data()){
                target = q;
                q->set_link(q->link());
                delete(target);
            }
        }
        p=p->link();
    }
}

//The main file

#include <cstdlib>
#include <iostream>
#include <set>
#include <algorithm>
#include "node2.h"
#include "bag5.h"

using namespace std;

// PROTOTYPE for a function used by this demonstration program
template <class Item, class SizeType, class MessageType>
void get_items(bag<Item>& collection, SizeType n, MessageType description)
// Postcondition: The string description has been written as a prompt to the
// screen. Then n items have been read from cin and added to the collection.
// Library facilities used: iostream, bag4.h
{
    Item user_input; // An item typed by the program's user
    SizeType i;

    cout << "Please type " << n << " " << description;
    cout << ", separated by spaces.\n";
    cout << "Press the <return> key after the final entry:\n";
    for (i = 1; i <= n; ++i)
    {
        cin >> user_input;
        collection.insert(user_input);
    }
    cout << endl;
}

int main()
{
    //demonstrate bag template class
    bag<int> bag_of_int;
    bag<string> bag_of_string;

    bag_of_int.insert(3);
    bag_of_string.insert("hello");
    bag_of_string.insert("goodbye");
    bag_of_string.insert("auf wiedersehen");
    bag_of_string.insert("goodbye");
    bag_of_string.insert("hello");
    bag_of_string.insert("goodbye");

    cout << "count of goodbye: " << bag_of_string.count("goodbye") << endl;
    cout << "count of guten morgen: " << bag_of_string.count("guten morgen") << endl;
    cout << "count of 3: " << bag_of_int.count(3) << endl;
  

    for(bag<string>::iterator cursor = bag_of_string.begin(); cursor != bag_of_string.end(); ++cursor)
        cout<<*cursor<< " ";
    cout<<endl;
  
   bag<int> bag_int;
   bag_int.insert (7);
   bag_int.insert (6);
   bag_int.insert (5);
   bag_int.insert (4);
   bag_int.insert (5);
   bag_int.insert (3);
   bag_int.insert (2);
   bag_int.insert (1);
   bag_int.insert (1);
   bag_int.insert (5);
   bag_int.print_value_range (5,20);
   bag_int.remove_repetitions();
   bag_int.print_value_range (5,20);
   return EXIT_SUCCESS;
  
}

#ifndef NODE2_H
#define NODE2_H

//the node header file with template implementations
#include <cstdlib>   // Provides NULL and size_t
#include <iterator> // Provides iterator and forward_iterator_tag


template <class Item>
class node
{
public:
        // TYPEDEF
        typedef Item value_type;
        // CONSTRUCTOR
        node(const Item& init_data=Item( ), node* init_link=NULL)
            { data_field = init_data; link_field = init_link; }
        // MODIFICATION MEMBER FUNCTIONS
        Item& data( ) { return data_field; }
        node* link( ) { return link_field; }
        void set_data(const Item& new_data) { data_field = new_data; }
        void set_link(node* new_link) { link_field = new_link; }
        // CONST MEMBER FUNCTIONS
        const Item& data( ) const { return data_field; }
        const node* link( ) const { return link_field; }
private:
        Item data_field;
        node *link_field;
};

// FUNCTIONS to manipulate a linked list:
template <class Item>
void list_clear(node<Item>*& head_ptr);

template <class Item>
void list_copy
     (const node<Item>* source_ptr, node<Item>*& head_ptr, node<Item>*& tail_ptr);

template <class Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry);

template <class Item>
void list_head_remove(node<Item>*& head_ptr);

template <class Item>
void list_insert(node<Item>* previous_ptr, const Item& entry);

template <class Item>
std::size_t list_length(const node<Item>* head_ptr);

template <class NodePtr, class SizeType>
NodePtr list_locate(NodePtr head_ptr, SizeType position);

template <class Item>
void list_remove(node<Item>* previous_ptr);

template <class NodePtr, class Item>
NodePtr list_search(NodePtr head_ptr, const Item& target);

// FORWARD ITERATORS to step through the nodes of a linked list
// A node_iterator of can change the underlying linked list through the
// * operator, so it may not be used with a const node. The
// node_const_iterator cannot change the underlying linked list
// through the * operator, so it may be used with a const node.
// WARNING:
// This classes use std::iterator as its base class;
// Older compilers that do not support the std::iterator class can
// delete everything after the word iterator in the second line:

template <class Item>
class node_iterator : public std::iterator<std::forward_iterator_tag, Item>
{
public:
       node_iterator(node<Item>* initial = NULL){ current = initial; }
      
        Item& operator *( ) const { return current->data( ); }
      
        node_iterator& operator ++( ) // Prefix ++
        {
       current = current->link( );
       return *this;
        }
      
        node_iterator operator ++(int) // Postfix ++
        {
       node_iterator original(current);
       current = current->link( );
       return original;        
        }
      
        bool operator ==(const node_iterator other) const { return current == other.current; }
      
        bool operator !=(const node_iterator other) const { return current != other.current; }
private:
        node<Item>* current;
};

template <class Item>
class const_node_iterator : public std::iterator<std::forward_iterator_tag, const Item>
{
public:
       const_node_iterator(const node<Item>* initial = NULL) { current = initial; }
      
        const Item& operator *( ) const { return current->data( ); }
      
        const_node_iterator& operator ++( ) // Prefix ++
        {
       current = current->link( );
       return *this;
        }
      
        const_node_iterator operator ++(int) // Postfix ++
        {
       const_node_iterator original(current);
       current = current->link( );
       return original;
        }
      
        bool operator ==(const const_node_iterator other) const { return current == other.current; }
      
        bool operator !=(const const_node_iterator other) const { return current != other.current; }
private:
   const node<Item>* current;
};


#include "node2.template"


#endif // NODE2_H

#ifndef BAG5_H
#define BAG5_H

//bag header file with template
#include <cstdlib>   // Provides NULL and size_t and NULL
#include "node2.h"   // Provides node class


template <class Item>
class bag
{
public:
    // TYPEDEFS
   typedef std::size_t size_type;
        typedef Item value_type;
   typedef node_iterator<Item> iterator;
   typedef const_node_iterator<Item> const_iterator;
  
        // CONSTRUCTORS and DESTRUCTOR
        bag( );
        bag(const bag& source);
        ~bag( );
  
        // MODIFICATION MEMBER FUNCTIONS
        size_type erase(const Item& target);
        bool erase_one(const Item& target);
        void insert(const Item& entry);
        void operator +=(const bag& addend);
        void operator =(const bag& source);
        void print_value_range(const Item& x, const Item& y);
        void remove_repetitions(); //<-- Implement this!!!!!

        // CONST MEMBER FUNCTIONS
        size_type count(const Item& target) const;
        Item grab( ) const;
        size_type size( ) const { return many_nodes; }
  
   // FUNCTIONS TO PROVIDE ITERATORS
   iterator begin( )
        { return iterator(head_ptr); }
   const_iterator begin( ) const
        { return const_iterator(head_ptr); }
   iterator end( )
        { return iterator( ); } // Uses default constructor
   const_iterator end( ) const
        { return const_iterator( ); } // Uses default constructor

private:
        node<Item> *head_ptr;        // Head pointer for the list of items
        size_type many_nodes;        // Number of nodes on the list
};

// NONMEMBER functions for the bag
template <class Item>
bag<Item> operator +(const bag<Item>& b1, const bag<Item>& b2);


// The implementation of a template class must be included in its header file:
#include "bag5.template"


#endif // BAG5_H

Solutions

Expert Solution

template <class Item>
void bag<Item>::remove_repetitions(){
  
   std::cout<<"Working progress"<<std::endl;
node<Item> *p = head_ptr;
  
   // loop over the bag from start to last element
   while(p!=NULL && p->link()!=NULL){
// set q equal to p
       node<Item> *q = p;
       // set next equal to node next to p
       node<Item> *next = p->link();
// loop over the bag from node next to p to end
       while(next != NULL)
       {
// next's data = p's data
           if(p->data() == next->data())
           {
               // delete next node
               // update link of q to node next to next
q->set_link(next->link());
               // delete the next node
               next->set_link(NULL);
               delete(next);
               // update next to new node next to q
               next = q->link();
}
           else // next's data != p's data
           {
               // set q to next
               q = next;
               // update next to node after next
               next = next->link();
           }
}
      
       // set p to node next to p
p=p->link();
}  
}


Related Solutions

In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
*In C++ Please! Give the full implementation of a constant member function that returns the second...
*In C++ Please! Give the full implementation of a constant member function that returns the second element from the top of the stack without actually changing the stack. Write separate solutions for the two different stack versions. (C++ program for array implementation of stack with insert function and display of the second element from top)
Write pseudo code for Min_Heap_Delete(A,i) which deletes the item at position i in Min_Heap A. Assume...
Write pseudo code for Min_Heap_Delete(A,i) which deletes the item at position i in Min_Heap A. Assume that A is a one-dimensional array. Hint:Think about “bubbling up” and “bubbling down” and the operations that do these, then give the best upper bound you can on the worst case running time of your method, in ordered notations.
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the Queue class: getLength(): returns the number of elements in the queue isEmpty(): returns true if the queue is empty, false otherwise peek(): returns the value at the front of the queue without removing it. Assumes queue is not empty (no need to check) peekRear(): returns the value at the end of the queue without removing it. Assumes queue is not empty (no need to...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
Write a c++ member function that sequentially searches for a specific element in a doubly linked...
Write a c++ member function that sequentially searches for a specific element in a doubly linked list. return the position if found or -1 is the element cannot be found.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT