-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreatenCountnode.c
52 lines (47 loc) · 1.13 KB
/
createnCountnode.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
// Program for creation of link list and display the number of nodes created
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void insert(struct node **head, int val) {
struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void displayCount(struct node *head) {
int count = 0;
struct node *temp = head;
printf("\nThe linked list is: ");
while (temp != NULL) {
printf("%d | ", temp->data);
temp = temp->next;
count++;
}
printf("\nThe number of nodes created is %d\n", count);
}
int main() {
int val;
struct node *head = NULL;
printf("Enter values for the list (enter -1 to finish):\n");
while (1) {
scanf("%d", &val);
if (val == -1) {
break;
}
insert(&head, val);
}
displayCount(head);
return 0;
}