#Linked List Functions

5 messages · Page 1 of 1 (latest)

wise tendon
#

Need help with implementing some Linked List functions. Attached below are all my files. My main concerns are with the assign and the reverse function. Haven't had the chance to try splice yet so if anyone has tips for that as well please help!

I will attach snippets for those who don't want to dig through the files here:

in header (defined lines 77-81, 107-111):

  // ex. my_list.assign(5, 3)
  // should produce a list containing <3, 3, 3>
  // Any existing data should be safely removed if necessary
  void assign(int count, const T &value);

// Reverse the order of elements in the list
  // The sentinel node should still be at the end (tail) of the reversed list
  // COMMON INTERVIEW CHALLENGE
  // Can be done with clever manipulation of pointers, no new/delete needed!
  void reverse();

In .hpp starting line 131:


    clear();

    head = new ListNode<T>;
    head -> data = value;
    ListNode<T> *currentNode = head;

    for(int i = 1; i < count; i++) {

        ListNode<T> *newNode = new ListNode<T>;
        newNode->data = value;
        currentNode->next = newNode;
        currentNode = newNode;
    }

    currentNode->next = nullptr;
    num_elements = count;

}

template <typename T> void MyList<T>::reverse() {

    ListNode<T> *currentNode = head;
    ListNode<T> *nextNode = nullptr;
    ListNode<T> *prevNode = nullptr;

    while(currentNode != nullptr){
        nextNode = currentNode -> next;
        currentNode -> next = prevNode;
        prevNode = currentNode;
        currentNode = nextNode;
    }

    head = prevNode;

}

dusty anchorBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

wise tendon
#

List and Iterator definitions:

public:
  T data;

  ListNode<T> *next;
  // ListNode<T> *prev;
  // un-comment the line above if you'd like to attempt a doubly-linked list
  // Warning: will require you to re-write some provided functions
};

template <typename T> class LinkedListIterator {
  ListNode<T> *ptr;

public:
  LinkedListIterator(ListNode<T> *p) { ptr = p; } // constructor
  T &operator*() {
    return ptr->data;
  } // de-referencing iterator gives access to the data in the current node
  ListNode<T> *operator->() { return ptr; }
  LinkedListIterator<T> &operator+(int increment) {
    int hops = 0;
    while (hops < increment && ptr != nullptr) {
      ptr = ptr->next; // "advance" the pointer to next node
      hops++;
    }
    return *this;
  }
  LinkedListIterator<T> &operator++() { // pre-fix increment
    ptr = ptr->next;
    return *this;
  }
  LinkedListIterator<T> operator++(int) { // post-fix increment
    LinkedListIterator<T> temp = *this;
    ptr = ptr->next;
    return temp;
  }
  bool is_null() { return ptr == nullptr; }
};
weak nacelle
#

So what are the concerns

wise tendon
#

!solved