-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlistCount.c
73 lines (60 loc) · 1.15 KB
/
linkedlistCount.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
int data;
struct node *next;
};
void append ( struct node **q, int num ) {
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=num;
new_node->next=NULL;
if(*q==NULL)
{
*q=new_node;
}
else{
struct node*temp=*q;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new_node;
}
}
void display ( struct node *q ) {
struct node*p=q;
printf("\nThe elements in the linked list are ");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL\n");
}
int count ( struct node * q ) {
struct node *temp=q;
int count=0,num=1;
while(temp->next!=NULL)
{
temp=temp->next;
count++;
num++;
}
printf("The number of elements in the linked list is %d",num);
}
int main() {
struct node *q=NULL;
int n,data;
printf("Enter the number of nodes:");
scanf("%d",&n);
while(n--)
{
scanf("%d",&data);
append(&q,data);
}
display(q);
count(q);
return 0;
}