-
Notifications
You must be signed in to change notification settings - Fork 28
/
DLinkList.cpp
210 lines (194 loc) · 3.87 KB
/
DLinkList.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
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
struct node *prev;
int data;
struct node *next;
};
class DLL
{
struct node *start;
public:
DLL () {start=NULL;};
void add(int);
void display(void);
int counts(void);
int inserts(int,int);
int deletes(int);
};
int DLL::deletes(int pos)
{
int result=0;
if(pos > 0 && pos<=counts())
{
struct node*q=start;
if(pos==1)
{
start=start->next;
delete q;
}
else
{
for(int i=0;i<pos-2;i++)
q=q->next;
struct node *temp=q->next;
q->next=temp->next;
delete temp;
}
result=1;
}
return result;
}
int DLL::inserts(int data,int pos)
{
int result=0;
if(pos>0 || pos<=counts()+1)
{
struct node *q=start,*l;
if(pos==1)
{
start=new(struct node);
start->prev=NULL;
start->next=q;
start->data=data;
}
else
{
for(int i=0;i<pos-2;i++)
q=q->next;
l=q->next;
q->next=new(struct node);
q->next->prev=q;
q->next->next=l;
q->next->data=data;
}
result=1;
}
return result;
}
int DLL::counts(void)
{
int counter=0;
if(start!=NULL)
{
struct node*q=start;
while(q!=NULL)
{
counter++;
q=q->next;
}
}
return counter;
}
void DLL::display(void)
{
if(start==NULL)
cout<<"\nNo list exists";
else
{
struct node *q=start;
while(q!=NULL)
{
cout<<q->data<<" ";
q=q->next;
}
}
}
void DLL::add(int data)
{
if(start==NULL)
{
start = new(struct node);
start->prev=start->next=NULL;
start->data=data;
}
else
{
struct node*q=start;
while(q->next!=NULL)
q=q->next;
q->next = new(struct node);
q->next->prev = q;
q->next->next = NULL;
q->next->data = data;
}
}
main()
{
int data,position,result;
char choice='y';
int option;
DLL lists;
while(choice=='y'||choice=='Y')
{
cout<<"\n1.Add a number ";
cout<<"\n2.Display all number ";
cout<<"\n3.Count items of list ";
cout<<"\n4.Insert a number ";
cout<<"\n5.Delete a number ";
cin>>option;
switch(option)
{
case 1:
{
cout<<"\nEnter data ";
cin>>data;
lists.add(data);
break;
}
case 2:
{
cout<<endl;
lists.display();
break;
}
case 3:
{
result=lists.counts();
cout<<"\nNo. of entries = "<<result;
break;
}
case 4:
{
cout<<"\nEnter a number ";
cin>>data;
cout<<"\nEnter the position ";
cin>>position;
if(position<=0 || position>lists.counts()+1)
{
cout<<"\nInvalid Position";
break;
}
result=lists.inserts(data,position);
if(result)
cout<<"\nNumber Inserted Successfully";
else
cout<<"\nNumber couldn't be inserted";
break;
}
case 5:
{
cout<<"\nEnter it's position ";
cin>>position;
if(position<=0 || position>lists.counts())
{
cout<<"\nInvalid Position";
break;
}
result=lists.deletes(position);
if(result)
cout<<"\nNumber deleted Successfully";
else
cout<<"\nNumber couldn't be deleted";
break;
}
default:
cout<<"\nInvalid Choice....Try Again";
}
cout<<"\nDo you want to continue....y/Y??";
//fflush(stdin);
cin>>choice;
}
}