Description
The range of int is -2147483648 ~ 2147483647; the range of long int is -2147483648 ~ 2147483647 on 32-bit platforms and is -9223372036854775808 ~ 9223372036854775807 on 64bit platforms; the range of long long int is -9223372036854775808 ~ 9223372036854775807.
What if we want to store an integer with value 1000000000000000000000?
We could create an ADT to represent large numbers! The number is represented by a linked list and each element stored in the linked list is an integer in the range 0 ~ 9.
Ex. 1 Templated Singly-Linked List
Related topics: template, linked list, deep copy
To get you familiar with templates, the singly-linked list you are going to implement is a templated
node last .
returnFront is used to return the pointer that points to the first node first in the list, so that
you can use it to iterate through the whole list. We don’t want the value of first to be changed outside the class, so there is a const before Node_t in the function declaration; we also don’t want this function itself to change any member in this class, so there is another const at the end of the function declaration.
print is already implemented in mylist_impl.h , which prints elements in the list in order.
Please do not modify it.
Since dynamically allocated storage occurs in this class, you must also provide a destructor, a copy constructor, and an assignment operator.
Ex. 2 Which one is larger?
As mentioned above, you are going to use this linked list to store a large integer. And you want to provide a function to compare two integers stored in two linked lists. In order to make the implementation of this function to be easier, an integer is represented “reversely”. For example, integer 415 is represented by:
The comparison function you need to implement is
bool isLarger(const List<int> &a, const List<int> &b); // EFFECTS: returns true if the number represented by a
// is larger than the number represented by b; // otherwise, returns false.
// returns false if both a and b are empty Example:
Ex. 3 Addition
Addition is a basic operation on integers. You want to implement this for List<int> . The representation of an integer by a List<int> is the same as in Ex. 2.
List<int> add(const List<int> &a, const List<int> &b);
// EFFECTS: adds the numbers represented by a and b; returns the result Example:
Submission
mylist.h and mylist_impl.h can be found in lab9_starter_files on Canvas. Please
implement the linked list methods and another two functions in mylist_impl.h . Submit it as a tar or zip file via online judgement system.
There is also a simpletest.cpp for you to test the linked list you implemented. Please check and make sure there is no memory leak.
Created by Yaxin Chen.
@UM-SJTU Joint Institute
Reviews
There are no reviews yet.