chain-2.cpp - #include\"chain.h#include\"chain_given.cpp PA1 functions Chain:~Chain)cfw clear length = 0 void Chain:insertFront(const Block ndata)cfw Node

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

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

#include "chain.h" #include "chain_given.cpp" // PA1 functions Chain::~Chain(){ clear(); length_ = 0; } void Chain::insertFront(const Block & ndata){ Node* newNode = new Node(ndata); if (length_ == 0) { tail_ = newNode; head_ = newNode; } else { newNode->next = head_; newNode->prev = NULL; head_->prev = newNode; head_ = newNode; } length_++; } void Chain::insertBack(const Block & ndata){ Node* newNode = new Node(ndata); if (length_ == 0) { tail_ = newNode; head_ = newNode; } else { newNode->prev = tail_; newNode->next = NULL; tail_->next = newNode; tail_ = newNode; } length_++; } void Chain::checkeredSwap(Chain &other) { Node *cur1 = head_; Node *cur1n = cur1->next; Node *cur2 = other.head_; Node *cur2n = cur2->next; while (cur1 != NULL && cur2 != NULL && cur1n != NULL && cur2n != NULL) { Node *temp = cur2n->next; cur1->next = cur2n; cur2n->prev = cur1; cur2n->next = cur1n->next; cur2->next = cur1n; cur1n->prev = cur2; cur1n->next = temp; cur1 = cur2n->next; cur2 = cur1n->next; if (cur1 != NULL && cur2 != NULL) { cur1->prev = cur2n; cur2->prev = cur1n; cur1n = cur1->next; cur2n = cur2->next; } else {
Image of page 1
tail_ = cur2n; other.tail_ = cur1n; } } } const Block* Chain::getBlock(int index) { if (index < 0 || index > length_) { cout << "Block is outside range" << endl; return NULL;
Image of page 2
Image of page 3

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture