Lab 4: Adding List-Based Integers

Problem overview

As you may already know, 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 for inspiration. Solutions that receive full credit need to modify the sample code, i.e., you can not just copy from my implementation from the notes.

  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 C++ 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).

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, remember the difference between "passing by reference" and "passing by value" and what you learned in Lab 3. I am forcing you to think about this by requiring the separate function for adding since you need to pass lists (aka numbers) ** at least once ** to solve this correctly. This does not mean you need to implement a copy constructor and/or an assignment operator but you will be subject to a potential "double free" if you are not careful.

  4. It also will help to revisit our recent lecture on stacks vs. queues. Using a concept from one of these data structures can make this assignment much easier. Think about it.

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.  We reserve the right to subtract up to 6 points if the list code is 
exactly the same as my notes; to receive full credit you must make key modifications to most efficiently solve 
this problem.   
+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-24.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.