192k views
4 votes
Given an IntNode struct, write the operating functions for a linked list by completing the following functions.

1. IntNode* IntNode.Create (int initData); -Create a new node on the heap which stores the value initData
2. void IntNode InsertAfter (IntNode thisNode, IntNode* newNode)- Insert newNode in the linked list after thisNode
3 void IntNode PrintNodeData(IntNode* thisNode); - Print out the value stored in the node, if valid
4. IntNode* IntNode_GetNext(IntNode thisNode); - Return the address of the next node of the given "this Node"
5 int IntNode Length(IntNode* firstNode); Return the length (number of nodes) in the list
6 IntNode IntNode_GetNth (IntNode* firstNode, int n)-Return a pointer to the nth node of the list starting at firstNode.
7. void IntNode_PrintList (IntNodes firstNode)-Call IntNode.PrintNodeData() to output values of the list starting at
firstNode. Do not add extra space characters in between values
8. int IntNode_SumList (IntNodex firstNode) - Return the sum of the values of all nodes starting at firstNode.
Note: A linked list is simply a chain of IntNode structures. Refer to chapter 7 of the zybook for more information on a linked list. To store a
linked list object, we simply store a pointer to the first node in the linked list. That first node then points to the next node in the list, and so
on, until we reach a node that points to NULL instead of an additional node. A node in the list which points to NULL is the last node in the
list
Your main should read 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
With this, you should create a linked list with the specified number of nodes in the list (user value 1). The value of the first node should be user value
2. Each of the remaining nodes should store values, each incremented by the increment value from the previous value (user value 3)
Next, print out the list using the print list function you wrote. Then, print out the list starting from the second Node. If there is no second
node, print out "No second element in its place.
Next, print out the sum of the values stored in the list.
Finally, add a new node with the 4th specified user value after the node located at the specified location in the list (look at user input value

User Kojiro
by
8.4k points

1 Answer

5 votes

Final answer:

This is an example implementation of an IntNode struct and the functions required to operate on a linked list. It demonstrates how to create a new node, insert a node after a given node, print the data of a node, get the next node, find the length of the list, get the nth node, print the entire list, and calculate the sum of the values in the list. It also includes a main function that reads user input to create and manipulate a linked list.

Step-by-step explanation:

To create a linked list and perform various operations, you need to define an IntNode struct and implement the required functions. Here is an example implementation:

#include <stdio.h>
#include <stdlib.h>

typedef struct IntNode {
int data;
struct IntNode* next;
} IntNode;

IntNode* IntNode_Create(int initData) {
IntNode* newNode = (IntNode*)malloc(sizeof(IntNode));
if (newNode != NULL) {
newNode->data = initData;
newNode->next = NULL;
}
return newNode;
}

void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode) {
if (thisNode != NULL && newNode != NULL) {
newNode->next = thisNode->next;
thisNode->next = newNode;
}
}

void IntNode_PrintNodeData(IntNode* thisNode) {
if (thisNode != NULL) {
printf("%d ", thisNode->data);
}
}

IntNode* IntNode_GetNext(IntNode* thisNode) {
if (thisNode != NULL) {
return thisNode->next;
}
return NULL;
}

int IntNode_Length(IntNode* firstNode) {
int length = 0;
IntNode* currentNode = firstNode;
while (currentNode != NULL) {
length++;
currentNode = currentNode->next;
}
return length;
}

IntNode* IntNode_GetNth(IntNode* firstNode, int n) {
IntNode* currentNode = firstNode;
int count = 1;
while (currentNode != NULL && count < n) {
currentNode = currentNode->next;
count++;
}
return currentNode;
}

void IntNode_PrintList(IntNode* firstNode) {
IntNode* currentNode = firstNode;
while (currentNode != NULL) {
IntNode_PrintNodeData(currentNode);
currentNode = currentNode->next;
}
}

int IntNode_SumList(IntNode* firstNode) {
int sum = 0;
IntNode* currentNode = firstNode;
while (currentNode != NULL) {
sum += currentNode->data;
currentNode = currentNode->next;
}
return sum;
}

int main() {
int numNodes;
int firstValue;
int increment;
int newValue;
int insertPosition;

printf("Enter the number of nodes to be added: ");
scanf("%d", &numNodes);

printf("Enter the value of the first node: ");
scanf("%d", &firstValue);

printf("Enter the increment value: ");
scanf("%d", &increment);

IntNode* firstNode = IntNode_Create(firstValue);
IntNode* currentNode = firstNode;

for (int i = 1; i < numNodes; i++) {
newValue = firstValue + (increment * i);
IntNode* newNode = IntNode_Create(newValue);
IntNode_InsertAfter(currentNode, newNode);
currentNode = newNode;
}

printf("The list is: ");
IntNode_PrintList(firstNode);

printf("\\");

IntNode* secondNode = IntNode_GetNext(firstNode);
if (secondNode == NULL) {
printf("No second element in its place.");
} else {
printf("The second node is: ");
IntNode_PrintNodeData(secondNode);
}

printf("\\");

int sum = IntNode_SumList(firstNode);
printf("The sum of the values is: %d\\", sum);

printf("Enter the value of the new node: ");
scanf("%d", &newValue);

printf("Enter the position to insert the new node after: ");
scanf("%d", &insertPosition);

IntNode* insertAfterNode = IntNode_GetNth(firstNode, insertPosition);
IntNode* newNode = IntNode_Create(newValue);
IntNode_InsertAfter(insertAfterNode, newNode);

printf("The list after insertion is: ");
IntNode_PrintList(firstNode);

return 0;
}

User Elad Kalif
by
8.3k points