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;
}