Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Questions 1. Consider a linked chain of three nodes, such that each node contain

ID: 638309 • Letter: Q

Question

Questions

1. Consider a linked chain of three nodes, such that each node contains a string. The first node contains "A", the second node contains "B", and the third node contains "C".

Write C++ statements that create the described linked chain. Beginning with a head pointer headPtr that contains nullptr, create and attach a node for "C", then create and attach a node for "B", and finally create and attach a node for "A".

b. Repeat part a, but instead create and attach nodes in the order "A", "B", "C"

2. Why are only a few changes necessary to reuse the code in Listing 3-2?

3. Why is a LinkedBag object not concerned about becoming full?

4. Suppose that the ADT bag had an operation that displayed its contents. Write a C++ definition for such a method for the class LinkedBag.

5. How many assignment operations does the method that you wrote for the previous question require?

6. If the pointer variable curPtr becomes nullptr in the method getPointerTo, what value does the method contains return when the bag is not empty?

7. Trace the execution of the method contains when the bag is empty.

8. Revise the definition of the method getPointerTo so that the loop is controlled by a counter and the value of itemCount.

9. What is a disadvantage of the definition of the method getPointerTo, as described in the previous question, when compared to its original definition?

10. Why should the method getPointerTo not be made public?

11. Given the previous definition of the method remove, which entry in a bag can be deleted in the least time? Why?

12. Given the previous definition of the method remove, which entry in a bag takes the most time to delete? Why?

Explanation / Answer

1.

LinkedBag<string> bag;

1. a.

bag.add("C");
bag.add("B");
bag.add("A");

------------------------------------------------------------------------------------------------------

1. b.

bag.add("A");
bag.add("B");
bag.add("C");

------------------------------------------------------------------------------------------------------

3.

You can add any number of nodes to the LinkedBag object.
There is no maximum size or limit for adding objects in the linked lists.

------------------------------------------------------------------------------------------------------

4.
template<class ItemType>
void LinkedBag<ItemType>::displayBag() const
{
Node<ItemType>* curPtr = headPtr;

while(curPtr != nullptr)
{
  cout << curPtr->getItem() << " ";
  curPtr = curPtr->getNext();
}
cout << endl;
}

------------------------------------------------------------------------------------------------------

5.

2 (Two) assignment operarators (=) are required.
Node<ItemType>* curPtr = headPtr;
curPtr = curPtr->getNext();

------------------------------------------------------------------------------------------------------

6.

If the argument variable anEntry is not found the non-empty bag,
then the variable curPtr reaches the nextNodePtr of the last node.
So, the method returns a nullPtr.

------------------------------------------------------------------------------------------------------

8.

template<class ItemType>
Node<ItemType>* LinkedBag<ItemType>::getPointerTo(const ItemType& anEntry) const
{
bool found = false;
Node<ItemType>* curPtr = headPtr;

int curPosition = 0;

while(!found && (curPtr < itemCount))
{
  if(anEntry == curPtr->getItem())
   found = true;
  else
  {
   curPtr = curPtr->getNext();
   curPosition++;
  }
}

return curPtr;
}