Question: typedef struct Node _ s Node; struct Node _ s { void * data; int next; } ; enum ListOutOfBounds { LIST _ OOB _

typedef struct Node_s Node;
struct Node_s {
void* data;
int next;
};
enum ListOutOfBounds {
LIST_OOB_START,
LIST_OOB_END
};
#define LIST_MAX_NUM_HEADS 2
#define LIST_MAX_NUM_NODES 2
typedef struct List_s List;
struct List_s{
Node nodes[LIST_MAX_NUM_NODES];
int size;
int head;
int available;
int current;
};
List* list_create(){
static List lists[LIST_MAX_NUM_HEADS];
static bool initialized = false;
if(!initialized){
for(int i =0; i < LIST_MAX_NUM_HEADS; i++){
lists[i].head =-1;
lists[i].size =0;
lists[i].available =0;
}
initialized = true;
}
for (int i =0; i < LIST_MAX_NUM_HEADS; ++i){
if (lists[i].head ==-1){
return &lists[i];
}
}
return NULL;
}
int List_count(List* pList){
// Ensure pList is not NULL (assert is optional)
assert(pList != NULL);
return pList->size;
}
void* List_first(List* pList){
if (pList == NULL || pList->head ==-1){
pList->current =-1;
return NULL;
}
pList->current = pList->head;
return pList->nodes[pList->head].data;
}
void* List_next(List* pList){
assert(pList != NULL);
if (pList->current ==-1){
return NULL; // No current item
}
int currentNodeIndex = pList->current;
if (pList->nodes[currentNodeIndex].next ==-1){
// If current item is already at the end of the list, move beyond the end
pList->current =-1;
return NULL;
}
pList->current = pList->nodes[currentNodeIndex].next;
return pList->nodes[pList->current].data;
}
void* List_prev(List* pList){
assert(pList != NULL);
if (pList->current ==-1){
return NULL; // No current item
}
int prevNodeIndex = pList->head;
int currentNodeIndex = pList->current;
if (currentNodeIndex == pList->head){
// If current item is at the start of the list, move to before the start
pList->current =-1;
return NULL;
}
while (pList->nodes[prevNodeIndex].next != currentNodeIndex){
prevNodeIndex = pList->nodes[prevNodeIndex].next;
}
pList->current = prevNodeIndex;
return pList->nodes[prevNodeIndex].data;
}
void* List_curr(List* pList){
assert(pList != NULL);
if (pList->current ==-1){
return NULL; // No current item
}
return pList->nodes[pList->current].data;
}
int List_append(List* pList, void* pItem){
assert(pList != NULL);
if (pList->size >= LIST_MAX_NUM_NODES){
return LIST_FAIL; // List is full
}
int newNodeIndex = pList->available;
pList->available = pList->nodes[pList->available].next;
pList->nodes[newNodeIndex].data = pItem;
pList->nodes[newNodeIndex].next =-1; // No next node, as it is the last node
if (pList->size ==0){
pList->head = newNodeIndex;
} else {
int lastNodeIndex = pList->head;
while (pList->nodes[lastNodeIndex].next !=-1){
lastNodeIndex = pList->nodes[lastNodeIndex].next;
}
pList->nodes[lastNodeIndex].next = newNodeIndex;
}
pList->size++;
return LIST_SUCCESS;
}
void List_concat(List* pList1, List* pList2){
assert(pList1!= NULL && pList2!= NULL);
if (pList2->size ==0){
return; // Nothing to concatenate
}
if (pList1->size ==0){
// If the first list is empty, simply copy the second list
*pList1=*pList2;
}
else {
int lastNodeIndex = pList1->head;
while (pList1->nodes[lastNodeIndex].next !=-1){
lastNodeIndex = pList1->nodes[lastNodeIndex].next;
}
pList1->nodes[lastNodeIndex].next = pList2->head;
}
// Update the size of the first list
pList1->size += pList2->size;
// Reset the second list
pList2->head =-1;
pList2->size =0;
pList2->available =0;
pList2->current =-1;
}
void printList(List* pList){
assert(pList != NULL);
printf("List: ");
int currentNodeIndex = pList->head;
while (currentNodeIndex !=-1){
printf("%d ",*((int*)pList->nodes[currentNodeIndex].data));
currentNodeIndex = pList->nodes[currentNodeIndex].next;
}
printf("
");
}
int main(){
List* myList = list_create();
List* myList2= list_create();
int items[]={10,20,30,40,50,60,70,80,90};
int numItems = sizeof(items)/ sizeof(items[0]);
// Append items in a loop
for (int i =0; i < numItems; ++i){
if (List_append(. fix this code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!