Hey any help would be awesome, crazy stuck. Thanks for any help you can provide.
ID: 646406 • Letter: H
Question
Hey any help would be awesome, crazy stuck. Thanks for any help you can provide. Ill put the question down below:
An integer can be represented by a linked list with one digit in each node.The least significant digit in this representation is in the first node of the list.This allows for integers of virtually any size.The nodes are of the following type:
struct digit
{ int Value; // a digit value, 0-9
digit* Next;
};
typedef digit* ptrType;
The following diagram shows the representation for 2149:
Develop an ADT named number with the following operations for unlimited-digit numbers. In the following descriptions, number refers to an object of your ADT. M, N and O are all of type number. The sample usage for each operation shows how it might be used, although you can make your operations work differently.
Increment: A method that increases the value of a number by 1. Sample usage: N.Increment();
Note that you cannot necessarily just change the last digit of the number; adding 1 to 1999, for example, requires changing all four digits to get 2000, and adding 1 to 9 requires making a new digit to get 10.
Hint: You should allow for three possibilities in your function regarding the initial value for N.
a. N is NULL
b. N is not NULL, and the value in the first node is 0..8
c. N is not NULL, and the value in the first node is 9
Copy: A method that copies one number to another. Note that you cannot just copy one pointer to another
Explanation / Answer
#include
#include
#include
class N
{
public:
N();
~N();
bool IsEmpty() const;
int GetLength() const;
void DisplayNumber();
void Copy(int M);
void Sum();
private:
struct digit
{
int Value;
struct digit* next;
struct digit* prev;
};
typedef digit* ptrType;
int size;
digit *head;
};
N::N() : size(0), head(NULL)
{
}
N::~N()
{
while (!IsEmpty())
remove(NULL);
}
bool N::IsEmpty() const
{
return size == 0;
}
int N::GetLength() const
{
return size;
}
void N::DisplayNumber()
{
digit *temp;
digit *temp2;
temp = head;
temp2 = head;
if (IsEmpty() == 1)
{
cout << "Contains no Numbers";
}
else
{
while (temp != NULL)
{
temp2 = temp;
temp = temp->next;
}
while (temp2 != NULL)
{
cout << temp2->Value;
temp2 = temp2->prev;
}
}
}
void N::Copy(int M)
{
int newLength = GetLength() + 1;
size = newLength;
digit *newPtr = new digit;
newPtr->Value = M;
newPtr->prev = 0;
newPtr->next = head;
if (head != NULL)
head->prev = newPtr;
head = newPtr;
}
void N::Sum()
{
digit *temp = head;
int mod = 1;
while (temp != NULL)
{
if (temp->Value + mod <= 9)
{
temp->Value = temp->Value + mod;
temp = temp->next;
mod = 0;
}
else
{
temp->Value = temp->Value + mod - 10;
temp = temp->next;
mod = 1;
}
}
if (temp == NULL && mod == 1)
{
temp = head;
temp = temp->next;
int newLength = GetLength() + 1;
digit *newPtr = new digit;
size = newLength;
if (size > 2 )
{
newPtr->Value = mod;
head->next = temp;
newPtr->next = NULL;
if (head == NULL)
head->next = temp;
else
{
while (temp->next !=NULL)
{
temp = temp->next;
}
temp->next = newPtr;
}
newPtr->prev = temp;
}
else
{
newPtr->Value = mod;
newPtr->prev = head;
newPtr->next = temp;
if (head != NULL)
head->next = newPtr;
}
}
}