Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

chain.cpp University of British Columbia CPSC 221

Rating
-
Sold
-
Pages
5
Grade
A+
Uploaded on
15-02-2023
Written in
2022/2023

#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 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_ = (); height_ = t(); } else if (() != width_ || t() != 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) {

Show more Read less
Institution
Course

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
}

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
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


https://www.coursehero.com/file/34809024/chaincpp/

, //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);

before->next = after;
after->prev = before;
newbefore ->next= first;
first->prev= newbefore;
end->next= newafter;
newafter->prev=end;


}


/**
* 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;

front->next = after;
after->prev=front;

while ( curr!= before) {
Node* temp = curr->prev;
pcurr->next=curr;
curr->prev=pcurr;

This study source was downloaded by 100000850872992 from CourseHero.com on 02-14-2023 22:10:52 GMT -06:00


https://www.coursehero.com/file/34809024/chaincpp/

Written for

Course

Document information

Uploaded on
February 15, 2023
Number of pages
5
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$6.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
ExamsConnoisseur Self
Follow You need to be logged in order to follow users or courses
Sold
587
Member since
3 year
Number of followers
344
Documents
1492
Last sold
1 week ago

4.2

68 reviews

5
40
4
11
3
13
2
1
1
3

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions