Obtaining linked list size recursively
Mar 2, 2015 at 8:52pm UTC
I am having trouble with link lists and recursion I was hoping someone can guide through this question:
suppose that the class LinkedBag did not have the data member item_count_. Revise the method getCurrentSize so that it counts the number of nodes in the linked chain a. iterartively b. Recursively
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
LinkedBag.h
template <class ItemType>
class LinkedBag : public BagInterface<ItemType>
{
public :
LinkedBag();
LinkedBag(const LinkedBag<ItemType>& a_bag);
LinkedBag<ItemType>& operator =(const LinkedBag<ItemType>& right_hand_side);
virtual ~LinkedBag();
int GetCurrentSize() const ;
bool IsEmpty() const ;
bool Add(const ItemType& new_entry);
bool Remove(const ItemType& an_entry);
void Clear();
bool Contains(const ItemType& an_ntry) const ;
int GetFrequencyOf(const ItemType& an_entry) const ;
vector<ItemType> ToVector() const ;
private :
Node<ItemType>* head_ptr_; // Pointer to first node
int item_count_;
LinkedBag.cpp
template <class ItemType>
LinkedBag<ItemType>::LinkedBag() : head_ptr_(nullptr ), item_count_(0)
{
}
template <class ItemType>
LinkedBag<ItemType>::LinkedBag(const LinkedBag<ItemType>& a_bag)
{
CopyNodesFrom(a_bag);
}
template <class ItemType>
LinkedBag<ItemType>& LinkedBag<ItemType>::operator =(const LinkedBag<ItemType>& right_hand_side) {
if (this != &right_hand_side) {
Clear(); // First deallocate all memory.
CopyNodesFrom(right_hand_side); // Then copy everything.
}
return *this ; // Return self.
}
template <class ItemType>
bool LinkedBag<ItemType>::IsEmpty() const
{
return item_count_ == 0;
}
template <class ItemType>
int LinkedBag<ItemType>::GetCurrentSize() const
{
return item_count_;
}
template <class ItemType>
bool LinkedBag<ItemType>::Add(const ItemType& new_entry)
{
Node<ItemType>* next_node_ptr = new Node<ItemType>(new_entry, head_ptr_);
head_ptr_ = next_node_ptr; // New node is now first node
item_count_++;
return true ;
}
template <class ItemType>
vector<ItemType> LinkedBag<ItemType>::ToVector() const
{
vector<ItemType> bag_contents;
Node<ItemType>* current_ptr = head_ptr_;
int counter = 0;
while (current_ptr != nullptr ) {
bag_contents.push_back(current_ptr->GetItem());
current_ptr = current_ptr->GetNext();
counter++;
// Check for possible serious error.
if (counter > item_count_) {
cout << "Serious error in LinkedBag<ItemType>::ToVector" << endl;
return bag_contents;
}
} // end while
return bag_contents;
}
template <class ItemType>
Node<ItemType>* LinkedBag<ItemType>::GetPointerTo(const ItemType& an_entry) const
{
Node<ItemType> *current_ptr = head_ptr_;
while (current_ptr != nullptr ) {
if (an_entry == current_ptr->GetItem())
return current_ptr;
else
current_ptr = current_ptr->GetNext();
} // end while
return nullptr ;
}
so right now we have the getcurrentsize function as:
template<class ItemType>
1 2 3 4
int LinkedBag<ItemType>::GetCurrentSize() const
{
return item_count_;
}
Mar 2, 2015 at 9:14pm UTC
would it be something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int GetCurrentSize(node* p)
{
if (!p)
return 0;
if (!p->_next_node)
return 1;
return 1 + count(p->next);
}
int GetCurrentSize(node* cur){
while (cur != NULL){
int count;
cur->next_node;
count++
}
}
Topic archived. No new replies allowed.