Question: Use the attached code below. In this lab exam, you are going to implement mergeLists function. This function aims to merge 2 sorted lists. Assume

Use the attached code below. In this lab exam, you are going to implement mergeLists function. This function aims to merge 2 sorted lists. Assume that input lists to mergeLists is sorted. You are going to use input.txt as input to the program. Example: When list1->1 3 4 6 7 9 and list2->2 4 5 the output merged list will be list3->1 2 3 4 4 5 6 7 9. In this program, allocation is done in a smart fashion, and count of memory allocated is kept track. And in the implementation of the function mergeLists, you must use already allocated memory and you are not going to allocate any extra memory. Hence: list1->1 3 4 6 7 9 and list2->2 4 5 at the end of first iteration, it will be list1->3 4 6 7 9, list2->2 4 5, list3->1 You will change the pointers, and do not need to allocate extra memory. Since you are always inserting to the end of list3, you can keep track of a temporary pointer as tail to ease insert at the end of the list, and not to traverse list3 till the end at each insert. At the end of the program, ensure that Allocated memory is 0. #include static int countsAllocated = 0; struct Node { int value; struct Node* next; }; void printList(struct Node* head) { struct Node* tmp = head; while (tmp != NULL) { printf("%d ", tmp->value); tmp = tmp->next; } printf(" ");

} struct Node* allocateNode(int value) { struct Node* node = (struct Node *) malloc(sizeof(struct Node)); node->value = value; node->next = NULL; countsAllocated++; return node; } struct Node* mergeLists(struct Node* pNumbers1, struct Node* pNumbers2) { } void insert(struct Node** pHead, int value) { struct Node* newNode = allocateNode(value); newNode->next = *pHead; *pHead = newNode; } void* freeList(struct Node** pHead) { struct Node* tmp = *pHead; while (tmp != NULL) { *pHead = tmp->next; free (tmp); tmp = *pHead; countsAllocated--; } return *pHead; } int main(void) { int i, numInputs;

char* str; float average; int value; int index; int* numArray = NULL; int countOfNums1; int countOfNums2; struct Node* head1 = NULL; struct Node* head2 = NULL; struct Node* head3 = NULL; FILE* inFile = fopen("input.txt","r"); fscanf(inFile, " %d ", &numInputs); while (numInputs-- > 0) { head1 = NULL; head2 = NULL; head3 = NULL; fscanf(inFile, " %d %d ", &countOfNums1, &countOfNums2); for (i = 0; i < countOfNums1; i++) { fscanf(inFile," %d", &value); insert(&head1, value); } for (i = 0; i < countOfNums2; i++) { fscanf(inFile, " %d", &value); insert(&head2, value); } printf("List 1: "); printList(head1); printf("List 2: "); printList(head2); printf("Memory Allocated before merge : %d ", countsAllocated); head3 = mergeLists(head1, head2);

printf("List 3: "); printList(head3); freeList(&head3); printf("Memory Allocated after free : %d ", countsAllocated); } fclose(inFile);

input

3 6 3 9 7 6 4 3 1 5 4 2 2 4 3 1 8 7 5 2 4 32 11 5 4 2 78 75 65 62 61 59 57 54 52 49 47 43 42 41 39 38 36 33 26 24 22 20 19 17 14 12 11 9 6 3 2 1

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!