Previous Section
 < Free Open Study > 
Next Section


Chapter 5

    1. The client code does not change when the implementation of the class changes. See answer to Chapter 4, Exercise 11.

  1. bool Identical(StackType<ItemType> stack1) const; // Prototype
    template<class ItemType)
    bool StackType<ItemType>::
       Identical(StackType<ItemType> stack1) const
    {
      NodeType<ItemType>* selfPtr;
      NodeType<ItemType>* otherPtr;
      bool same = true;G13
    
      selfPtr = topPtr;
      otherPtr = stack1.topPtr;
      while (same)
      {
        same = (selfPtr != NULL && otherPtr != NULL
            && selfPtr->info == otherPtr->info);
        if (same)
        {
          selfPtr = selfPtr->next;
          otherPtr = otherPtr->next;
        }
      }
      if (selfPtr == NULL && otherPtr == NULL)
        return true;
      else
        return false;
    }
    
  1. int Length(): // Prototype
    template<class ItemType>
    int QueType<ItemType>::Length()
    {
      NodeType<ItemType>* location = qFront:
      int length = 0;
    
      while (location != NULL)
      {
        length++;
        location = location->next;
      }
      return length;
    }
    
  2. template<class ItemType>
    bool UnsortedType<ItemType>::IsThere(ItemType item)
    {
      NodeType<ItemType>* location = listData;
      bool found = false;
    
      while (!found && location != NULL)
      {
        found = location->info == item;
        location = location->next;
      }
      return found;
    }
    
    1. void SplitLists(SortedType<ItemType>& list1,
         SortedType<ItemType>& list2, ItemType item); // Prototype
      template<class ItemType>
      void SortedType::SplitLists(SortedType<ItemType>& list1,
         SortedType<ItemType>& list2, ItemType item)
      {
        NodeType<ItemType>* listPtr = listData;
      
        list1.MakeEmpty();
        list2.MakeEmpty();
        while (listPtr != NULL && listPtr->info <= item)
        {
          list1.InsertItem(listPtr->info);
          listPtr = listPtr->next;
        }
        while (listPtr != NULL)
        {
          list2.InsertItem(listPtr->info);
          listPtr = listPtr->next;
        }
      }
      
    2. See Chapter 3, Exercise 8.

    1. True

    2. True

    3. False. A linked list is not a random-access structure.

    4. False. A sequential list may be stored in a statically allocated or a dynamically allocated structure.

    5. True

    6. False. A queue is not a random-access structure; access is always to the first one stored.

    1. 30

    2. 90

    3. 45

    1. true

    2. false

    3. false

    4. true

    1. ok

    2. You cannot assign a struct to a pointer.

    3. You cannot assign a pointer to a struct.

    4. You cannot assign an info member to a pointer.

    5. ok

    6. ok

    1. listData = ptr1->next;

    2. ptr2 = ptr2->next;

    3. listData = NULL

    4. ptr1->next->info = 60



Previous Section
 < Free Open Study > 
Next Section
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)