2019/6/6 Program #5
https://oregonstate.instructure.com/courses/1725108/assignments/7582147 1/5
Program #5
C++ Linked List Implementation
Motivation
As we discussed in class, the data structures that you use to implement your program can have a profound
impact on it's overall performance. A poorly written program will often need much more RAM and CPU time
then a well-written implementation. One of the most basic data structure questions revolves around the
difference between an array and a linked list. After you finish this homework you should have a firm
understanding of their operation.
NOTE: The submission guidelines for this assignment are slightly different from the previous homeworks. Be
sure to understand the readme.txt requirement.
Problem Statement
For this assignment, you will implement a linked list class using pointers and object-oriented programming.
Although the C++ STL (Standard Template Library) offers a linked list implementation, you must implement
this program "from scratch" and cannot simply utilize the existing STL offerings ( or ).
Your linked list will be designed to contain signed integers of type int.
Required Classes
You must implement the classes shown below (as well as the exact member functions that are listed).
Note: It is okay to add additional functions or variables as desired. You cannot add extra parameters to the
functions that are listed.
class Linked_List_Node {
public:
int val; // the value that this node stores
Linked_List_Node *next; // a pointer to the next node in the list
// you can add constructors or other functionality if you you find it useful or necessary
}
Note: Linked_List_Node is being used akin to a struct (with public member variables). This is intentional so
that you can easily modify the member variables from within the Linked_List class.
class Linked_List {
private:
unsigned int length; // the number of nodes contained in the list
Linked_List_Node *first; // a pointer to the first node in the list
// anything else you need...
public:
int get_length();
// note: there is no set_length(unsigned int) (the reasoning should be intuitive)
2019/6/6 Program #5
https://oregonstate.instructure.com/courses/1725108/assignments/7582147 2/5
void print(); // output a list of all integers contained within the list
void clear(); // delete the entire list (remove all nodes and reset length to 0)
unsigned int push_front(int); // insert a new value at the front of the list (returns the new length of the li
st)
unsigned int push_back(int); // insert a new value at the back of the list (returns the new length of the lis
t)
unsigned int insert(int val, unsigned int index); // insert a new value in the list at the specified index (re
turns the new length of the list)
void sort_ascending(); // sort the nodes in ascending order. You must implement the recursive Merge Sort algor
ithm
// Note: it's okay if sort_ascending() calls a recursive private function to perform the sorting.
void sort_descending(); // sort the nodes in descending order
// you can add extra member variables or functions as desired
}
Note that the sort_ascending() function must be implemented using the recursive Merge Sort algorithm
(https://en.wikipedia.org/wiki/Merge_sort) . sort_descending() can utilize Merge Sort or a different algorithm
(see extra credit).
You are also required to implement a function that counts the number of prime numbers within a Linked_List.
This can be written as part of the Linked_List class or in some other class. For our purposes, a negative
number is never considered to be prime.
Application Description
Once you have implemented the fundamental building blocks of a linked list you will use this functionality to
build a simple application. Implement a program to replicate the following behavior:
Note: All of the program functionality must be implemented using your linked list. You should not be storing
or copying the user input into an array, vector, or other types of data structures.
Please enter a number: 146
Do you want another num (y or n): y
Enter a number: 30
Do you want another num (y or n): y
Enter a number: 73
Do you want another num (y or n): y
Enter a number: 10
Do you want another num (y or n): y
Enter a number: -31
Do you want another num (y or n): n
Sort ascending or descending (a or d)? a
Your linked list is: -31 10 30 73 146
You have 1 prime number(s) in your list.
Do you want to do this again (y or n)? n
Other Program Requirements
When sorting nodes, you may not swap the values between nodes and must change the pointers on the
nodes to swap them.
You must have a class for each of the following things: Linked_List , Linked_List_Node . As usual, it is
completely fine to implement additional classes if you can defend their existence.
Your program must be factored into interface, implementation, and application. Specifically, you should
have one header file and one implementation file for each class, and you should have a single
2019/6/6 Program #5
https://oregonstate.instructure.com/courses/1725108/assignments/7582147 3/5
application file containing your main() function. If you choose to implement a template class, it is
acceptable to insert both the header and implementation information into a corresponding .hpp file.
You must provide a working Makefile in the TAR archive.
The Big 3 must be implemented as appropriate.
Your program may not have any memory leaks. I strongly recommend that you use Valgrind to
occasionally test your program as you develop it (even when everything seems to be working).
Segmentation faults/core dumps are not allowed (e.g. your program crashes).
If your implementation does not follow the spirit of the assignment you will lose 50 points! You must
implement a linked list for the storage of the data.
This program is due immediately prior to the start finals week. As a result, you will not schedule the usual
demo with a TA. Instead, the TAs will grade this homework on their own. For this reason, your TAR file is
required to include an extra text file named readme.txt which will contain your name, a description of the
program, and any additional information that the TA should know when grading your assignment. In
addition, if you implement the extra credit work, the readme.txt file MUST list the extra credit work that
you did.
Extra Credit
In addition to the requirements above, you may earn extra credit as follows:
(5 points) Implement the assignment using a template class. Use the template class to implement the
original homework assignment (with type int). Utilize the exact same template class to implement a
second version that operates with input of type unsigned int. When your application executes, give the
user the option to utilize the int or unsigned int version of your code. If you implement this extra credit,
be sure to describe this in the readme.txt file. Note: if your code is implemented correctly, even a gigantic
prime number (such as 4294963943) should be detected (as long as it fits into an unsigned int).
(5 points) Implement sort_descending() using a recursive Selection Sort algorithm. If you implement this
extra credit, be sure to describe this in the readme.txt file.
Implementation Details
For this assignment you have additional freedom to design your own implementation. There are certain
baseline requirements as specified in this document. However, any additional member functions and
member variables are up to you, as long as the spirit of the assignment is followed.
PLEASE... take the time to design your program on paper. Use scratch paper with flow charts or pseudocode. In particular, think of how your program will work when the various nodes are added or removed. What
needs to happen when a link is added? What happens when a link is deleted? Consider the edge cases that
arise when you add or delete nodes at the beginning and end of the list. These are the sorts of questions
that should be answered during the design stage, long before you start writing code.
Taking the time to do a thorough program design can save you hours of frustrating debugging time!
Programming Style/Comments
2019/6/6 Program #5
https://oregonstate.instructure.com/courses/1725108/assignments/7582147 4/5
C++ Linked List Rubirc
In your implementation, make sure that you include a file header for every .cpp, .h, or .hpp file. Also ensure
that you use proper indentation/spacing and include comments! Below is an example header to include. As
usual, please refer to the style guidelines for this class
(https://web.engr.oregonstate.edu/~goinsj/resources/general/cpp_style_guideline.pdf) if you have any
questions regarding the proper formatting.
/******************************************************
** Program: linked_list.cpp
** Author: Your Name
** Date: 05/29/2019
** Description:
** Input:
** Output:
******************************************************/
When you compile your code, it is acceptable to use C++11 functionality in your program. For this final
programming assignment you are welcome to research and utilize smart pointers if you desire (although
they may complicate your design).
In order to submit your homework assignment, you must create a TAR archive that contains your readme.txt,
source code, and Makefile. This tar file will be submitted to TEACH
(https://engineering.oregonstate.edu/teach) . In order to create the tar file, use the following command:
tar –cvf assign5.tar readme.txt Makefile
Special Notes
This is the final programming assignment for this class and you are not required to demo this code. Instead,
the TAs will grade your code on their own during finals week. This is why it is important for you to create a
readme.txt with any additional information that you feel the TAs should know.
2019/6/6 Program #5
https://oregonstate.instructure.com/courses/1725108/assignments/7582147 5/5
: 100.0 , 100.0
10.0
10.0
60.0
10.0
5.0
0.0
0.0
0.0
5.0
This criterion is linked to a Learning Outcome Program Header / Good indentation & Use of whitespace /
Function Documentation
At a minimum, header should contain author's name (2pts) and a description of the program. (2pts)
Conditional blocks of code should always be indented. (3pts) Every function contains it's own initial block
comment (or multiple lines of comments prior to the function definition) that provides the reader with an
explanation of the function's purpose (3 pts). -1 pt for each function that is missing a header (up to 5 point
penalty).
Determine Prime numbers in a linked list
Correctly implement a function that counts the number of prime numbers within a Linked_List
Correct function behaviors
Implement all functions in Linked_List correctly. get_length(), print(), clear(), push_front(), push_back() 5
pts each..........insert() 9 pts (6 pts for correctly insert a node, 3 pts for returning the new length of the
list)..........sort_ascending(), sort_descending() 13 pts each (-10 pts if sort_ascending() is not implemented
using recursive merge sort algorithm)
Application
Implement an application program to replicate the behavior provided in the requirement
Use Big 3 appropriately
It's okay if students don't "need" the big 3 in their application.
Extra Credit 1 (5 pts)
Implement the assignment using a template class. Program must allow user to select and use "unsigned
int" mode. Only receives 3 out of 5 pts if program does not detect 4294963943 as prime.
Extra Credit 2 (5 pts)
Implement sort_descending() using a recursive Selection Sort algorithm
Auto Deductions
-50 Not utilizing linked list to follow the spirit of the assignment (send to Justin Goins if unsure) -20 swap
just values but not pointers when sorting nodes; -10 did not use required classes and functions with exact
same names (-1 pt for each unmatched name found, 10 points at max) -10 memory leaks (use valgrind
and sliding scale for how many/bad it is), -10 no makefile or tar, -10 use of globals or libraries not
introduced in class, -10 functions over 20 lines (sliding scale for how many/bad it is), -20 didn't separate
files
Included readme.txt
TAR file contains readme.txt with author's name, a description of the program, and any additional
information that the TA should know. Extra credit work should also be described here.