Describe the changes to the previous stack implementation that are necessaryto replace the fixed-size array with a resizable array.

What will be an ideal response?


In the header file for ArrayStack, change the declaration of items and declare a new data member maxItems:
```
ItemType* items = new ItemType[DEFAULT_CAPACITY]; // Array of stack items
int maxItems; // Max capacity of the stack

```
Also, change the name of MAX_STACK to DEFAULT_CAPACITY to reflect its change in meaning.
In the implementation file, add an initializer to the definition of the constructor that initializes maxItems to DEFAULT_CAPACITY. Thus, the constructor appears as follows:
```
template
ArrayStack::ArrayStack() : top(-1), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor

```
Then change the definition of the pushmethod so that when the array—and hence the stack—becomes full, pushdoubles the capacity of the array instead of failing to add the data item and returning false. Since the stack is never full, the following version of the pushmethod alwaysreturns true:
```
template
boolArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (!hasRoomToAdd)
{
ItemType* oldArray = items;
items = newItemType[2 * maxItems];
for (int index = 0; index < maxItems; index++)
items[index] = oldArray[index];
delete [ ] oldArray;
maxItems = 2 * maxItems;
} // end if
// We can always add the item
items[itemCount] = newEntry;
itemCount++;
return true;
} // end push

```

Computer Science & Information Technology

You might also like to view...

________ are objects such as arrows, lines, and callouts

Fill in the blank(s) with correct word

Computer Science & Information Technology

The Notification area of Windows 8 is displayed in the bottom-left corner of the Start screen

Indicate whether the statement is true or false

Computer Science & Information Technology

________ are easy-to-use filtering controls within a PivotTable or PivotReport that enables you to intuitively drill down through large amounts of data in an interactive way

Fill in the blank(s) with correct word

Computer Science & Information Technology

If you are adding a hard drive to a system that already has a drive with Windows installed on it, what should you do?

A. Boot from the Windows setup DVD or USB to prepare the new drive. B. Boot Windows and use Disk Management to prepare the new drive. C. Use the BIOS setup screen to partition and format the new drive. D. Check that the new hard drive must use the same standard as the Windows drive.

Computer Science & Information Technology