FOR DR. GEOFREY ONLY

profileMesfer almesfer

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 <stdexcept>
#include <initializer_list>
#include "LinkedListNode.h"
#include <utility>
using namespace std;

template <typename T>
class LinkedList
{
private:

	//points to the front of the linked list
	LinkedListNode<T> *_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<T> *createNode(T value)
	{
		return new LinkedListNode < T > { value };
	}

public:

    //default constructor
	LinkedList()
	{
		_front = nullptr;
	}

	//copy constructor
	LinkedList(const LinkedList<T> &other)
	{
		for (int i = 0; i < other.getSize(); i++)
		{
			addElement(other.getElementAt(i));
		}
	}

	//move constructor
	LinkedList(LinkedList<T> &&other)
	{
	    //take other's data
		_front = other._front;
		_size = other._size;

		//reset other's pointers
		other._front = nullptr;
	}

	//initializer list constructor
	LinkedList(initializer_list<T> values)
	{
		for (auto item : values)
		{
			addElement(item);
		}
	}

	//Always remember to clean up pointers in destructor!
	virtual ~LinkedList()
	{
		LinkedListNode<T> *current = _front;
		while (current != nullptr)
		{
			LinkedListNode<T> *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;
    }

    //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
    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<T> *new_value = createNode(value);

		//MA #1 TODO: IMPLEMENT! ************** and here 
		// Add variable new_value to proper location inside
		// our linked list.

	}
};

#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 <typename T>
class LinkedListNode
{
protected:

	//value that our box contains
	T _value;

	//pointer to next node in the LL sequence
	LinkedListNode<T> *_next;

public:

	//constructor must accept a default value
	LinkedListNode(const T &value) : _value(value)
	{
		_next = nullptr;
	}

	LinkedListNode()
	{
		_next = nullptr;
	}

	//copy constructor prevents premature deletion of next pointer
	LinkedListNode(const LinkedListNode<T> &other)
	{
		_value = other.getValue();
		_next = other.getNext();
	}

	virtual ~LinkedListNode()
	{

	}

	//copy operator allows us to reassign previously created list nodes
	LinkedListNode<T> &operator=(const LinkedListNode<T> &other)
	{
		if (this != &other)
		{
			LinkedListNode<T> temp(other);
			swap(*this, temp);
		}
		return *this;
	}

	//returns a pointer to the next list node in the sequence
	LinkedListNode<T> *getNext()
	{
		return _next;
	}

	//sets the pointer to the next node in the sequence
	void setNext(LinkedListNode<T> *next)
	{
		_next = next;
	}

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

#endif

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

#include <iostream>
#include <string>
#include "LinkedList.h"

using namespace std;

void linkedListTest()
{
    cout << "***Linked List Test***" << endl;
    LinkedList<int> test_list{};

    test_list.addElementAt(0, 1);
    test_list.addElementAt(1, 2);
    test_list.addElementAt(0, 3);
    test_list.addElementAt(2, 4);
    cout << "Number of elements in LL: " << test_list.getSize() << " (expected: 4)" << endl;
    cout << "Value at 0: " << test_list.getElementAt(0) << " (expected: 3)" << endl;
    cout << "Value at 1: " << test_list.getElementAt(1) << " (expected: 1)" << endl;
    cout << "Value at 2: " << test_list.getElementAt(2) << " (expected: 4)" << endl;
    cout << "Value at 3: " << test_list.getElementAt(3) << " (expected: 2)" << endl;
}

int main()
{
    linkedListTest();
}


    • 10 years ago
    • 6
    Answer(1)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      linked_lists.txt