pr8
Write C++ program for storing binary number using doubly linked lists. Write functions
a)To compute 1‘s and 2‘s complement
b)Add two binary numbers
OBJECTIVES:
1. To understand concept of OOP.
2. To understand class structures for DLL in C++.
3. To understand primitive operations on DLL.
#include <iostream>
#include <string>
using namespace std;
// Define a structure for a doubly linked list node
struct Node {
int bit;
Node* next;
Node* prev;
Node(int b) : bit(b), next(nullptr), prev(nullptr) {}
};
// Class to represent a binary number using a doubly linked list
class BinaryNumber {
private:
Node* head; // Head pointer for the doubly linked list
Node* tail; // Tail pointer for the doubly linked list
public:
BinaryNumber() : head(nullptr), tail(nullptr) {}
// Function to add a bit at the end of the list (representing the binary number)
void addBit(int bit) {
Node* newNode = new Node(bit);
if (head == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
// Function to display the binary number
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->bit;
temp = temp->next;
}
cout << endl;
}
// Function to compute the 1's complement of the binary number
BinaryNumber onesComplement() {
BinaryNumber result;
Node* temp = head;
while (temp != nullptr) {
result.addBit(temp->bit == 0 ? 1 : 0); // Invert the bit
temp = temp->next;
}
return result;
}
// Function to compute the 2's complement of the binary number
BinaryNumber twosComplement() {
BinaryNumber result = onesComplement();
Node* temp = result.head;
// Add 1 to the 1's complement to get the 2's complement
bool carry = true;
while (temp != nullptr && carry) {
if (temp->bit == 0) {
temp->bit = 1;
carry = false;
} else {
temp->bit = 0;
carry = true;
}
temp = temp->next;
}
return result;
}
// Function to add two binary numbers
static BinaryNumber add(BinaryNumber& num1, BinaryNumber& num2) {
BinaryNumber result;
Node* temp1 = num1.tail;
Node* temp2 = num2.tail;
int carry = 0;
// Add bit by bit from the least significant bit (tail)
while (temp1 != nullptr || temp2 != nullptr || carry != 0) {
int bit1 = (temp1 != nullptr) ? temp1->bit : 0;
int bit2 = (temp2 != nullptr) ? temp2->bit : 0;
int sum = bit1 + bit2 + carry;
result.addBit(sum % 2); // Add the sum (mod 2) to the result
carry = sum / 2; // Carry for the next addition
if (temp1 != nullptr) temp1 = temp1->prev;
if (temp2 != nullptr) temp2 = temp2->prev;
}
// Reverse the result to get the correct order
reverse(result);
return result;
}
// Function to reverse the binary number (to match the correct order)
static void reverse(BinaryNumber& num) {
Node* temp = num.head;
Node* prev = nullptr;
Node* next = nullptr;
while (temp != nullptr) {
next = temp->next;
temp->next = prev;
temp->prev = next;
prev = temp;
temp = next;
}
num.head = prev;
}
// Destructor to free memory
~BinaryNumber() {
Node* temp = head;
while (temp != nullptr) {
Node* next = temp->next;
delete temp;
temp = next;
}
}
};
int main() {
BinaryNumber num1, num2;
// Add bits to the first binary number (1101)
num1.addBit(1);
num1.addBit(1);
num1.addBit(0);
num1.addBit(1);
// Add bits to the second binary number (1011)
num2.addBit(1);
num2.addBit(0);
num2.addBit(1);
num2.addBit(1);
cout << "Binary Number 1: ";
num1.display();
cout << "Binary Number 2: ";
num2.display();
// Compute and display 1's complement of num1
BinaryNumber onesComp = num1.onesComplement();
cout << "1's Complement of num1: ";
onesComp.display();
// Compute and display 2's complement of num1
BinaryNumber twosComp = num1.twosComplement();
cout << "2's Complement of num1: ";
twosComp.display();
// Add num1 and num2
BinaryNumber sum = BinaryNumber::add(num1, num2);
cout << "Sum of num1 and num2: ";
sum.display();
return 0;
}
Comments
Post a Comment