forked from siddhant3s/cs215
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll1.cpp
executable file
·234 lines (185 loc) · 4.61 KB
/
ll1.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
using namespace std;
class linklist
{
private:
struct node
{
int data;
node *link;
}*p;
public:
linklist();
void append( int num );
void add_as_first( int num );
int addafter( int c, int num );
int del( int num );
void display();
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(int num)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}
void linklist::add_as_first(int num)
{
node *q;
q = new node;
q->data = num;
q->link = p;
p = q;
}
int linklist::addafter( int c, int num)
{
node *q,*t;
int i;
for(i=0,q=p;i<c;i++)
{
q = q->link;
if( q == NULL )
{
cout<<"\nThere are less than "<<c<<" elements.";
return 0;
}
}
t = new node;
t->data = num;
t->link = q->link;
q->link = t;
return 1;
}
int linklist::del( int num )
{
node *q,*r;
q = p;
if( q->data == num )
{
p = q->link;
delete q;
return 1;
}
r = q;
while( q!=NULL )
{
if( q->data == num )
{
r->link = q->link;
delete q;
return 1;
}
r = q;
q = q->link;
}
cout<<"\nElement "<<num<<" not Found.";
return 0;
}
void linklist::display()
{
node *q;
cout<<endl;
for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
linklist::~linklist()
{
node *q;
if( p == NULL )
return;
while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
int main()
{ char ch;
linklist ll;
do{
//cout<<"No. of elements = "<<ll.count();
cout <<"\n\tMENU"
<<"\n1.Create a Linked List"
<<"\n2.Add Element At Last"
<<"\n3.Add Element At First"
<<"\n4.Add Element in Between"
<<"\n5.Delete a Element"
<<"\n6.Display the List"
<<"\nPress 0(zero) to exit";
cin>>ch;
switch(ch)
{
case '1': int N,V;
cout<<"\nEnter the the Number of Elements you want to start with:";
cin>>N;
cout<<"\nNow Enter the Elements(Press Enter after Each entry):";
for(int i=0;i<N;i++)
{ cin>>V;
ll.append(V);
}
cout<<"\nThe following List has been Created:\n";
ll.display();
break;
case '2':
cout<<"\nEnter the Element to be added at Last:";
cin>>V;
ll.append(V);
cout<<"\nThe Element has been Added at Last";
break;
case '3':
cout<<"\nEnter the Element to be added at First:";
cin>>V;
ll.add_as_first(V);
cout<<"\nThe Element has been Added at First";
break;
case '4': cout<<"\nEnter a Position after which you want to add(1,2,3....):";
cin>>N;
cout<<"\nEnter the Element:";
cin>>V;
if(ll.addafter(N-1,V))
cout<<"\nElement is been added after "<<N<<"th Position";
break;
case '5':
cout<<"\nEnter the Element to be Deleted:";
cin>>V;
if(ll.del(V))
cout<<"Deleted";
break;
case '6': cout<<"\nDisplaying Linked List:\n";
ll.display();
break;
case '0': return 0;
}
cout<<"\nDo You want to Continue?(Y/N):";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}