Assignment03 (Light Theme)
Assignment03 (Light Theme)
Description
In this assignment, you will implement insertion sort and merge sort so you can compare them experimentally.
The catch is you will need to implement a linked list class similar to assignment 1. Let’s take a look at the
linked list class
# ifndef LL_H
# define LL_H
node * head ;
node * tail ;
std :: size_t size ;
public :
class Iterator
{
private :
node * current ;
1
public :
friend class LL ;
Iterator () : current ( nullptr ) {}
Iterator ( node * p ) : current ( p ) {}
Type operator *() { return current - > data ; }
2
Iterator spliceOut ( Iterator &);
void insertion ( Iterator & , Iterator &);
};
# endif
3
• void LL<Type>::push_back(LL<Type>::Iterator& nodeToBePushedBack) - function that pushes an
existing node pointed to by nodeToBePushedBack iterator and insert this existing node to the back of
the linked list, increment size counter by 1 afterwards
• size_t LL<Type>::getSize() const - returns the size of the linked list
• typename LL<Type>::Iterator LL<Type>::pop_front() - a function that removes the front element
of the linked list and returns an iterator that points to the original front node of the linked list,
decrement size counter by 1 afterwards
• typename LL<Type>::Iterator LL<Type>::spliceOut(LL<Type>::Iterator& it) - a function that
removes a node from the linked list pointed to by the ”it” iterator, the iterator is to be assigned to
the next node of the linked list and then an iterator that points to the original ”it” node is returned,
decrement size counter by 1 before returning the iterator
• void LL<Type>::insertion(LL<Type>::Iterator& locationOfLL,
LL<Type>::Iterator& nodeToBeInserted) - a function that inserts a node pointed to by nodeToBeInserted
right after node pointed to by locationOfLL in the linked list, if locationOfLL is nullptr, then insert
the node nodeToBeInserted to the front, increment size counter by 1 afterwards
• typename LL<T>::Iterator LL<T>::begin() const - returns an Iterator object whose current field
contains this->head
• typename LL<T>::Iterator LL<T>::end() const - returns an Iterator object whose current field
contains this->tail
Input
A list of integers (one integer per line), each line is terminated with an end of line character, you would need
to have an LL<int> object declared and then do a tail insert for each element read from the file
Output
You need to output a sorted list in ascending order, each element needs to be separated by a white space so
code grade would be able to compare you answers
4
LL < int > mergeSort ( LL < int >& list , LL < int >:: Iterator front , LL < int >:: Iterator back )
Once you receive the two sorted linked lists, you call merge. Merge takes two sorted linked lists, inside
the function you call the pop_front() functions on the two sorted linked lists and then push_back() the
iterator that contains the smaller value into a local linked list object. Once everything is merged, return the
local object that contains the final merged linked list. The function prototype can be seen below.
LL < int > merge ( LL < int >& leftList , LL < int >& rightList )
Criteria
• Include header comments for every file turned in
• Document your code, must be valid documentation not just commented out content for debugging
• Make sure you linked list properly deallocates
• Your code must pass all test cases to get full credit
• Do not hard code the output
• You can only use push_back(const Type& item) only when building a linked list from the input, for
each sorting algorithm you need to use Iterator spliceOut(Iterator&) and
void insertion(Iterator&, Iterator&) to implement insertion sort, and to implement merge sort
you can only use Iterator pop_front() and void push_back(Iterator&)
Sample Run
g ++ insertion_sort . cpp
./ a . out < input01 . txt
g ++ merge_sort . cpp
./ a . out < input01 . txt
Submission
Submit the LL.cpp, insertion sort.cpp, and merge sort.cpp to code grade by the deadline
References
• Supplemental Video https://youtu.be/3U01YRzB8kg
• Link to the top image can be found at https://spongebob.fandom.com/wiki/F.U.N. Song?file=F.U.N 071.png