C++ PROGRAMMING (Linked list) complete the functions: You have to continue on implementing your Array List namely the following functions: Example ArrayList: [10, 30, 40, 50] void addAt(int num, int pos) This method will add the integer num to the posth position of the list. Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the array will now look like this: [10, 20, 30, 40, 50] When the value of pos is greater than the size + 1 or less than one, output "Position value invalid" void removeAt(int pos) Removes the number in the posth position of the list. Performing removeAt(3) in the example list will remove the 3rd element of the list and the updated array will be: [10, 30, 50] When the value of pos is greater than the size or less than one, output "Position value invalid"
C++
(Linked list) complete the functions:
You have to continue on implementing your Array List namely the following functions:
Example ArrayList: [10, 30, 40, 50]
- void addAt(int num, int pos)
This method will add the integer num to the posth position of the list.
Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the array will now look like this: [10, 20, 30, 40, 50]
When the value of pos is greater than the size + 1 or less than one, output "Position value invalid"
- void removeAt(int pos)
Removes the number in the posth position of the list.
Performing removeAt(3) in the example list will remove the 3rd element of the list and the updated array will be: [10, 30, 50]
When the value of pos is greater than the size or less than one, output "Position value invalid"
My incomplete code:
#include <cstdlib>
#include <iostream>
using namespace std;
class ArrayList : public List { // : means "is-a" / extend
int* array;
int index;
public:
// CONSTRUCTOR
ArrayList() {
array = (int*) malloc(10);
index = 0;
}
void add(int num) {
*(array + index) = num;
index++;
}
void remove(int num){ // first num that we found
// FIND the num
for (int i = 0; i < index; i++) {
if (num == *(array + i)) { // we have found the num
for (int j = i; j < index - 1; j++) {
*(array + j) = *(array + j + 1);
}
index--;
return;
}
}
}
int get(int pos){
return *(array + pos - 1);
}
int size(){
return index;
}
void print() {
cout << "[";
for (int i = 0; i < index; i++) {
cout << *(array + i);
if (i != index - 1) {
cout << ", ";
}
}
cout << "]";
}
void addAt(int num, int pos) {
}
void removeAt(int pos) {
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps









