forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountnode operation.cpp
83 lines (77 loc) · 1.08 KB
/
countnode operation.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
78
79
80
81
82
83
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
void append(struct node **,int);
void display(struct node*);
int main()
{
struct node *start=NULL;
int choice,n;
do
{
cout<<"\n slect an operation:";
cout<<"\n1.Append a node \n2. display \n3.countnodes\n4. quit";
cout<<"enetr your choice";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the value to be added";
cin>>n;
append(&start,n);
break;
case 2:
display(start);
break;
case 3:
cout<<"Thank you for using the program";
break;
default:
cout<<"Wrong choice! Try again";
}
}
while(choice!=3);
return 0;
}
void append(struct node **ps,int x)
{
struct node *p,*temp;
p=(struct node *)malloc(sizeof(struct node));
if (p==NULL)
{
cout<<" Cannot add new node";
return;
}
p->data=x;
p->next=NULL;
if (*ps==NULL)
{
*ps=p;
cout<<"\n Node sucessfully added";
return;
}
temp=*ps;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
cout<<"\n Node is succesfully added";
}
void display(struct node *p)
{
if(p==NULL)
{
cout<<"list is emepty";
return;
}
while(p!=NULL)
{
cout<<"\n "<<p->data;
p=p->next;
}
}