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
Write a procedure to determine the number of items (or more precisely, the number of nodes) on a linked list.
While you once would have written this recursively in Scheme, you should NOT use recursion to implement this procedure. (You can write it with a straightforward for loop.)
int List_Length(IntNode* first)
Unit tests will be used to assess your procedure, so you may use main to test your procedure however you like.
Note that the linked list functions have been factored into the IntNode.h header with implementations in IntNode.c.
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
// 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
Get step-by-step solutions from verified subject matter experts
