Define an ADT for a sequence of integers remember that a seq

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<iostream>

#include<stdlib.h>

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;i++)

if(integerArray[i] == value) {

return true;

}

return false;

}

  

// To print all the values in the list.

void printList()

{

if(length==0){

cout <<\"None\";

}

for(int i=0;i<length;i++)

cout <<integerArray[i]<<\" \";

cout<<endl;

}

  

private:

bool IsIndexValidated(int index){

if(index<=0 || index>=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\"<<endl;

cout<<\"Display list\"<<endl;

list.printList();

  

cout<<\"Adding 2 and 3\"<<endl;

list.add(2);

list.add(3);

  

cout<<\"Display list\"<<endl;

list.printList();

  

cout<<\"Get Value at index 1\"<<endl;

cout << list.get(1)<<endl;

cout<<\"Get Value at index 2\"<<endl;

cout << list.get(2)<<endl;

  

cout<<\"Adding 6 and 10\"<<endl;

list.add(6);

list.add(10);

  

cout<<\"Length of Array\"<<endl;

cout << list.lengthOfArray()<<endl;

  

cout<<\"Display List\"<<endl;

list.printList();

  

cout<<\"Remove from index 1\"<<endl;

list.remove(1);

  

cout<<\"Display List\"<<endl;

list.printList();

  

cout<<\"Does value 10 present in array\"<<endl;

cout << list.isValuePresent(10) <<endl;

  

cout<<\"Set index 1 value as 100\"<<endl;

list.setAtIndex(1,100);

  

cout<<\"Display List\"<<endl;

list.printList();

return (0);

}

 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 sho
 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 sho
 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 sho
 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 sho
 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 sho
 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 sho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site