0% found this document useful (0 votes)
9 views5 pages

Assignment03 (Light Theme)

The assignment requires the implementation of insertion sort and merge sort using a linked list class. Students must create a linked list class with specific functionalities and utilize iterators for sorting algorithms. The final output should be a sorted list of integers read from a file, and submissions must include the relevant source files by the deadline.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Assignment03 (Light Theme)

The assignment requires the implementation of insertion sort and merge sort using a linked list class. Students must create a linked list class with specific functionalities and utilize iterators for sorting algorithms. The final output should be a sorted list of integers read from a file, and submissions must include the relevant source files by the deadline.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS302

Assignment 3: More Fun With Sorting

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

Linked List Class Template


# include < cstdlib >

# ifndef LL_H
# define LL_H

template < class Type >


class LL
{
private :
struct node
{
Type data ;
node * next ;
node * prev ;
};

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 ; }

Iterator operator ++()


{
current = current - > next ;
return * this ;
}
Iterator operator ++( int )
{
Iterator i (* this );
current = current - > next ;
return i ;
}

Iterator operator - -()


{
current = current - > prev ;
return * this ;
}
Iterator operator - -( int )
{
Iterator i (* this );
current = current - > prev ;
return i ;
}

bool operator ==( const Iterator & rhs )


{
return this - > current == rhs . current ;
}

bool operator !=( const Iterator & rhs )


{
return this - > current != rhs . current ;
}
};

LL () : head ( nullptr ) , tail ( nullptr ) , size (0) {}


LL ( const LL < Type >&);
const LL < Type >& operator =( const LL < Type >&);
~ LL ();
Iterator begin () { return Iterator ( head ); }
Iterator end () { return Iterator ( tail ); }
std :: size_t getSize () const { return size ;}
void push_back ( const Type &);
bool isEmpty () const { return head == nullptr ; }

void push_back ( Iterator &);


Iterator pop_front ();

2
Iterator spliceOut ( Iterator &);
void insertion ( Iterator & , Iterator &);

};

# include " LL . cpp "

# endif

Each member of the Iterator class performs/contains the following


• node * current - a pointer that contains the address of the node that the Iterator object points to
• LL<T>::Iterator::Iterator() - default constructor that sets current with NULL or nullptr
• LL<T>::Iterator::Iterator(Node* ptr) - constructor that sets current = ptr
• T LL<T>::Iterator::operator*() const - overloads the dereference operator, just returns the data
field of the node the Iterator object is pointing to
• const typename LL<T>::Iterator& LL<T>::Iterator::operator++(int) - postfix ++ operator that
moves the Iterator object one node over to the right
• const typename LL<T>::Iterator& LL<T>::Iterator::operator++() - prefix ++ operator that
moves the Iterator object one node over to the right
• const typename LL<T>::Iterator& LL<T>::Iterator::operator--(int) - postfix – operator that
moves the Iterator object one node over to the left
• const typename LL<T>::Iterator& LL<T>::Iterator::operator--(int) - refixfix – operator that
moves the Iterator object one node over to the left
• bool LL<T>::Iterator::operator==(const Iterator& rhs) const - comparison operator, com-
pares if the *this Iterator and the rhs Iterator point to the same node, if they do return true
else return false
• bool LL<T>::Iterator::operator!=(const Iterator& rhs) const - comparison operator, com-
pares if the *this Iterator and the rhs Iterator point to a different node, if they point to different
nodes return true else return false
Each member of the LL class performs/contains the following
• struct node - a struct object that contains an element in the list and a pointer for its left and right
neighbor within the list
• node * head - head pointer, points to the start of the list (the leftmost node)
• node * tail - tail pointer, points to thee end of the list (the rightmost node)
• size_t size - denotes the size of the linked list (the amount of nodes in the linked list)
• LL<T>::LL() - default constructor, assigns the head and tail with NULL or nullptr
• LL<T>::LL(const LL<T>& copy) - deep copy constructor, deep copies the copy object into the *this
object
• const LL<T>& LL<T>::operator=(const LL<T>& rhs) - deep copy assignment operator, deep copies
the rhs object into the *this object, make sure you deallocate the *this object first before performing
the deep copy, also check for self assignment, then return *this at the end
• LL<T>::~LL() - destructor, deallocates the entire linked list
• void LL<T>::back_back(const T& item) - insert a new node to the back of the linked list and this
node’s data field must contain the contents in the item parameter, increment size afterwards

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

Insertion Sort Main


Recall from lecture the insertion sort algorithm, you have a loop counter i and it starts at 1 and traverses
the size n array, each iteration of the outer loop sets a key variable to with array at index i and then the
inner loop tries to find the location in the array from 0 to i - 1 to place the key.
For our implementation, you will be using iterators not indices, thus i would be an iterator along with j and
the key, then j iterates through all of the elements to the left of i and when the correct spot is found, you
splice out key from the linked list and call the insertion(j, key) function to put the key into the correct
position.

Merge Sort Main


Recall from lecture the merge sort algorithm, you initially pass in the array into the merge sort function,
you first have to split the array into two pieces, so you will need to create an iterator that is set to the head
node, to the tail node, and one to the middle of the list, then call two merge sort functions and pass in the
array and the front and middle iterators into the first function call and for the second function call, pass in
the list, the middle, and end iterators. The result of each function would be to return two sorted arrays.
Below is the function prototype

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

You should output a sorted list

g ++ merge_sort . cpp
./ a . out < input01 . txt

You should ohtput a sorted list

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

You might also like