For this micro assignment, you must implement two Linked List functions. We will use the following
example Linked List:
2 0 -1 5 7
getElementAt(index)
This function should return the element (i.e. value) of the Nth item inside the linked list. For example,
on the Linked List above, getElementAt(0) should return 2; getElementAt(3) should return 5.
addElementAt(value, location)
This function should insert a new value at the given location. Note that the location supplied must be
within bounds of the LinkedList. For example, we cannot call addElementAt(4, 11) on the above Linked
List because 11 is beyond the size of the Linked List.
Here are some examples. If we call addElementAt(0, 1), the above Linked List would now look like:
1 2 0 -1 5 7
If we again call addElementAt(2, 123), we would get:
1 2 123 0 -1 5 7
Grading
Your submission will be graded based on the following:
1. [7] Your solution does not cause any runtime issues and your file passes all test cases
2. [3] Your code contains good style. For example,
You provide meaningful variable names
You provide sufficient and meaningful comments
Your code is well structured
@@@@@@@@@@@@@@@@@@@@@@@@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include
#include
#include "LinkedListNode.h"
#include
using namespace std;
template
class LinkedList
{
private:
//points to the front of the linked list
LinkedListNode *_front = nullptr;
//keeping track of size in a variable eliminates need to continually
//count LL boxes.
int _size = 0;
protected:
//creates a new LinkedListNode for us
virtual LinkedListNode *createNode(T value)
{
return new LinkedListNode < T > { value };
}
public:
//default constructor
LinkedList()
{
_front = nullptr;
}
//copy constructor
LinkedList(const LinkedList &other)
{
for (int i = 0; i < other.getSize(); i++)
{
addElement(other.getElementAt(i));
}
}
//move constructor
LinkedList(LinkedList &&other)
{
//take other's data
_front = other._front;
_size = other._size;
//reset other's pointers
other._front = nullptr;
}
//initializer list constructor
LinkedList(initializer_list values)
{
for (auto item : values)
{
addElement(item);
}
}
//Always remember to clean up pointers in destructor!
virtual ~LinkedList()
{
LinkedListNode *current = _front;
while (current != nullptr)
{
LinkedListNode *temp = current->getNext();
delete current;
current = temp;
}
}
//will return true if the LL is empty.
virtual bool isEmpty() const
{
return _size == 0;
}
//returns the size of the LL.
virtual int getSize() const
{
return _size;
}
.
For this micro assignment, you must implement two Linked List functi.docx
1. For this micro assignment, you must implement two Linked List
functions. We will use the following
example Linked List:
2 0 -1 5 7
getElementAt(index)
This function should return the element (i.e. value) of the Nth
item inside the linked list. For example,
on the Linked List above, getElementAt(0) should return 2;
getElementAt(3) should return 5.
addElementAt(value, location)
This function should insert a new value at the given location.
Note that the location supplied must be
within bounds of the LinkedList. For example, we cannot call
addElementAt(4, 11) on the above Linked
List because 11 is beyond the size of the Linked List.
Here are some examples. If we call addElementAt(0, 1), the
above Linked List would now look like:
1 2 0 -1 5 7
If we again call addElementAt(2, 123), we would get:
1 2 123 0 -1 5
7
2. Grading
Your submission will be graded based on the following:
1. [7] Your solution does not cause any runtime issues and your
file passes all test cases
2. [3] Your code contains good style. For example,
ble names
@@@@@@@@@@@@@@@@@@@@@@@@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include
#include
#include "LinkedListNode.h"
#include
using namespace std;
3. template
class LinkedList
{
private:
//points to the front of the linked list
LinkedListNode *_front = nullptr;
//keeping track of size in a variable eliminates need to
continually
//count LL boxes.
int _size = 0;
protected:
//creates a new LinkedListNode for us
virtual LinkedListNode *createNode(T value)
4. {
return new LinkedListNode < T > { value };
}
public:
//default constructor
LinkedList()
{
_front = nullptr;
}
//copy constructor
LinkedList(const LinkedList &other)
{
for (int i = 0; i < other.getSize(); i++)
{
addElement(other.getElementAt(i));
6. {
addElement(item);
}
}
//Always remember to clean up pointers in destructor!
virtual ~LinkedList()
{
LinkedListNode *current = _front;
while (current != nullptr)
{
LinkedListNode *temp = current->getNext();
delete current;
current = temp;
}
}
//will return true if the LL is empty.
7. virtual bool isEmpty() const
{
return _size == 0;
}
//returns the size of the LL.
virtual int getSize() const
{
return _size;
}
//adds the supplied item to the end of our LL
virtual void addElement(T value)
{
addElementAt(value, getSize());
}
//Returns the value of the LinkedListNode at the given index
8. virtual T& getElementAt(int index)
{
//MA #1 TODO: ACTUALLY IMPLEMENT!
**************** add here
int value = -1;
return value;
}
//adds the specified item at the specified index and shifts
everything else
//to the "right" by one.
virtual void addElementAt(T value, int location)
{
LinkedListNode *new_value = createNode(value);
//MA #1 TODO: IMPLEMENT! ************** and
here
// Add variable new_value to proper location inside
// our linked list.
9. }
};
#endif // !LINKED_LIST_H
@@@@@@@@@@@@@@@@@@@@@@@
#ifndef LINKED_LIST_NODE_H
#define LINKED_LIST_NODE_H
//A linked list node represents a single "box" inside a lined list.
In this
//scheme, the LinkedList is simply a collection of
LinkedListNode boxes.
template
class LinkedListNode
{
protected:
10. //value that our box contains
T _value;
//pointer to next node in the LL sequence
LinkedListNode *_next;
public:
//constructor must accept a default value
LinkedListNode(const T &value) : _value(value)
{
_next = nullptr;
}
LinkedListNode()
{
_next = nullptr;
}
11. //copy constructor prevents premature deletion of next
pointer
LinkedListNode(const LinkedListNode &other)
{
_value = other.getValue();
_next = other.getNext();
}
virtual ~LinkedListNode()
{
}
//copy operator allows us to reassign previously created
list nodes
LinkedListNode &operator=(const LinkedListNode
&other)
{
if (this != &other)
12. {
LinkedListNode temp(other);
swap(*this, temp);
}
return *this;
}
//returns a pointer to the next list node in the sequence
LinkedListNode *getNext()
{
return _next;
}
//sets the pointer to the next node in the sequence
void setNext(LinkedListNode *next)
{
_next = next;
}
13. //returns the value of the list node
T &getValue()
{
return _value;
}
//constant version of the getter
const T& getValue() const
{
return _value;
}
//sets the value of the current list node
void setValue(const T &value)
{
_value = value;
}