SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
IN C CODE
Given an IntNode struct and the operating functions for a linked list, complete the following
functions to extend the functionality of the linked list:
IntNode* IntNode_GetNth(IntNode* firstNode, int n)- Return a pointer to the nth node of the list
starting at firstNode.
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.
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:
The number of nodes to be added to a new list
The value of the first node of the list
An increment between the values of two consecutive nodes.
A value of a new node
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:
GIVEN CODE TO USE.
#include
#include
typedef struct IntNode_struct {
int dataVal;
struct IntNode_struct* nextNodePtr;
} IntNode;
// Allocate a node for initData
IntNode* IntNode_Create (int initData) {
IntNode* newNode = (IntNode*)malloc(sizeof(IntNode));
newNode->dataVal = initData;
newNode->nextNodePtr = NULL;
return newNode;
}
/* 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;
}
/* ******** New functions ********/
// Return the length of the list
int IntNode_Length(IntNode* firstNode) {
int length = 0;
IntNode* currentNode = firstNode;
while(currentNode != NULL) {
++length;
currentNode = currentNode->nextNodePtr;
}
return length;
}
// Return the Nth element of the list. First node is 1.
IntNode* IntNode_GetNth(IntNode* firstNode, int pos) {
/* Type your code here. */
}
// Print the entire list starting at firstNode
void IntNode_PrintList(IntNode* firstNode) {
/* Type your code here. */
}
// Sum the numbers in the list
int IntNode_SumList(IntNode* firstNode) {
/* Type your code here. */
}
int main(void) {
IntNode* firstNode = NULL;
IntNode* lastNode = NULL;
IntNode* newNode = NULL;
IntNode* curNode = NULL;
int listSize; // how many integers to read
int increment; // increment between consecutive node values
int firstNum; // first node value
// New node for newNum will be inserted after position newPos (first node = 1)
int newPos;
int newNum;
int i;
// Input:
scanf("%d %d %d%d %d", &listSize, &firstNum, &increment, &newNum, &newPos);
// Create the list
firstNode = IntNode_Create(firstNum);
// Create node for first value
// New list has just one node, so lastNode is same as firstNode
lastNode = firstNode;
// Add nodes to the list
for (i = 1; i < listSize; ++i) {
// Create a new node for the next nmber
newNode = IntNode_Create((increment * i) + firstNum);
// Add the node to the end of the list
IntNode_InsertAfter(lastNode, newNode);
lastNode = newNode;
}
// Print the list
printf("%d element list: ", IntNode_Length(firstNode));
IntNode_PrintList(firstNode);
printf("n");
// If list length > 1 print the list starting at the second element
printf("From second element: ");
if (IntNode_Length(firstNode) <=1) {
printf("No second elementn");
}
else {
IntNode_PrintList(IntNode_GetNext(firstNode));
printf("n");
}
// Print the sum
printf("sum: %dn", IntNode_SumList(firstNode));
// Add a node after node newPos
newNode = IntNode_Create(newNum);
curNode = IntNode_GetNth(firstNode, newPos);
IntNode_InsertAfter(curNode, newNode);
printf("New list: ");
IntNode_PrintList(firstNode);
printf("n");
return 0;
}

Más contenido relacionado

Similar a IN C CODEGiven an IntNode struct and the operating functions for a.pdf

#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docxajoy21
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfformicreation
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
Provide copy constructor- destructor- and assignment operator for the.docx
Provide copy constructor- destructor- and assignment operator for the.docxProvide copy constructor- destructor- and assignment operator for the.docx
Provide copy constructor- destructor- and assignment operator for the.docxtodd921
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfIn C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfstopgolook
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
Take the following C code. (  see  file fib.c )1.)Compile .docx
Take the following C code. (  see  file fib.c )1.)Compile .docxTake the following C code. (  see  file fib.c )1.)Compile .docx
Take the following C code. (  see  file fib.c )1.)Compile .docxSANSKAR20
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxPhil4IDBrownh
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdfaathiauto
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdfaathmaproducts
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdfarihantelehyb
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfarjunstores123
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfEdwardw5nSlaterl
 

