Question: Please do it in C and know that the IntNode.c. and IntNode.h are read only and all changes need to be made in the main

Please do it in C and know that the IntNode.c. and IntNode.h are read only and all changes need to be made in the main part. Need it now!!!

Implement a function List_PutFirst that moves the first node in a list containing a sought after value to the front of the list.

You will need to search the list item-by-item to find the desired item. Next you need to move that given item from its current place in the list (if it is not already first) to the beginning 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_PutFirst. Neither should it change any node's dataVal field.

In effect, you will be reassigning several pointers. Consider a list where the item "sought" appears somewhere in the middle of the list (defined as IntNode * first), as shown below.

After calling, say List_PutFirst(&first, 42) (where 42 is the value sought to be put first), the various pointers will be reassigned as follows:

Your search loop must account for the need to manipulate the nextNodePtr field of the node preceding the entry containing "sought". (Alas, there is no "previous" field in our list node structure.)

Finally, your program should respond appropriately in all cases. In particular, List_PutFirst should return false if the list is empty or the item designated is not found on the list, and return true otherwise.

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

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); }

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!