TASK You can represent an integer with any number of digits by storing the integer as a linked list of digits. In this a
Posted: Sat Nov 27, 2021 2:40 pm
TASK
You can represent an integer with any number of digits by storing
the integer as a linked
list of digits. In this assignment, you’ll design and implement a
class for whole numbers
arithmetic in which a number is implemented as a linked list of
integers. You are asked
to use doubly linked list as the internal data structure for the
linked list of digits.
"423974289737970176029837287023745938752093"
4->2->3->9->7->4->2->8->9->7->3->7->9->7->0->1->7->6->0->2->9->8->3->7->2->8->7->0->2->3->7->4->5->9->3->8->7->5->2->0->9->3
This assignment will help you get practice with:
• Linked Lists
• Doubly Linked List
• Implementing iterator for a data structure
• Working with really big integer numbers
• Converting from string into a linked list of digits
• Reading from a file
Doubly Linked List
A doubly linked list is a list that contains links to next and
previous nodes. Unlike singly
linked lists where traversal is only one way, doubly linked lists
allow traversals in both
ways. A generic doubly linked list node can be designed as:
template <class T>
class Node{
public:
T data;
Node<T>* next;
Node<T>* prev;
};
Functional Requirements:
1. Write a C++ class, a generic data type for doubly linked list
data structure,
namely DoublyLinkedList, that implements the following API:
template <class T>
class DoublyLinkedList {
protected:
Node<T> *first; //a pointer to the first of the linked
list
Node<T> *last; //a pointer to the last node of the
linked
list
Node<T> *iterator; //an internal iterator for the linked
list
object
int length; //number of items in the linked list
public:
//default no-argument constructor
DoublyLinkedList();
//destructor
~DoublyLinkedList();
//copy constructor
DoublyLinkedList(const DoublyLinkedList<T> &);
//copy assignment operator
DoublyLinkedList operator=(const DoublyLinkedList<T>
&);
//returns true if the list is empty, false otherwise
bool isEmpty() ;
//returns the number of items in the list
int getLength();
//inserts a new item to the beginning of the list
void insertFirst(const T &);
//inserts a new item at the end of the list
void insertLast(const T &);
//deletes the first item from the list
void deleteFirst();
//deletes the last item in the list
void deleteLast();
//destroys the list and makes it empty
void clear();
//iterator functions
//sets the iterator to the beginning of the linked list
void setIteratorFirst();
//sets the iterator to the beginning of the linked list
void setIteratorLast();
//checks if the iterator has next
bool hasNext();
//checks if the iterator has prev
bool hasPrev();
//sets the iterator to the next node
void next();
//sets the iterator to the previous node
void prev();
//returns true if the iterator is null
bool isIteratorNULL();
//gets the data the iterator pointing at
T getData();
//friend functions
//overloading operator<<
template <class U>
friend ostream& operator<<(ostream& out, const
DoublyLinkedList<U> &);
//overloading operator>>
template <class U>
friend istream& operator>>(istream& in,
DoublyLinkedList<U> &);
};
2. Design and implement BigInteger class that uses the doubly
linked list
implementation to represent big numbers. BigInteger class should
overload
the following functions:
a. operator+
b. operator-
c. operator<=
d. operator>=
e. operator==
f. operator>
g. operator<
h. operator=
i. operator<<
j. operator>>
3. Throw the specified exception for the following corner
cases:
a. Create a IllegalArgumentException exception class and throw
an
IllegalArgumentException for a nun-numeric input to the
constructor
b. Create a NoSuchElementException class and throw an
NoSuchElementException if the client calls next() and/or
prev()
method in the iterator when there are no more items to return
4. A sample test.cpp file provided for you to test your
implementation. Please make
sure your implementation works with test.cpp and you run the tests
before you
submit your work. Please note that the total score after passing
all test cases is
40: 20 points for doubly linked list implementation + 20 points for
Big Integer
class implementation. The total point will be your final score for
correctness of
the assignment.
//test.cpp
#include "DoublyLinkedList.h"
#include "BigInteger.h"
#include <iostream>
#include <fstream>
using namespace std;
int score = 0;
void grade(bool condition, int points)
{
if (condition)
{
cout << "Pass" << endl;
score += points;
}
else
{
cout << "Fail" << endl;
}
}
void dllTest(){
DoublyLinkedList<int> list;
grade(list.isEmpty() == true, 1);
for(int i=0; i<10; i++){
list.insertFirst(i);
}
grade(list.getLength() == 10, 2);
list.setIteratorFirst();
grade(list.hasNext() == false, 2);
grade(list.getData() == 9, 1);
list.next();
grade(list.getData() == 8, 2);
list.prev();
grade(list.getData() == 9, 2);
list.setIteratorLast();
grade(list.hasNext() == true, 2);
grade(list.getData() == 0, 1);
list.insertFirst(10);
list.setIteratorFirst();
grade(list.getData() == 10, 1);
list.insertLast(40);
list.setIteratorLast();
grade(list.getData() == 40, 1);
list.deleteFirst();
list.setIteratorFirst();
grade(list.getData() == 9, 2);
list.deleteLast();
list.setIteratorLast();
grade(list.getData() == 0, 2);
list.clear();
grade(list.getLength() == 0, 1);
}
void bigIntegerTest(){
BigInteger int1("19"), int2("300");
BigInteger actual2 = int1 - int2;
BigInteger expected2("-281");
grade(actual2 == expected2, 3);
grade(actual2.isNegative() == true, 2);
BigInteger bigInt1("55555556666666687888999");
grade(bigInt1.getLength() == 23, 2);
BigInteger bigInt2, bigInt3;
ifstream file;
file.open("test.txt");
file>>bigInt2>>bigInt3;
grade(bigInt2.getLength() == 23, 2);
grade(bigInt3.getLength()==24, 2);
BigInteger actual1 = bigInt2 + bigInt3;
BigInteger expected1("234657681223242153555775");
grade(actual1 == expected1, 2);
BigInteger actual3 = bigInt2 - bigInt2;
BigInteger expected3("0");
grade(actual3 == expected3, 2);
BigInteger actual4 = bigInt2 - bigInt3;
BigInteger expected4("-123546567889908777777777");
grade(actual4 == expected4, 2);
grade(bigInt2<bigInt3 == true, 1);
grade(bigInt3>=bigInt2 == true, 1);
BigInteger
bigInt4("12345678997654321"),bigInt5("12345678997654321");
grade((bigInt4<=bigInt5 == true), 1);
}
int main(){
dllTest(); //test cases for doubly linked list
implementation
bigIntegerTest(); //test cases for big integer implementation
cout<<"Test Score:"<<score<<endl;
return 0;
}
Here is the content of the test.txt file.
55555556666666687888999
179102124556575465666776
You can represent an integer with any number of digits by storing
the integer as a linked
list of digits. In this assignment, you’ll design and implement a
class for whole numbers
arithmetic in which a number is implemented as a linked list of
integers. You are asked
to use doubly linked list as the internal data structure for the
linked list of digits.
"423974289737970176029837287023745938752093"
4->2->3->9->7->4->2->8->9->7->3->7->9->7->0->1->7->6->0->2->9->8->3->7->2->8->7->0->2->3->7->4->5->9->3->8->7->5->2->0->9->3
This assignment will help you get practice with:
• Linked Lists
• Doubly Linked List
• Implementing iterator for a data structure
• Working with really big integer numbers
• Converting from string into a linked list of digits
• Reading from a file
Doubly Linked List
A doubly linked list is a list that contains links to next and
previous nodes. Unlike singly
linked lists where traversal is only one way, doubly linked lists
allow traversals in both
ways. A generic doubly linked list node can be designed as:
template <class T>
class Node{
public:
T data;
Node<T>* next;
Node<T>* prev;
};
Functional Requirements:
1. Write a C++ class, a generic data type for doubly linked list
data structure,
namely DoublyLinkedList, that implements the following API:
template <class T>
class DoublyLinkedList {
protected:
Node<T> *first; //a pointer to the first of the linked
list
Node<T> *last; //a pointer to the last node of the
linked
list
Node<T> *iterator; //an internal iterator for the linked
list
object
int length; //number of items in the linked list
public:
//default no-argument constructor
DoublyLinkedList();
//destructor
~DoublyLinkedList();
//copy constructor
DoublyLinkedList(const DoublyLinkedList<T> &);
//copy assignment operator
DoublyLinkedList operator=(const DoublyLinkedList<T>
&);
//returns true if the list is empty, false otherwise
bool isEmpty() ;
//returns the number of items in the list
int getLength();
//inserts a new item to the beginning of the list
void insertFirst(const T &);
//inserts a new item at the end of the list
void insertLast(const T &);
//deletes the first item from the list
void deleteFirst();
//deletes the last item in the list
void deleteLast();
//destroys the list and makes it empty
void clear();
//iterator functions
//sets the iterator to the beginning of the linked list
void setIteratorFirst();
//sets the iterator to the beginning of the linked list
void setIteratorLast();
//checks if the iterator has next
bool hasNext();
//checks if the iterator has prev
bool hasPrev();
//sets the iterator to the next node
void next();
//sets the iterator to the previous node
void prev();
//returns true if the iterator is null
bool isIteratorNULL();
//gets the data the iterator pointing at
T getData();
//friend functions
//overloading operator<<
template <class U>
friend ostream& operator<<(ostream& out, const
DoublyLinkedList<U> &);
//overloading operator>>
template <class U>
friend istream& operator>>(istream& in,
DoublyLinkedList<U> &);
};
2. Design and implement BigInteger class that uses the doubly
linked list
implementation to represent big numbers. BigInteger class should
overload
the following functions:
a. operator+
b. operator-
c. operator<=
d. operator>=
e. operator==
f. operator>
g. operator<
h. operator=
i. operator<<
j. operator>>
3. Throw the specified exception for the following corner
cases:
a. Create a IllegalArgumentException exception class and throw
an
IllegalArgumentException for a nun-numeric input to the
constructor
b. Create a NoSuchElementException class and throw an
NoSuchElementException if the client calls next() and/or
prev()
method in the iterator when there are no more items to return
4. A sample test.cpp file provided for you to test your
implementation. Please make
sure your implementation works with test.cpp and you run the tests
before you
submit your work. Please note that the total score after passing
all test cases is
40: 20 points for doubly linked list implementation + 20 points for
Big Integer
class implementation. The total point will be your final score for
correctness of
the assignment.
//test.cpp
#include "DoublyLinkedList.h"
#include "BigInteger.h"
#include <iostream>
#include <fstream>
using namespace std;
int score = 0;
void grade(bool condition, int points)
{
if (condition)
{
cout << "Pass" << endl;
score += points;
}
else
{
cout << "Fail" << endl;
}
}
void dllTest(){
DoublyLinkedList<int> list;
grade(list.isEmpty() == true, 1);
for(int i=0; i<10; i++){
list.insertFirst(i);
}
grade(list.getLength() == 10, 2);
list.setIteratorFirst();
grade(list.hasNext() == false, 2);
grade(list.getData() == 9, 1);
list.next();
grade(list.getData() == 8, 2);
list.prev();
grade(list.getData() == 9, 2);
list.setIteratorLast();
grade(list.hasNext() == true, 2);
grade(list.getData() == 0, 1);
list.insertFirst(10);
list.setIteratorFirst();
grade(list.getData() == 10, 1);
list.insertLast(40);
list.setIteratorLast();
grade(list.getData() == 40, 1);
list.deleteFirst();
list.setIteratorFirst();
grade(list.getData() == 9, 2);
list.deleteLast();
list.setIteratorLast();
grade(list.getData() == 0, 2);
list.clear();
grade(list.getLength() == 0, 1);
}
void bigIntegerTest(){
BigInteger int1("19"), int2("300");
BigInteger actual2 = int1 - int2;
BigInteger expected2("-281");
grade(actual2 == expected2, 3);
grade(actual2.isNegative() == true, 2);
BigInteger bigInt1("55555556666666687888999");
grade(bigInt1.getLength() == 23, 2);
BigInteger bigInt2, bigInt3;
ifstream file;
file.open("test.txt");
file>>bigInt2>>bigInt3;
grade(bigInt2.getLength() == 23, 2);
grade(bigInt3.getLength()==24, 2);
BigInteger actual1 = bigInt2 + bigInt3;
BigInteger expected1("234657681223242153555775");
grade(actual1 == expected1, 2);
BigInteger actual3 = bigInt2 - bigInt2;
BigInteger expected3("0");
grade(actual3 == expected3, 2);
BigInteger actual4 = bigInt2 - bigInt3;
BigInteger expected4("-123546567889908777777777");
grade(actual4 == expected4, 2);
grade(bigInt2<bigInt3 == true, 1);
grade(bigInt3>=bigInt2 == true, 1);
BigInteger
bigInt4("12345678997654321"),bigInt5("12345678997654321");
grade((bigInt4<=bigInt5 == true), 1);
}
int main(){
dllTest(); //test cases for doubly linked list
implementation
bigIntegerTest(); //test cases for big integer implementation
cout<<"Test Score:"<<score<<endl;
return 0;
}
Here is the content of the test.txt file.
55555556666666687888999
179102124556575465666776