Similar a IN C CODEGiven an IntNode struct and the operating functions for a.pdf (20)

#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Provide copy constructor- destructor- and assignment operator for the.docx
Provide copy constructor- destructor- and assignment operator for the.docxProvide copy constructor- destructor- and assignment operator for the.docx
Provide copy constructor- destructor- and assignment operator for the.docx
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfIn C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Take the following C code. (  see  file fib.c )1.)Compile .docx
Take the following C code. (  see  file fib.c )1.)Compile .docxTake the following C code. (  see  file fib.c )1.)Compile .docx
Take the following C code. (  see  file fib.c )1.)Compile .docx
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
 

Último

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 

Último (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

IN C CODEGiven an IntNode struct and the operating functions for a.pdf

  • 1. IN C CODE Given an IntNode struct and the operating functions for a linked list, complete the following functions to extend the functionality of the linked list: IntNode* IntNode_GetNth(IntNode* firstNode, int n)- Return a pointer to the nth node of the list starting at firstNode. 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. 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: The number of nodes to be added to a new list The value of the first node of the list An increment between the values of two consecutive nodes. A value of a new node 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: GIVEN CODE TO USE. #include #include typedef struct IntNode_struct { int dataVal; struct IntNode_struct* nextNodePtr; } IntNode; // Allocate a node for initData IntNode* IntNode_Create (int initData) { IntNode* newNode = (IntNode*)malloc(sizeof(IntNode)); newNode->dataVal = initData; newNode->nextNodePtr = NULL;
  • 2. return newNode; } /* 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; } /* ******** New functions ********/ // Return the length of the list int IntNode_Length(IntNode* firstNode) { int length = 0; IntNode* currentNode = firstNode; while(currentNode != NULL) { ++length; currentNode = currentNode->nextNodePtr; } return length; } // Return the Nth element of the list. First node is 1. IntNode* IntNode_GetNth(IntNode* firstNode, int pos) { /* Type your code here. */ } // Print the entire list starting at firstNode
  • 3. void IntNode_PrintList(IntNode* firstNode) { /* Type your code here. */ } // Sum the numbers in the list int IntNode_SumList(IntNode* firstNode) { /* Type your code here. */ } int main(void) { IntNode* firstNode = NULL; IntNode* lastNode = NULL; IntNode* newNode = NULL; IntNode* curNode = NULL; int listSize; // how many integers to read int increment; // increment between consecutive node values int firstNum; // first node value // New node for newNum will be inserted after position newPos (first node = 1) int newPos; int newNum; int i; // Input: scanf("%d %d %d%d %d", &listSize, &firstNum, &increment, &newNum, &newPos); // Create the list firstNode = IntNode_Create(firstNum); // Create node for first value // New list has just one node, so lastNode is same as firstNode lastNode = firstNode; // Add nodes to the list for (i = 1; i < listSize; ++i) { // Create a new node for the next nmber newNode = IntNode_Create((increment * i) + firstNum); // Add the node to the end of the list IntNode_InsertAfter(lastNode, newNode); lastNode = newNode; } // Print the list printf("%d element list: ", IntNode_Length(firstNode));
  • 4. IntNode_PrintList(firstNode); printf("n"); // If list length > 1 print the list starting at the second element printf("From second element: "); if (IntNode_Length(firstNode) <=1) { printf("No second elementn"); } else { IntNode_PrintList(IntNode_GetNext(firstNode)); printf("n"); } // Print the sum printf("sum: %dn", IntNode_SumList(firstNode)); // Add a node after node newPos newNode = IntNode_Create(newNum); curNode = IntNode_GetNth(firstNode, newPos); IntNode_InsertAfter(curNode, newNode); printf("New list: "); IntNode_PrintList(firstNode); printf("n"); return 0; }