Implement two functions specified in list.h. You need to fill in the implementat
ID: 3829223 • Letter: I
Question
Implement two functions specified in list.h. You need to fill in the implementation in list.cpp: bool insert(int val, int intList[], int& size) the above function will insert “val” into the correct position of a sorted“intList” and increment size. When doing the insertion, the list should maintain sorted. For example, when the list is [3], after inserting 1, it should become [1, 3]. It returns false if “intList” has val already and it won’t insert the duplicate. You should not use a sorting algorithm for this. bool remove(int val, int intList[], int& size) the above function will remove “val” from “intList” and decrement size. For example, when the list is [1, 3, 5], after removing 3, the list should be [1, 5]. It returns false if “intList” doesn’t have “val”
list.h
#ifndef LIST_H
#define LIST_H
bool insert(int val, int intList[], int& size)
{
if(int
}
bool remove(int val, int intList[], int& size)
{
}
void print(const int intList[], int size)
{
for(int i = 0; i < size; i++)
{
cout << intList[i];
}
}
#endif
Explanation / Answer
Please find my implementation:
Please let me know in case of any issue.
bool insert(int val, int intList[], int& size)
{
//insert your code here and modify the following line
i = 0;
while(i < size && intList[i] > val)
i++;
// insertignm at last
if(intList[i == size]){
intList[i] = val;
size++;
return true;
}
// found duplicate
if(intList[i] == val)
return false;
for(int j=size; j>i; j--)
intList[j] = intList[j-1];
intList[i] = val;
size++;
return true;
}
bool remove(int val, int intList[], int& size)
{
//insert your code here and modify the following line
int i = 0;
while(i < size && intList[i] != val)
i++;
if(i == size)
return false;
for(int j=i; j<size-1; j++)
intList[i] = intList[i+1];
size = size - 1;
return true;
}