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 5 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*//////////////////// length_ = 0; clear(); } /** * Inserts a new node at the back of the List. * This function **SHOULD** create a new ListNode. * * @param ndata The data to be inserted. */ void Chain::insertBack(const Block & ndata){ /*your code here*/ if (length_ == 0) { width_ = ndata.width(); height_ = ndata.height(); } else if (ndata.width() != width_ || ndata.height() != height_) { return; //If dimensions of block to insert do not match, return and do nothing } Node* new_node = new Node(ndata); Node* last_node = tail_->prev; //Insert new node: last_node->next = new_node; new_node->prev = last_node; tail_->prev = new_node; new_node->next = tail_; length_++; //Increment chain length } /** * Modifies the Chain by moving a contiguous subset of len Nodes * dist nodes toward the end of the chain beginning from startPos * (maintaining the sentinel at the end). Their order is * not changed in the move. You may assume that the startPos and * len parameters are kind: startPos + len -1 <= length. If there * are not enough nodes to shift by dist, just shift as many as * possible. */ void Chain::moveBack(int startPos, int len, int dist){ /*your code here*/ if(startPos + len > length_||dist == 0||length_==0){ return;} else if(startPos + dist > length_){ return;} if (startPos == 0) { startPos = 1; len--; } Node * before = walk(head_,startPos-1); // node before startPos

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture