Question: In C please and the IntNode files pasted down below are read only so the program can be written only in the main Write a

In C please and the IntNode files pasted down below are read only so the program can be written only in the main

Write a function List_TransposeFirstTwo that transposes the (first) two nodes in a list.

That is, you must rearrange the pointers so that the node containing the first item becomes second in the list, and the node containing the second item becomes the head of the list.

Because manipulation of lists is most efficient if only pointers are manipulated, your program should neither create new nodes nor dispose of existing ones. In particular, your program must not use either the malloc or free functions during the processing of List_TransposeFirstTwo. Neither should it change any node's dataVal field.

If you had a variable IntNode* list referring to the list (or the head/first node), the list itself may look like this before the call:

And the list node pointers should be reassigned so that the list appears as follows after the call List_TransposeFirstTwo(&list);

The function should have the following signature

// Transpose first two nodes of the given list // Precondition: The list contains at least two nodes void List_TransposeFirstTwo(IntNode ** pList) 

Your procedure does not need to verify the precondition.

Your procedure will be assessed by unit tests, therefore no input is required and you may use main however you wish for testing.

Hint: Consider the pictures above. How many pointers need to change? Your solution should make that many assignments.

Note that the linked list functions have been factored into the IntNode.h header with implementations in IntNode.c.

IntNode.c. :

#include #include #include #include "IntNode.h"

// Constructor void IntNode_Create (IntNode* thisNode, int dataInit, IntNode* nextLoc) { thisNode->dataVal = dataInit; thisNode->nextNodePtr = nextLoc; }

/* Insert newNode after node. Before: thisNode -- next After: thisNode -- newNode -- next */ void IntNode_InsertAfter (IntNode* thisNode, IntNode* newNode) { IntNode* tmpNext = NULL; tmpNext = thisNode->nextNodePtr; // Remember next thisNode->nextNodePtr = newNode; // this -- new -- ? newNode->nextNodePtr = tmpNext; // this -- new -- next }

// Print dataVal void IntNode_PrintNodeData(IntNode* thisNode) { printf("%d ", thisNode->dataVal); }

// Grab location pointed by nextNodePtr IntNode* IntNode_GetNext(IntNode* thisNode) { return thisNode->nextNodePtr; }

// Insert a new node onto a list // Returns success indicator bool List_Insert (IntNode** pList, int value) { IntNode* newNode = (IntNode*)malloc(sizeof(IntNode)); // Create a new node if (newNode==NULL) return false; newNode->dataVal = value; newNode->nextNodePtr = *pList; // Dereference to get the address of the node // Dereference in assignment to set the reference IntNode* to point to newNode *pList = newNode; return true; }

// Initialize a list void List_Initialize (IntNode** pList) { *pList = NULL; // Set the referent to NULL }

// Print a list void List_Print (IntNode* node) { for (IntNode* curr = node ; curr != NULL ; curr = curr->nextNodePtr ) printf("%d ", curr->dataVal); }

IntNode.h :

#ifndef __INTNODE_H__ #define __INTNODE_H__

#include

typedef struct IntNode_struct { int dataVal; struct IntNode_struct* nextNodePtr; } IntNode;

// Constructor void IntNode_Create(IntNode* thisNode, int dataInit, IntNode* nextLoc);

/* Insert newNode after node. Before: thisNode -- next After: thisNode -- newNode -- next */ void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode);

// Print dataVal void IntNode_PrintNodeData(IntNode* thisNode);

// Grab location pointed by nextNodePtr IntNode* IntNode_GetNext(IntNode* thisNode);

// Insert a new node onto a list // Returns success indicator bool List_Insert (IntNode** pList, int value);

// Initialize a list void List_Initialize (IntNode** pList);

// Print a list void List_Print (IntNode* node);

#endif

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!