Question
In C please Write a function List_DeleteFirst that removes the first node containing an item from a list. In addition to rearranging the pointers so
In C please
Write a function List_DeleteFirst that removes the first node containing an item from a list.
In addition to rearranging the pointers so that the node containing the first item is no longer in the chain, you must also remember to de-allocate (aka "free") that node in question.
The function should have the following signature
// Delete the first instance of a value from a list (if present) // Returns whether the item was found (and thus deleted) bool List_DeleteFirst(IntNode ** pList, int value)
Your procedure will be assessed by unit tests, therefore no input is required and you may use main however you wish for testing.
Note that the other linked list functions have been factored into the IntNode.h header with implementations in IntNode.c. Your function will go in main.c.
IntNode.c:
#include
// 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started