forked from Srikant797/Cplusplus-for-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublylinkedlist.cpp
219 lines (200 loc) · 3.95 KB
/
doublylinkedlist.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
#include <bits/stdc++.h>
using namespace std;
class DLL;
class node{
node *prev;
bool n;
node*next;
public:
node(){
prev=next=NULL;
}
node(bool b){
n=b;
prev=next=NULL;
}
friend class DLL;
};
class DLL
{
node *start;
public:
DLL()
{
start=NULL;
}
void Binary(int no);
void displayBinary();
void Ones_Complement_of_Binary();
void Twos_Complement_of_Binary();
DLL operator +(DLL n1);
bool addBitAtBegin(bool val)
{
node *nodee=new node(val);
if(start==NULL)
{
start=nodee;
}
else
{
nodee->next=start;
start->prev=nodee;
start=nodee;
}
return true;
}
};
void DLL::Binary(int no){
bool rem;
node *p;
rem=no%2;
start=new node(rem);
no=no/2;
while(no!=0)
{
rem=no%2;
no=no/2;
p=new node(rem);
p->next=start;
start->prev=p;
start=p;
}
}
void DLL::displayBinary(){
node *t;
t=start;
while(t!=NULL)
{
cout<<t->n;
t=t->next;
}
}
void DLL::Ones_Complement_of_Binary(){
node *t;
t=start;
while(t!=NULL)
{
if(t->n==0)
t->n=1;
else
t->n=0;
t=t->next;
}
}
DLL DLL::operator +(DLL n1){
DLL sum;
node *a=start;
node *b=n1.start;
bool carry=false;
while(a->next!=NULL)
a=a->next;
while(b->next!=NULL)
b=b->next;
while(a!=NULL && b!=NULL)
{
sum.addBitAtBegin((a->n)^(b->n)^carry);
carry=((a->n&& b->n) || (a->n&& carry) || (b->n && carry));
a=a->prev;
b=b->prev;
}
while(a!=NULL)
{
sum.addBitAtBegin(a->n^carry);
a=a->prev;
}
while(b!=NULL)
{
sum.addBitAtBegin(b->n^carry);
b=b->prev;
}
sum.addBitAtBegin(carry);
return sum;
}
void DLL::Twos_Complement_of_Binary(){
Ones_Complement_of_Binary();
bool carry=1;
node *t;
t=start;
while(t->next!=NULL)
{
t=t->next;
}
while(t!=NULL){
if(t->n==1&& carry==1){
t->n=0;
carry=1;
}
else if(t->n==0&& carry==1){
t->n=1;
carry=0;
}
else if(carry==0)
break;
t=t->prev;
}
displayBinary();
}
int main(){
int num,num1;
DLL n1,n3,n2;
int agree=1, choice;
cout<<"\nDouble Linked List Implementation\n";
while(agree!=0){
cout<<"\n"<<"Enter number according to the menu provided below"<<"\n";
cout<<"\n"<<"1.One's Complement of a Binary Number"<<"\n"
<<"2.Two's Complement of a Binary Number"<<"\n"
<<"3.Addition of 2 binary numbers"<<"\n"
<<"4.Exit"<<"\n\n"<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:cout<<"\nEnter Number in decimal form: ";
cin>>num;
n1.Binary(num);
cout<<"\nBinary Representation of the number you entered: ";
n1.displayBinary();
cout<<"\nOne's Complement: ";
n1.Ones_Complement_of_Binary();
n1.displayBinary();
cout<<"\n";
break;
case 2:cout<<"\nEnter Number in decimal form: ";
cin>>num;
n1.Binary(num);
cout<<"\nBinary Representation of the number you entered: ";
n1.displayBinary();
cout<<"\nTwo's complement: ";
n1.Twos_Complement_of_Binary();
cout<<"\n";
break;
case 3: cout<<"\nEnter Two Numbers you want to add in Decimal form: \n";
cout<<"\nEnter the First Number: ";
cin>>num;
n1.Binary(num);
cout<<"\nBinary Representation of the number you entered: ";
n1.displayBinary();
cout<<"\nEnter the Second Number: ";
cin>>num1;
n2.Binary(num1);
cout<<"\nBinary Representation of the number you entered: ";
n2.displayBinary();
cout<<"\n";
n1.displayBinary();
cout<<" + ";
n2.displayBinary();
cout<<" = ";
n3=n1+n2;
n3.displayBinary();
cout<<"\n";
break;
case 4: cout<<"\nYou've chosen to exit the program!"<<"\n";
cout<<"Why (ToT)?"<<"\n";
agree=0;
cout<<"\n";
break;
default: cout<<"Learn counting between 1 to 4 (-_-)"<<"\n";
cout<<"\n";
break;
}
}
return 0;
}