-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.6LinkedLists_COUNTnodes
64 lines (63 loc) · 1009 Bytes
/
1.6LinkedLists_COUNTnodes
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
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
int count(struct node *);
/*To count no. of nodes*/
void append(struct node **,int );
void main()
{ clrscr();
struct node *n;
n=NULL;
int i=0,a[5]={1,2,3,4,5};
while(i<5)
{
append(&n,a[i]);
i++;
}
int c = count(n);
printf("\nNo. of nodes = %d\n",c);
while(n!=NULL)
{
printf("%d ",(n->data));
n=n->link;
}
free(n);
getch();
}
int count(struct node *a)
{
int i=0;
while(a!=NULL)
{
a=a->link;
i++;
}
return i;
}
/*any element we want to insert to linked lists will append */
void append(struct node **a,int num)
{
struct node *temp,*p;
if(*a==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*a=temp;
}
else
{
temp=*a; // *a passes the address of first node
while(temp->link!=NULL)
temp=temp->link;
p=(struct node*)malloc(sizeof(struct node));
p->data=num;
p->link=NULL;
temp->link=p;
}
}