SlideShare a Scribd company logo
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,
ble names
@@@@@@@@@@@@@@@@@@@@@@@@
#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;
}
//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 *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
class LinkedListNode
{
protected:
//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;
}
//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)
{
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;
}
//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
#include
#include "LinkedList.h"
using namespace std;
void linkedListTest()
{
cout << "***Linked List Test***" << endl;
LinkedList 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();
}

More Related Content

PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
PDF
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
PDF
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
PDF
How do I fix it in javaLinkedList.java Defines a doubl.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
could you implement this function please, im having issues with it..pdf
DOCX
C++ Please write the whole code that is needed for this assignment- wr.docx
This assignment and the next (#5) involve design and development of a.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
could you implement this function please, im having issues with it..pdf
C++ Please write the whole code that is needed for this assignment- wr.docx

Similar to For this micro assignment, you must implement two Linked List functi.docx (20)

PDF
To complete the task, you need to fill in the missing code. I’ve inc.pdf
PDF
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
PDF
Consider a double-linked linked list implementation with the followin.pdf
PDF
This is problem is same problem which i submitted on 22017, I just.pdf
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PPTX
Link List Programming Linked List in Cpp
PDF
Lab-2.4 101.pdf
PDF
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
PDF
Write a function to merge two doubly linked lists. The input lists ha.pdf
PDF
File LinkedList.java Defines a doubly-l.pdf
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
To complete the task, you need to fill in the missing code. I’ve inc.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
Hi,I have added the methods and main class as per your requirement.pdf
Consider a double-linked linked list implementation with the followin.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Link List Programming Linked List in Cpp
Lab-2.4 101.pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
Data Structures in C++I am really new to C++, so links are really .pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdf
File LinkedList.java Defines a doubly-l.pdf
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Ad

More from mckellarhastings (20)

DOCX
Conduct an internet search finding a job you are interested (HR .docx
DOCX
Conduct an Internet or library search for information on The Bay of.docx
DOCX
Conduct an internet search about the murder of Yeardley Love. After .docx
DOCX
At this point, you’ve organized your HR project team and you are.docx
DOCX
At the beginning of 2012, the Jeater company had the following balan.docx
DOCX
At many different points throughout the collection Born a Crime, Tre.docx
DOCX
At least 200 wordss or more per question. Answer UNDER question. And.docx
DOCX
At least 200 words per question. Chapter 11The Idea .docx
DOCX
At least 150 words each. Use a reference for each question and us.docx
DOCX
At least 250 words per question. Chapter 11The Idea of Craft A.docx
DOCX
At its core, pathology is the study of disease. Diseases occur for m.docx
DOCX
assumptions people make about this topic (homelessness, immigration,.docx
DOCX
At age 12, Freeman Hrabowski marched with Martin Luther King. Now he.docx
DOCX
At each of the locations listed below, there is evidence of plat.docx
DOCX
Assume you hold the Special Agent in Charge role of the Joint .docx
DOCX
Assume you are a DFI and you must deliver a presentation to the Stat.docx
DOCX
Assume that you work for the District Board of Education. The Direct.docx
DOCX
Assume that you have been tasked by your employer to develop an inci.docx
DOCX
Assume that you generate an authenticated and encrypted message by f.docx
DOCX
Assume that you are in your chosen criminal justice profession, .docx
Conduct an internet search finding a job you are interested (HR .docx
Conduct an Internet or library search for information on The Bay of.docx
Conduct an internet search about the murder of Yeardley Love. After .docx
At this point, you’ve organized your HR project team and you are.docx
At the beginning of 2012, the Jeater company had the following balan.docx
At many different points throughout the collection Born a Crime, Tre.docx
At least 200 wordss or more per question. Answer UNDER question. And.docx
At least 200 words per question. Chapter 11The Idea .docx
At least 150 words each. Use a reference for each question and us.docx
At least 250 words per question. Chapter 11The Idea of Craft A.docx
At its core, pathology is the study of disease. Diseases occur for m.docx
assumptions people make about this topic (homelessness, immigration,.docx
At age 12, Freeman Hrabowski marched with Martin Luther King. Now he.docx
At each of the locations listed below, there is evidence of plat.docx
Assume you hold the Special Agent in Charge role of the Joint .docx
Assume you are a DFI and you must deliver a presentation to the Stat.docx
Assume that you work for the District Board of Education. The Direct.docx
Assume that you have been tasked by your employer to develop an inci.docx
Assume that you generate an authenticated and encrypted message by f.docx
Assume that you are in your chosen criminal justice profession, .docx
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Presentation on HIE in infants and its manifestations
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
master seminar digital applications in india
Chinmaya Tiranga quiz Grand Finale.pdf
Complications of Minimal Access Surgery at WLH
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
01-Introduction-to-Information-Management.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Presentation on HIE in infants and its manifestations
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
A systematic review of self-coping strategies used by university students to ...
VCE English Exam - Section C Student Revision Booklet
Microbial disease of the cardiovascular and lymphatic systems
102 student loan defaulters named and shamed – Is someone you know on the list?
STATICS OF THE RIGID BODIES Hibbelers.pdf

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));
  • 5. } } //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)
  • 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; }
  • 14. }; #endif @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ #include #include #include "LinkedList.h" using namespace std; void linkedListTest() { cout << "***Linked List Test***" << endl; LinkedList test_list{}; test_list.addElementAt(0, 1); test_list.addElementAt(1, 2); test_list.addElementAt(0, 3);
  • 15. 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(); }