This is a working linked list that will be used as a vector. Instead of using dynamic arrays, I am using a linked list. I would like to see a more concise solution without utilizing the STL.
SimpleVector.h
class SimpleVector
{
public:
SimpleVector(){
head = nullptr;
}
SimpleVector(const SimpleVector &);
~SimpleVector();
void appendNode(const int value);
void insertNode(const int value);
int getElementAt(const int index);
void displayVect();
private:
struct Node
{
int value;
Node *next;
};
Node *head;
};
Copy constructor implementation
SimpleVector::SimpleVector(const SimpleVector &obj)
{
Node *oldNodePtr;
Node *newNodePtr;
// Initialize head to nullptr incase obj is empty
this->head = nullptr;
// Position oldNodePtr to the head of the list.
oldNodePtr = obj.head;
if (oldNodePtr != nullptr) {
// Create new node. This is the first node in the new list
Node *newNode = new Node;
newNode->value = oldNodePtr->value;
// Set head to new node address
this->head = newNode;
newNodePtr = newNode;
oldNodePtr = oldNodePtr->next;
// While oldNodePtr points to a node, traverse the list.
while (oldNodePtr->next != nullptr) {
Node *newNode = new Node;
newNode->value = oldNodePtr->value;
newNodePtr->next = newNode;
newNodePtr = newNodePtr->next;
oldNodePtr = oldNodePtr->next;
}
Node *lastNode = new Node;
lastNode->value = oldNodePtr->value;
newNodePtr->next = lastNode;
lastNode = nullptr;
}
}
this->head
for example. So next time when only posting a method also include a section with the member variables so we can see their types. \$\endgroup\$