Given an IntNode struct and the operating functions for a linked list, complete the following functions to extend the functionality of the linked list: 1. IntNode* IntNode_GetNth (IntNode* firstNode, int n)- Return a pointer to the nth node of the list starting at firstNode. 2. void IntNode_PrintList (IntNode* firstNode) - Call IntNode_PrintNodeData() to output values of the list starting at firstNode. Do not add extra space characters in between values. 3. int IntNode_SumList (IntNode* firstNode) - Return the sum of the values of all nodes starting at firstNode. Note: The code for IntNode_Create() provided here differs from the code shown in the book. The given main() performs various actions to test IntNode_GetNth(), IntNode_PrintList(), and IntNode_SumList(). main() reads 5 integers from a user: 1. The number of nodes to be added to a new list 2. The value of the first node of the list 3. An increment between the values of two consecutive nodes. 4. A value of a new node 5. The position of a node after which the new node will be added, with 1 indicating the first node Ex: If the input is: 4 2 5 99 1 the output is: 4 element list: 2 7 12 17 From second element: 7 12 17 sum: 38 New list: 2 99 7 12 17

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16SA
icon
Related questions
icon
Concept explainers
Question

Given an IntNode struct and the operating functions for a linked list, complete the following functions to extend the functionality of the linked list(this is not graded).

H
Given an IntNode struct and the operating functions for a linked list, complete the following functions to extend the functionality of the
linked list:
1. IntNode* IntNode_GetNth (IntNode* firstNode, int n)- Return a pointer to the nth node of the list starting at firstNode.
2. void IntNode_PrintList (IntNode* firstNode) - Call IntNode_PrintNodeData() to output values of the list starting at firstNode.
Do not add extra space characters in between values.
3. int IntNode_SumList (IntNode* firstNode) - Return the sum of the values of all nodes starting at firstNode.
Note: The code for IntNode_Create() provided here differs from the code shown in the book.
The given main() performs various actions to test IntNode_GetNth(), IntNode_PrintList(), and IntNode_SumList().
main() reads 5 integers from a user:
1. The number of nodes to be added to a new list
2. The value of the first node of the list
3. An increment between the values of two consecutive nodes.
4. A value of a new node
5. The position of a node after which the new node will be added, with 1 indicating the first node
Ex: If the input is:
4 2 5 99 1
the output is:
4 element list: 2 7 12 17
From second element: 7 12 17
sum: 38
New list: 2 99 7 12 17
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct IntNode_struct {
5
int dataVal;
6
7 } IntNode;
8
9 // Allocate a node for initData
10 IntNode* IntNode_Create (int initData) {
11
12
13
14
15 }
16
17 /* Insert newNode after node.
18 Before: thisNode -- next
19 After: thisNode-newNode --next
20 */
21 void IntNode_InsertAfter (IntNode* thisNode, IntNode* newNode) {
22 IntNode* tmpNext = NULL;
23
24 tmpNext = thisNode->nextNodePtr;
25
26
27 }
28
37 }
struct IntNode_struct* nextNodePtr;
38
IntNode* newNode = (IntNode*) malloc(sizeof(IntNode));
newNode->dataVal = initData;
29 // Print dataVal
30 void IntNode_PrintNodeData (IntNode* thisNode) {
printf("%d ", thisNode->dataVal);
31
newNode->nextNodePtr = NULL;
return newNode;
32}
33
34 // Grab Location pointed by nextNodePtr
35 IntNode* IntNode_GetNext (IntNode* thisNode) {
36
40
thisNode->nextNodePtr = newNode;
newNode->nextNodePtr = tmpNext;
46
47
39 /* ******** New functions ********/
48
49
50
51 }
return thisNode->nextNodePtr;
41 // Return the Length of the list
42 int IntNode_Length (IntNode* firstNode) {
43
44
45
int length = 0;
IntNode* currentNode = firstNode;
while (currentNode != NULL) {
}
++length;
currentNode = currentNode->nextNodePtr;
return length;
// Remember next
// this
// this
--
new -- ?
new -- next
Transcribed Image Text:H Given an IntNode struct and the operating functions for a linked list, complete the following functions to extend the functionality of the linked list: 1. IntNode* IntNode_GetNth (IntNode* firstNode, int n)- Return a pointer to the nth node of the list starting at firstNode. 2. void IntNode_PrintList (IntNode* firstNode) - Call IntNode_PrintNodeData() to output values of the list starting at firstNode. Do not add extra space characters in between values. 3. int IntNode_SumList (IntNode* firstNode) - Return the sum of the values of all nodes starting at firstNode. Note: The code for IntNode_Create() provided here differs from the code shown in the book. The given main() performs various actions to test IntNode_GetNth(), IntNode_PrintList(), and IntNode_SumList(). main() reads 5 integers from a user: 1. The number of nodes to be added to a new list 2. The value of the first node of the list 3. An increment between the values of two consecutive nodes. 4. A value of a new node 5. The position of a node after which the new node will be added, with 1 indicating the first node Ex: If the input is: 4 2 5 99 1 the output is: 4 element list: 2 7 12 17 From second element: 7 12 17 sum: 38 New list: 2 99 7 12 17 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct IntNode_struct { 5 int dataVal; 6 7 } IntNode; 8 9 // Allocate a node for initData 10 IntNode* IntNode_Create (int initData) { 11 12 13 14 15 } 16 17 /* Insert newNode after node. 18 Before: thisNode -- next 19 After: thisNode-newNode --next 20 */ 21 void IntNode_InsertAfter (IntNode* thisNode, IntNode* newNode) { 22 IntNode* tmpNext = NULL; 23 24 tmpNext = thisNode->nextNodePtr; 25 26 27 } 28 37 } struct IntNode_struct* nextNodePtr; 38 IntNode* newNode = (IntNode*) malloc(sizeof(IntNode)); newNode->dataVal = initData; 29 // Print dataVal 30 void IntNode_PrintNodeData (IntNode* thisNode) { printf("%d ", thisNode->dataVal); 31 newNode->nextNodePtr = NULL; return newNode; 32} 33 34 // Grab Location pointed by nextNodePtr 35 IntNode* IntNode_GetNext (IntNode* thisNode) { 36 40 thisNode->nextNodePtr = newNode; newNode->nextNodePtr = tmpNext; 46 47 39 /* ******** New functions ********/ 48 49 50 51 } return thisNode->nextNodePtr; 41 // Return the Length of the list 42 int IntNode_Length (IntNode* firstNode) { 43 44 45 int length = 0; IntNode* currentNode = firstNode; while (currentNode != NULL) { } ++length; currentNode = currentNode->nextNodePtr; return length; // Remember next // this // this -- new -- ? new -- next
Expert Solution
steps

Step by step

Solved in 3 steps with 5 images

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning