#include "chain.h"
#include "chain_"
// 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 a...
include chainh include chaingivencpp pa1 functions destroys the current ch
Written for
CPSC 221
All documents for this subject (1)
Seller
Follow
ExamsConnoisseur
Reviews received
Content preview
#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
}
/**
* 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
Node * first = walk(head_, startPos); // node at startPos
Node * end = walk(first,len-1); // last node of subset
This study source was downloaded by 100000850872992 from CourseHero.com on 02-14-2023 22:10:52 GMT -06:00
, //if not enough nodes to shift by dist, update new end
while(walk(end,dist)->next==NULL){
end=end->prev;
}
Node * after = walk(end,1); // node after last node of subset
Node * newbefore = walk(end,dist);
Node * newafter = walk(end,dist+1);
/**
* Rolls the current Chain by k nodes: removes the last
* k nodes from the Chain and attaches them, in order, at
* the start of the chain (maintaining the sentinel at the front).
*/
void Chain::roll(int k){
/*your code here*/
if(k==0 || k> length_){return;} //include the guards?
else{
Node * subhead = walk(head_, length_-k+1);
Node * subtail = walk(head_, length_);
Node * front1 = walk(head_, 1);
subhead->prev->next=tail_;
tail_->prev = subhead->prev;
head_->next = subhead;
subhead->prev = head_;
front1->prev = subtail;
subtail->next= front1;
}
}
/**
* Modifies the current chain by reversing the subchain
* between pos1 and pos2, inclusive. The positions are
* 1-based. You may assume that pos1 and pos2 are valid
* locations in the Chain, and that pos2 >= pos1.
*/
void Chain::reverseSub(int pos1, int pos2){
/*your code here */
if(pos1==pos2){return;}
Node* front = walk(head_,pos1);
Node* before = front->prev;
Node* end = walk(head_,pos2);
Node* after = end-> next;
Node* curr= end;
Node* pcurr = before;
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller ExamsConnoisseur. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $6.49. You're not tied to anything after your purchase.