Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ArrayList.h: 8 : #ifndef _ ARRAY _ LIST 9 : #define _ ARRAY _ LIST 1 1 : #include ListInterface.h 1 2 : #include

ArrayList.h:
8: #ifndef _ARRAY_LIST
9: #define _ARRAY_LIST
11: #include "ListInterface.h"
12: #include "PrecondViolatedExcep.h"
14: template
15: class ArrayList : public ListInterface
16: {
17: private:
18: static const int DEFAULT_CAPACITY =5; // Small capacity to test for a full list
19: ItemType items[DEFAULT_CAPACITY]; // Array of list items
20: int itemCount; // Current count of list items
21: int maxItems; // Maximum capacity of the list
22:
23: public:
24: ArrayList();
25: // Copy constructor and destructor are supplied by compiler
26:
27: bool isEmpty() const;
28: int getLength() const;
29: bool insert(int newPosition, const ItemType& newEntry);
30: bool remove(int position);
31: void clear();
32:
33: /** @throw PrecondViolatedExcep if position 1 or
34: position > getLength().*/
35: ItemType getEntry(int position) const throw(PrecondViolatedExcep);
37: /** @throw PrecondViolatedExcep if position 1 or
38: position > getLength().*/
39: void setEntry(int position, const ItemType& newEntry)
40: throw(PrecondViolatedExcep);
41: }; // end ArrayList
43: #include "ArrayList.cpp"
44: #endif
ArrayList.cpp:
7: #include "ArrayList.h"// Header file
9: template
10: ArrayList::ArrayList() : itemCount(0), maxItems(DEFAULT_CAPACITY)
11: {
12: }// end default constructor
14: template
15: bool ArrayList::isEmpty() const
16: {
17: return itemCount ==0;
18: }// end isEmpty
20: template
21: int ArrayList::getLength() const
22: {
23: return itemCount;
24: }// end getLength
26: template
27: bool ArrayList::insert(int newPosition, const ItemType& newEntry)
28: {
29: bool ableToInsert =(newPosition >=1) && (newPosition = itemCount +1) &&
30: (itemCount maxItems);
31: if (ableToInsert)
32: {
33: // Make room for new entry by shifting all entries at
34: // positions >= newPosition toward the end of the array
35: //(no shift if newPosition == itemCount +1)
36: for (int pos = itemCount; pos >= newPosition; pos--)
37: items[pos]= items[pos -1];
38:
39: // Insert new entry
40: items[newPosition -1]= newEntry;
41: itemCount++; // Increase count of entries
42: }// end if
43:
44: return ableToInsert;
45: }// end insert
47: template
48: bool ArrayList::remove(int position)
49: {
50: bool ableToRemove =(position >=1) && (position = itemCount);
51: if (ableToRemove)
52: {
53: // Remove entry by shifting all entries after the one at
54: // position toward the beginning of the array
55: //(no shift if position == itemCount)
56: for (int fromIndex = position, toIndex = fromIndex -1; fromIndex itemCount;
57: fromIndex++, toIndex++)
58: items[toIndex]= items[fromIndex];
59:
60: itemCount--; // Decrease count of entries
61: }// end if
62:
63: return ableToRemove;
64: }// end remove
66: template
67: void ArrayList::clear()
68: {
69: itemCount =0;
70: }// end clear
72: template
73: ItemType ArrayList::getEntry(int position) const throw(PrecondViolatedExcep)
74: {
75: // Enforce precondition
76: bool ableToGet =(position >=1) && (position = itemCount);
77: if (ableToGet)
78: return items[position -1];
79: else
80: {
81: string message = "getEntry() called with an empty list or ";
82: message = message + "invalid position.";
83: throw(PrecondViolatedExcep(message));
84: }// end if
85: }// end getEntry
87: template
88: void ArrayList::setEntry(int position, const ItemType& newEntry) throw(PrecondViolatedExcep)
89: {
90: // Enforce precondition
91: bool ableToSet =(position >=1) && (position = itemCount);
92: if (ableToSet)
93: items[position -1]= newEntry;
94: else
95: {
96: string message = "setEntry() called with an empty list or ";
97: message = message + "invalid position.";
98: throw(PrecondViolatedExcep(message));
99: }// end if
100: }
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions