Your assignment for next week is to use the solution posted from the previous pr
ID: 3889091 • Letter: Y
Question
Your assignment for next week is to use the solution posted from the previous pre-lab and add the following functions:
int removeItem(int index, List userList) – This function removes the item at location index from userList. If there isn’t an item at that location the function just returns an error code. Note that you are free to decide what error code is returned but you must provide appropriate documentation (comments) for the user.
List appendList(List uList1, List uList2) – This function creates a new list consisting of the items on list uList1 followed by the items on list uList2. Note that this function can’t inform the user if malloc fails, so your documentation needs to warn about that.
List mergeList(List uList1, List uList2) – This function creates a new list consisting of the items on list uList1 followed by the items on list uList2 but with duplicates removed. Note that this function can’t inform the user if malloc fails, so your documentation needs to warn about that
(PREVIOUS WEEK SOLUTION)
int main (void)
{
List myList;
createList(&myList, 100);
addItem (0.13, &myList);
printf(“Item zero: %f ”, getItem(0, mylist));
printf(“List size: %d ”, sizeOfList(myList));
deleteList(&myList);
}
1. int createList(List *, int) – This function creates and initializes a List variable. The first argument takes the address of a List variable so that its element values can be initialized by the function. The second argument is an integer giving the maximum possible size of the list.
int createList(List *listPtr, int listSize){
listPtr->maxSizeOfList = listSize;
listPtr->tail = 0;
listPtr->array = malloc(sizeof(float)*listSize);
if (listPtr->array == NULL)return 0;
return 1;
}
2.int addItem(float, List *) – This function takes the first argument and makes it the last item, i.e., the value at the end of the List specified by the second argument. Return 0 if there is an error, else 1.
int addItem(float number, List *listPtr){
if (listPtr->tail == listPtr->maxSizeOfList)return(0);
listPtr->array[listPtr->tail] = number;
listPtr->tail++;
return 1;
}
3. float getItem(int, List) – This function returns the value located at the index given by the first argument.
float getItem(int index, List userList){
return userList.array[index];
}
4. int sizeOfList(List) – This function returns the number of items on the list.
int sizeOfList(List userList){
return userList.tail;
}
5. void deleteList(List *) – This function deletes the memory allocated for the array.
void deleteList(List *userList)
{ userList->tail = 0;
free (userList->array);
userList->array = NULL;
}
Explanation / Answer
Code:
#include<iostream>
using namespace std;
int createList(List *listPtr, int listSize){
listPtr->maxSizeOfList = listSize;
listPtr->tail = 0;
listPtr->array = malloc(sizeof(float)*listSize);
if (listPtr->array == NULL)return 0;
return 1;
}
int addItem(float number, List *listPtr){
if (listPtr->tail == listPtr->maxSizeOfList)return(0);
listPtr->array[listPtr->tail] = number;
listPtr->tail++;
return 1;
}
float getItem(int index, List userList){
return userList.array[index];
}
int sizeOfList(List userList){
return userList.tail;
}
void deleteList(List *userList)
{ userList->tail = 0;
free (userList->array);
userList->array = NULL;
}
int main (void)
{
List myList;
createList(&myList, 100);
addItem (0.13, &myList);
printf(“Item zero: %f ”, getItem(0, mylist));
printf(“List size: %d ”, sizeOfList(myList));
deleteList(&myList);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.