SlideShare a Scribd company logo
1 of 4
Download to read offline
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"<

More Related Content

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

Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
Bharat17485
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
Mark Needham
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 

Similar to Define an ADT for a sequence of integers (remember that a sequenc.pdf (20)

Bt0065
Bt0065Bt0065
Bt0065
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Bw14
Bw14Bw14
Bw14
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
 
Arrays
ArraysArrays
Arrays
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
The STL
The STLThe STL
The STL
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Cs341
Cs341Cs341
Cs341
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
Arrays
ArraysArrays
Arrays
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 

More from fazilfootsteps

Why are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdfWhy are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdf
fazilfootsteps
 
Why is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdfWhy is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdf
fazilfootsteps
 
What does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdfWhat does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdf
fazilfootsteps
 
Thoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdfThoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdf
fazilfootsteps
 
potential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdfpotential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdf
fazilfootsteps
 
Please write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdfPlease write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdf
fazilfootsteps
 
Module 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdfModule 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdf
fazilfootsteps
 
List and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdfList and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdf
fazilfootsteps
 
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdfIntroduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
fazilfootsteps
 
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdfIndian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
fazilfootsteps
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdf
fazilfootsteps
 

More from fazilfootsteps (20)

Why are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdfWhy are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdf
 
Why is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdfWhy is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdf
 
Using the phase diagram of polydstyrene and DOP below, would you ex.pdf
Using the phase diagram of polydstyrene and DOP below, would you ex.pdfUsing the phase diagram of polydstyrene and DOP below, would you ex.pdf
Using the phase diagram of polydstyrene and DOP below, would you ex.pdf
 
What does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdfWhat does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdf
 
True or False Statement The DNS namespace is hierarchical. Protocols.pdf
True or False Statement The DNS namespace is hierarchical. Protocols.pdfTrue or False Statement The DNS namespace is hierarchical. Protocols.pdf
True or False Statement The DNS namespace is hierarchical. Protocols.pdf
 
Thoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdfThoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdf
 
The symmetry properties of the plane figure formed by the four point.pdf
The symmetry properties of the plane figure formed by the four point.pdfThe symmetry properties of the plane figure formed by the four point.pdf
The symmetry properties of the plane figure formed by the four point.pdf
 
QUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdf
QUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdfQUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdf
QUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdf
 
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdf
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdfRobbinsdale Hospital is one of two hospitals among the six facilitie.pdf
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdf
 
Research excise taxes in other states besides North Carolina. Report.pdf
Research excise taxes in other states besides North Carolina. Report.pdfResearch excise taxes in other states besides North Carolina. Report.pdf
Research excise taxes in other states besides North Carolina. Report.pdf
 
Prove that E X is not connected if and only if there exist open sets.pdf
Prove that E  X is not connected if and only if there exist open sets.pdfProve that E  X is not connected if and only if there exist open sets.pdf
Prove that E X is not connected if and only if there exist open sets.pdf
 
potential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdfpotential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdf
 
Please write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdfPlease write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdf
 
Nessus is a network security tool- write a pragraph describe itsto.pdf
Nessus is a network security tool- write a pragraph describe itsto.pdfNessus is a network security tool- write a pragraph describe itsto.pdf
Nessus is a network security tool- write a pragraph describe itsto.pdf
 
Module 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdfModule 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdf
 
List and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdfList and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdf
 
Most integer types are directly supported by hardware, but some are n.pdf
Most integer types are directly supported by hardware, but some are n.pdfMost integer types are directly supported by hardware, but some are n.pdf
Most integer types are directly supported by hardware, but some are n.pdf
 
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdfIntroduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
 
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdfIndian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdf
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 

Recently uploaded (20)

Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 

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

  • 1. 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;
  • 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"<