Introduction:
A list is a basic data structure that stores elements in a linear order. Unlike arrays, lists can dynamically grow or shrink in size, and elements can be inserted or removed at any position. The elements in a list are stored sequentially, and each element can be accessed directly by its position or index
Some functions in List are described as bellow:
bool is_full()
: returns true if the list is full (i.e., size == MAX_SIZE), otherwise, return false.void append(element)
: Appends the element at the end of the list. If the list is full, no action is performedint front()
: return the first element of the list. If the list is empty, return -1.int back()
: return the last element of the list. If the list is empty, return -1.void insert(int index, int value)
: insert value at position index. If the list is full or the index is invalid, no action is performed.void remove(int index)
: Removes the element at position index If the index is invalid, no action is performed.void erase(int value)
: Remove the first element equal to the value. If the value is not found, no action is performed.void clear()
: Remove all the elements in the list. Make the list is empty.
static const int MAX_SIZE = 4; // Capacity of the list
int data[MAX_SIZE]; // Linearly stores data in a fixed-size
array
int size; // Current number of elements in the
array
// Default constructor initializes an empty list
List() {
size = 0;
}
Note: The capacity of the list is MAX_SIZE = 4
. If an operation cannot be performed, the list remains unchanged.
Based on the above configuration and function definitions, complete the table below
The table has been completed and is provided below for your reference
Id | Operators | data[] | size | front() | back() | is_full() |
---|---|---|---|---|---|---|
0 | List myList = List(); | [] | 0 | -1 | -1 | false |
1 | myList.append(1) | [1] | 1 | 1 | 1 | false |
2 | myList.append(3) | [1, 3] | 2 | 1 | 3 | false |
3 | myList.append(5) | [1, 3, 5] | 3 | 1 | 5 | false |
4 | myList.append(7) | [1, 3, 5, 7] | 4 | 1 | 9 | true |
5 | myList.append(9) | [1, 3, 5, 7] | 4 | 1 | 7 | true |
6 | myList.remove(2) | [1, 3, 7] | 3 | 1 | 7 | false |
7 | myList.erase(7) | [1, 3] | 2 | 1 | 3 | false |
8 | myList.erase(4) | [1, 3] | 2 | 1 | 3 | false |
9 | myList.clear() | [] | 0 | -1 | -1 | false |
Id | Operators | data[] | size | front() | back() | is_full() |
---|---|---|---|---|---|---|
0 | List myList = List(); | [] | 0 | -1 | -1 | false |
1 | myList.append(4) | |||||
2 | myList.append(9) | |||||
3 | myList.append(2) | |||||
4 | myList.append(7) | |||||
5 | myList.append(5) | |||||
6 | myList.insert(1, 8) | |||||
7 | myList.remove(3) | |||||
8 | myList.erase(7) | |||||
9 | myList.append(6) | |||||
10 | myList.insert(2, 3) | |||||
11 | myList.clear() |