forked from xanxit/CPP-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count the nodes of a linked list.cpp
77 lines (75 loc) · 1.07 KB
/
count the nodes of a linked list.cpp
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
74
75
76
77
#include <iostream>
using namespace std;
class node
{
public:
int data;
node*next;
};
//node*first=NULL;
void insert(node*,int,int);
void disp(node*);
int main()
{
/*int i,x,n;
cout<<"Enter no. of nodes ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter data ";
cin>>x;
insert(first,i,x);
}*/
node*head=NULL;
node*second=NULL;
node*third=NULL;
node*last=NULL;
head=new node;
second = new node;
third=new node;
last=new node;
head->data=1;
head->next=second;
second->data=0;
second->next=third;
third->data=9;
third->next=last;
last->data=6;
last->next=NULL;
disp(head);
return 0;
}
/*void insert(node*p,int pos,int val)
{
node*t=NULL;
t=new node;
if(pos==0)
{
t->data=val;
t->next=first;
first=t;
}
else
{
p=first;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
if(p!=0)
{
t->data=val;
t->next=p->next;
p->next=t;
}
}
}*/
void disp(node*p)
{int count=0;
while(p!=NULL)
{
count++;
p=p->next;
}
cout<<"The number of nodes created : "<<count ;
}