chain.cpp - #include\"chain.h#include\"chain_given.cpp PA1 functions Destroys the current Chain This function should ensure that memory does not leak on

chain.cpp - #include"chain.h#include"chain_given.cpp PA1...

This preview shows page 1 - 2 out of 4 pages.

#include "chain.h"#include "chain_given.cpp"// PA1 functions/*** Destroys the current Chain. This function should ensure that* memory does not leak on destruction of a chain.*/Chain::~Chain(){/* your code here */clear();delete head_;}/*** Inserts a new node at the end of the List.* This function **SHOULD** create a new ListNode.** @param ndata The data to be inserted.*/void Chain::insertBack(const Block & ndata){Node * new_node_ = new Node(ndata);// In relation to head_head_->prev->next = new_node_;new_node_->prev = head_->prev;head_->prev = new_node_;new_node_->next = head_;length_++;}/*** Modifies the Chain by moving the subchain of len Nodes,* starting at position startPos, dist positions toward the* end of the chain. If startPos + len - 1 + dist > length* then dist = length - startPos - len + 1 (to prevent gaps* in the new chain).* The subchain occupies positions (startPos + dist) through* (startPos + dist + len - 1) of the resulting chain.* The order of subchain nodes is not changed in the move.* You may assume that: 1 <= startPos <= length - len + 1,* 0 <= dist <= length, and 0 <= len <= length. */void Chain::moveBack(int startPos, int len, int dist){if (dist == 0)return;if (startPos + len - 1 + dist > length_) {dist = length_ - startPos - len + 1;}Node* startSubchain = walk(head_, startPos);Node* endSubchain = walk(startSubchain, len - 1);Node* endPosition = walk(endSubchain, dist);// Re-orderingstartSubchain->prev->next = endSubchain->next;
Background image
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture