-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked_list_length.cpp
More file actions
47 lines (42 loc) · 870 Bytes
/
Copy pathlinked_list_length.cpp
File metadata and controls
47 lines (42 loc) · 870 Bytes
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
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
struct Node *next;
Node(int x){
data=x;
next=NULL;
}
};
void append(struct Node** head_ref,struct Node** tail_ref,int new_data)
{
struct Node* new_node =new Node(new_data);
if(*head_ref==NULL)
*head_ref=new_node;
else
(*tail_ref)->next=new_node;
*tail_ref=new_node;
}
int Count(struct Node* head)
{
if (head == NULL)
{
return 0;
}
return(1+Count(head->next));
}
int main()
{
struct Node* head = NULL,*tail=NULL;
cout<<"Enter the number of elements"<<endl;
int n,temp;
cin>>n;
cout<<"Enter each element seperated by space"<<endl;
for(int i=1;i<=n;i++)
{
cin>>temp;
append(&head,&tail,temp);
}
cout<<endl<<"Number of nodes is : "<<Count(head);
return 0;
}