Z University of Guelph C x CIS 1500 F16 Meet with x G chegg

Z University of Guelph C x CIS 1500 F16: Meet with x G chegg Google Search x c https:// 11.9 A first linked list Normally, a linked list would be implemented as an ADT using a set interface functions and struct type declaration, such as \"intList\". Data members of that struct might include a pointer to the list head, the list size, and a pointer to the list tail (the last node in the list). Supporting functions might include InsertAfter (insert a new node after the given node), PushBack (insert a new node after the last node), PushFront (insert a new node at the front of the list, just after the head), D Node (deletes the node from the list), etc 11.9.1: Linked list negative values counting Activity Assign negativeCntr with the number of negative values in the linked list. 1 test passed All tests passed Int Node InsertAfter (lastobj, currobj); Append curr lastobj currobj; Curr is the new last item 54 Currobj head obj; Print the list 55 while (currobj NULL) printf d, IntNode GetDataVal (currobj)); currobj IntNode GetNext(currobj); 59 printf (\"In\"); currob headobj; Count number of negative numbers 62 While (cur robj NULL) Your solution goes here currobj IntNode GetNext (currobj); 68 print (\"Number of negatives: \'Pod\ \", negativeCntr); return 0; 100% View your last submission V Participation activities Feedback? 0% 4) ENG 11:15 AM Ask me anything 2016-11-23

Solution

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
// Linked List contains integer type data value and link to next node
typedef struct IntNode_struct {
int dataVal;
struct IntNode_struct* nextNodePtr;
} IntNode;
// Constructor will initialize new node with value dataInit and nextLocation
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
}
// Grab location pointed by nextNodePtr
IntNode* IntNode_GetNext(IntNode* thisNode) {
return thisNode->nextNodePtr;
}
// Get Data Value for current node
int IntNode_GetDataVal(IntNode* thisNode) {
return thisNode->dataVal;
}
int main(void) {
IntNode* headObj = NULL; // Create intNode objects
IntNode* currObj = NULL;
IntNode* lastObj = NULL;
int i = 0; // Loop index
int negativeCntr = 0; // negative counter - counts negative value
headObj = (IntNode*)malloc(sizeof(IntNode)); // Front of nodes list
IntNode_Create(headObj, -1, NULL);
lastObj = headObj;
for (i = 0; i < 10; ++i) { // Append 10 rand nums
currObj = (IntNode*)malloc(sizeof(IntNode));
IntNode_Create(currObj, (rand() % 21) - 10, NULL);
IntNode_InsertAfter(lastObj, currObj); // Append curr
lastObj = currObj; // Curr is the new last item
}
currObj = headObj; // Print the list
while (currObj != NULL) {
printf(\"%d, \", IntNode_GetDataVal(currObj));
currObj = IntNode_GetNext(currObj);
}
printf(\"n\");
currObj = headObj; // Count number of negative numbers
while (currObj != NULL) {
/* Check currObj node value is negative IntNode_GetDataVal method returns value of node */
if(IntNode_GetDataVal(currObj) < 0){
    negativeCntr++; // Increement counter negativeCntr when currObj value is negative
}
currObj = IntNode_GetNext(currObj);
}
printf(\"Number of negatives: %dn\", negativeCntr);
return 0;
}

 Z University of Guelph C x CIS 1500 F16: Meet with x G chegg Google Search x c https:// 11.9 A first linked list Normally, a linked list would be implemented a
 Z University of Guelph C x CIS 1500 F16: Meet with x G chegg Google Search x c https:// 11.9 A first linked list Normally, a linked list would be implemented a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site