Untitled
unknown
c_cpp
4 years ago
1.2 kB
11
Indexable
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void insert_node (struct Node **head, int nodeValue); void print_list (struct Node *head); int main(int argc, char *argv[]) { struct Node* headNode; headNode = NULL; /* Initialize our first node pointer to be NULL. */ size_t listSize, i; do { printf("How many numbers would you like to input?\n"); } while(1 != scanf("%zu", &listSize)); for (i = 0; i < listSize; i++) { int numToAdd; do { printf("Enter a number:\n"); } while (1 != scanf("%d", &numToAdd)); insert_node (&headNode, numToAdd); printf("Current list after your inserted node: \n"); print_list(headNode); } return 0; } void print_list (struct Node *head) { struct Node* currentNode = head; /* Iterate through each link. */ while (currentNode != NULL) { printf("Value: %d\n", currentNode->data); currentNode = currentNode -> next; } } void insert_node (struct Node **head, int nodeValue) { struct Node *currentNode = malloc(sizeof *currentNode); currentNode->data = nodeValue; currentNode->next = (*head); }
Editor is loading...