Define an ADT for a sequence of integers (remember that a sequence may contain duplicates,
and supports the concept of position for its elements). Your ADT should consist of the functions
that can be performed on a sequence to control its membership, check the size, check if a given
element is in the set, and so on. Each function should be defined in terms of its input and output.
Data Structures and Algorithm Analysis in C++ by Clifford Shaffer
Define an ADT for a sequence of integers (remember that a sequence may contain duplicates,
and supports the concept of position for its elements). Your ADT should consist of the functions
that can be performed on a sequence to control its membership, check the size, check if a given
element is in the set, and so on. Each function should be defined in terms of its input and output.
Data Structures and Algorithm Analysis in C++ by Clifford Shaffer
Data Structures and Algorithm Analysis in C++ by Clifford Shaffer
Solution
#include
#include
using namespace std;
/*
List ADT Implementation for Integers
Supports: Duplicates values and position based access.
Change in membership of postion.
check size, availability in set.
Can be resized automatically based on requirement of size.
*/
class Arraylist
{
int *integerArray;
int length;
int size;
public:
Arraylist(int defaultsize = 10)
{
integerArray = new int[size];
length = 0;
size = defaultsize;
}
// To get integer at index i
int get(int index)
{
if(!IsIndexValidated(index)) {
cout<< "The index is invalid";
return -1;
}
return integerArray[index];
}
// To get array length
int lengthOfArray()
{
return length;
}
// To set integer at index i
bool setAtIndex(int index, int value)
{
if(!IsIndexValidated(index)) {
return false;
}
integerArray[index] = value;
return true;
}
// To add integer in the list
bool add(int value)
{
expandArrayIfrequired();
integerArray[length++] = value;
return true;
}
// To remove integer in the list by index
bool remove(int index)
{
if(!IsIndexValidated(index)) {
return false;
}
for (int i = index; i < length; i++)
integerArray[i] = integerArray[i+1];
integerArray[length-1] = 0;
length--;
return true;
}
// To check the availability of value in integerArray
bool isValuePresent(int value)
{
for(int i=0;i=length) {
return false;
}
return true;
}
bool expandArrayIfrequired(){
if(length >= size) {
int* resize_arr = new int[size *2];
for(int i = 0; i < size; i++)
resize_arr[i] = integerArray[i];
size++;
integerArray = resize_arr;
delete[] resize_arr;
return true;
}
return false;
}
};
//Main method to test ArrayList
int main()
{
Arraylist list(1);
cout<<"Array List of size 1 created"<

Define an ADT for a sequence of integers (remember that a sequenc.pdf

  • 1.
    Define an ADTfor a sequence of integers (remember that a sequence may contain duplicates, and supports the concept of position for its elements). Your ADT should consist of the functions that can be performed on a sequence to control its membership, check the size, check if a given element is in the set, and so on. Each function should be defined in terms of its input and output. Data Structures and Algorithm Analysis in C++ by Clifford Shaffer Define an ADT for a sequence of integers (remember that a sequence may contain duplicates, and supports the concept of position for its elements). Your ADT should consist of the functions that can be performed on a sequence to control its membership, check the size, check if a given element is in the set, and so on. Each function should be defined in terms of its input and output. Data Structures and Algorithm Analysis in C++ by Clifford Shaffer Data Structures and Algorithm Analysis in C++ by Clifford Shaffer Solution #include #include using namespace std; /* List ADT Implementation for Integers Supports: Duplicates values and position based access. Change in membership of postion. check size, availability in set. Can be resized automatically based on requirement of size. */ class Arraylist { int *integerArray; int length;
  • 2.
    int size; public: Arraylist(int defaultsize= 10) { integerArray = new int[size]; length = 0; size = defaultsize; } // To get integer at index i int get(int index) { if(!IsIndexValidated(index)) { cout<< "The index is invalid"; return -1; } return integerArray[index]; } // To get array length int lengthOfArray() { return length; } // To set integer at index i bool setAtIndex(int index, int value) { if(!IsIndexValidated(index)) { return false; } integerArray[index] = value; return true; } // To add integer in the list
  • 3.
    bool add(int value) { expandArrayIfrequired(); integerArray[length++]= value; return true; } // To remove integer in the list by index bool remove(int index) { if(!IsIndexValidated(index)) { return false; } for (int i = index; i < length; i++) integerArray[i] = integerArray[i+1]; integerArray[length-1] = 0; length--; return true; } // To check the availability of value in integerArray bool isValuePresent(int value) { for(int i=0;i=length) { return false; } return true; } bool expandArrayIfrequired(){ if(length >= size) { int* resize_arr = new int[size *2]; for(int i = 0; i < size; i++) resize_arr[i] = integerArray[i]; size++;
  • 4.
    integerArray = resize_arr; delete[]resize_arr; return true; } return false; } }; //Main method to test ArrayList int main() { Arraylist list(1); cout<<"Array List of size 1 created"<