Lab 4: Adding List-Based Integers

Problem overview

As you may already know from earlier courses (e.g., CS130), integer values in C++ (as well as other programming languages) are limited by the number of bits used to represent these data. For instance, a 64-bit unsigned integer has the maximum value of 2^64 - 1 or 18446744073709551615.

To represent integers of arbitrary length, we can use a linked list data structure such that that a node in the linked list corresponds to a single digit in the integer.

For instance, the number 123 can be stored as linked-list that could look like this:

[ 3 ] -> [ 2 ] -> [ 1 ] -> NULL

Note that the first (or head) node in this list contains the least significant digit (in this case 3), while the last node contains the most significant digit (1).

For this problem, you are to read in pairs of arbitrary length integers into custom, user-defined linked lists in C++ (see Requirements below), use the lists to add each pair of numbers, and produce correct output as outlined below.

Inspiration

Note, this problem is inspired by Problem 8.19 from Elements of Programming Interviews and Problem 2.5 from Cracking the Code Interview.

The goal is to cover an interview-related practice problem based on a basic data structure that exists as a container in the STL, but with a more "under the hood" C++ view relevant for future 202/302 content (as well as future job interviews).

Input / Output

You will be given a series of integers from standard input in the follow format:

integer1 integer2
integer1 integer2
...
integer1 integer2

Each integer is of arbitrary length. Moreover, integer1 is not guaranteed to be the same length as integer2.

Example

You are to add each pair of integers. Given the following input:

1 1
123 123
1 12

Your program should output the following:

2
246
13

Requirements

On top of being interview prep, this specific assignment is intended to cover and assess the learning objective in CS202 of "Implementing linked data structures." As such, your solution is required to do the following:

  1. You must create a custom linked list implementation in C++ (i.e., you cannot use std::list); however, you are allowed to use Dr. Emrich's code as a starter. We will ask that you "untemplate" it (see below)

  2. You must define a function that takes two lists and returns a list that contains the sum of the two input lists. This can be a simple global function, i.e, it is not required to overload either "+" or "+=" (but of course you could; this would be more natural in syntax but we might not cover overloading officially until 302 if at all).

  3. You must manage dynamic memory properly for full credit (i.e., no memory leaks or segmentation faults).

  4. The TAs previoiusly were unanimous that templating this was more trouble than its worth relative to other topics/where you are in your coding journey. So this will not be templated but it still must be custom, i.e., you need to make and keep track of your own nodes. We'll push actual templating to 302 as a bonus lecture if there was interest.

Hints

  1. You may wish to read the integers in as std::strings.

  2. Be sure to account for the carry when you perform addition, especially when two strings have the same size (e.g, 50 + 50).

  3. If you implement your linked list as a C++ class, with a "print" function on a returned Sum list as in the lecture notes, you will need to include/write a copy constructor and assignment operator to get full credit for this assignment (see rubric below). Given a list is also a sequence container the code is conceptually similar to what you did for Lab 3 but using a decent amount of pointers vs. a more straightforward copy.


Rubric

We will grade your submission relative to the rubric below.

Note that failure to use a custom list (e.g., using STL list) results in a maximum grade of 23/35 (F); see below.

+2    Code is well formatted, commented (inc. name, assignment, and overview), with reasonable variable names
+12   Uses custom list with fundamental type int   
+2    Defines a function that takes two lists and returns the sum as a linked list 
+3    No issues reported by valgrind on second test applied
+18   Test cases involving at least one number with two or more digits successfully solved (2 points each)

Testing your code prior to submission

To faciliate testing, you were previously asked to clone the course Github repository as follows:

git clone https://github.com/semrich/CS202-22.git cs202

For this assignment, update this clone by using the following:

git pull

We'll discuss this in class but note that your program must be named "main.cpp" and compilable using make. To test your solution against ours, type:

make test

Submission

Please run the following command prior to submission:

tar -cvf lab4.tar list.h list.cpp main.cpp

Note: Although submission will be faciliated by Canvas, we will compile and test on EECS lab machines!

If you develop your solution elsewhere please make sure it works on the lab computers prior to the deadline.