forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_driven_prog_for_Linkedlist_in_c++.cpp
165 lines (148 loc) · 2.3 KB
/
menu_driven_prog_for_Linkedlist_in_c++.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream>
using namespace std;
class sllnode
{
public:
int info;
sllnode*next;
sllnode()
{
info=0;
next=NULL;
}
};
class sll
{
public:
sllnode*head;
sllnode*tail;
sll()
{
head=NULL;
tail=NULL;
}
public:
void addtotail(int );
void traverse();
void visit(int );
void addtohead(int);
bool search(int);
};
void sll:: addtohead(int h)
{
sllnode*n1=new sllnode();
if(head==NULL)
{
n1->info=h;
head=n1;
tail=n1;
}
else
{
n1->info=h;
n1->next=head;
head=n1;
}
}
void sll:: addtotail(int a)
{
sllnode *n1=new sllnode;
if(tail==NULL)
{
n1->info=a;
head=n1;
tail=n1;
}
else
{
n1->info=a;
tail->next=n1;
tail=n1;
}
}
bool sll:: search( int n)
{
bool value=false;
sllnode*temp=head;
while(temp!=NULL)
{
if(temp->info==n)
return true;
else
temp= temp->next;
}
return value;
}
void sll:: traverse()
{
sllnode*temp=head;
if(tail==NULL)
{
cout<<" empty ";
}
else
{
while(temp!=NULL)
{
visit(temp->info);
temp=temp->next;
}
}
}
void sll::visit(int k)
{
cout<<k<<" ";
}
int main()
{
sll a;
int choice;
cout<<"1 for eneter the element to tail "<<endl;
cout<<"2 to enter element at head"<<endl;
cout<<"3 to print "<<endl;
cout<<"4 for search"<<endl;
cout<<"5 for EXIT"<<endl;
cin>>choice;
while(choice!=5)
{
switch(choice)
{
case 1:
{ int value;
cout<<"enter the element to be added to tail"<<endl;
cin>>value;
a.addtotail(value);
break;
}
case 2:
{
int value;
cout<<"enter the element to be added to head";
cin>>value;
a.addtohead(value);
break;
}
case 3:
{
a.traverse();
break;
}
case 4:
{
int h;
cout<<"### ENTER THE ELEMENT TO BE SEARCHED ###";
cin>>h;
bool check;
check=a.search(h);
if (check==true)
cout<<"element found "<<endl;
else
cout<<"not found"<<endl;
break;
}
}
cout<<"**************enter your choice again***************";
cin>>choice;
}
return 0;
}