We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 41a16db + 4588157 commit 3fcce41Copy full SHA for 3fcce41
data_structures/C++/CountNodes.cpp
@@ -0,0 +1,41 @@
1
+#include <iostream>
2
+using namespace std;
3
+
4
+struct Node{
5
+ int data;
6
+ struct Node *next;
7
8
+ Node(int x){
9
+ data=x;
10
+ next=NULL;
11
+ }
12
+};
13
14
+int CountElements(Node *head){
15
16
+ int count=0;
17
18
+ struct Node *curr= head;
19
+ while(curr!=NULL){
20
+ curr=curr->next;
21
+ count++;
22
23
+ return count;
24
+}
25
26
+int main()
27
+{
28
+ int data,n;
29
+ cin>>n;
30
+ cin>>data;
31
+ struct Node *head= new Node(data);
32
+ struct Node *tail= head;
33
+ for(int i=0;i<n-1;i++){
34
35
+ tail->next=new Node(data);
36
+ tail=tail->next;
37
38
+ cout<<CountElements(head)<<endl;
39
+ return 0;
40
41
0 commit comments