Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code for remove_current()
- void Sequence::remove_current() {
- if (is_item()) {
- node* temp;
- temp = cursor;
- if (cursor == headPtr && cursor == tailPtr) {
- cursor = nullptr;
- precursor = nullptr;
- headPtr = nullptr;
- tailPtr = nullptr;
- }
- else if (cursor == headPtr) {
- headPtr = cursor->next;
- cursor = headPtr;
- precursor = nullptr;
- }
- else {
- if (cursor == tailPtr) {
- tailPtr = precursor;
- precursor = nullptr;
- cursor = nullptr;
- }
- else {
- precursor->next = cursor->next;
- cursor = cursor->next;
- }
- }
- delete temp;
- numitems--;
- }
- }
- // Code for copy(copyMe)
- void Sequence::copy(const Sequence& copyMe) {
- numitems = 0;
- if (copyMe.headPtr == nullptr) {
- cursor = precursor = headPtr = tailPtr = nullptr;
- }
- else {
- // allocate a new node for the new sequence
- node* newPtr = new node;
- newPtr -> data = copyMe.headPtr -> data;
- numitems++;
- // start the new sequence with this node
- headPtr = newPtr;
- precursor = nullptr;
- cursor = headPtr;
- tailPtr = headPtr;
- // create a node to traverse the origin sequence
- node* originPtr = copyMe.headPtr -> next;
- while (originPtr != nullptr) {
- // add node to the new list, make it the current, & assign data
- newPtr->next = new node;
- newPtr = newPtr->next;
- newPtr->data = originPtr->data;
- numitems++;
- // Correct cursor and precursor positions
- if(originPtr == copyMe.cursor) {
- this->cursor = newPtr;
- }
- if (originPtr == copyMe.precursor) {
- this->precursor = newPtr;
- }
- originPtr = originPtr->next;
- if (originPtr != nullptr) {
- cout << "newPtr is: " << newPtr << " and originPtr is: " << originPtr << endl;
- }
- else {
- cout << "NULL REACHED" << endl;
- }
- // if the thing being copied is the last thing, make it tailptr
- if (originPtr == nullptr) {
- // tailPtr = newPtr->next;
- tailPtr = newPtr;
- cout << "tail assigned is: " << tailPtr->data<< endl;
- newPtr->next = nullptr;
- originPtr = nullptr;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment