-
Notifications
You must be signed in to change notification settings - Fork 0
/
bignum.cpp
68 lines (57 loc) · 1.51 KB
/
bignum.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
#include <iostream>
#include <string>
using namespace std;
// Node structure for a linked list
struct Node {
int digit;
Node* next;
};
// Function to insert a digit into the linked list
void insertDigit(Node*& head, int digit) {
Node* newNode = new Node;
newNode->digit = digit;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}
// Function to display the linked list
void displayLinkedList(Node* head) {
if (head == nullptr) {
cout << "The linked list is empty." << endl;
return;
}
Node* current = head;
while (current != nullptr) {
cout << current->digit;
current = current->next;
}
cout << endl;
}
int main() {
string bigNumber;
cout << "Enter a large integer number: ";
cin >> bigNumber;
Node* linkedList = nullptr;
// Iterate through the string and insert each digit into the linked list
for (char digitChar : bigNumber) {
int digit = digitChar - '0'; // Convert character to integer
insertDigit(linkedList, digit);
}
cout << "Linked List Representation of the Number: ";
displayLinkedList(linkedList);
// Clean up the linked list
Node* current = linkedList;
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp;
}
return 0;
}