C - Linked List Implementation


C - Linked List Implementation

CODE
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int main() { struct Node *head = NULL; struct Node *currentNode; currentNode = head; for(int i = 1; i <= 10; i++) { struct Node *temp = (struct Node*)malloc(sizeof(struct Node)); temp->data = i; temp->next = NULL; if(currentNode == NULL) currentNode = head = temp; else { currentNode->next = temp; currentNode = temp; } } //Print all the nodes in the list currentNode = head; while (currentNode) { printf("%d ", currentNode->data); currentNode = currentNode->next; } return 0; //OUTPUT //1 2 3 4 5 6 7 8 9 10 }

Comments