16-nodes.c 689 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node {
  4. int data;
  5. struct Node *next;
  6. } Node;
  7. void insert(Node **head, int data) {
  8. Node *newNode = (Node *)malloc(sizeof(Node));
  9. newNode->data = data;
  10. newNode->next = NULL;
  11. if (*head == NULL) {
  12. *head = newNode;
  13. } else {
  14. Node *temp = *head;
  15. while (temp->next != NULL) {
  16. temp = temp->next;
  17. }
  18. temp->next = newNode;
  19. }
  20. }
  21. void display(Node *head) {
  22. Node *temp = head;
  23. while (temp != NULL) {
  24. printf("%d ", temp->data);
  25. temp = temp->next;
  26. }
  27. printf("\n");
  28. }
  29. int main() {
  30. Node *head = NULL;
  31. insert(&head, 1);
  32. insert(&head, 2);
  33. insert(&head, 3);
  34. display(head);
  35. return 0;
  36. }