app.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* * C program to create a linked list and display the elements in the list */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main() { struct node { int num; struct node *ptr; }; typedef struct node NODE; NODE *head, *first, *temp = 0; int count = 0; int choice = 1; first = 0; while (choice) { head = (NODE *)malloc(sizeof(NODE)); printf("Enter the data item\n"); scanf("%d", &head-> num); if (first != 0) { temp->ptr = head; temp = head; } else { first = temp = head; } fflush(stdin); printf("Do you want to continue(Type 0 or 1)?\n"); scanf("%d", &choice); } temp->ptr = 0; /* reset temp to the beginning */ temp = first; printf("\n status of the linked list is\n"); while (temp != 0) { printf("%d=>", temp->num); count++; temp = temp -> ptr; } printf("NULL\n"); printf("No. of nodes in the list = %d\n", count); } |