NP2000

removecurrent and copy(copyMe)

Dec 3rd, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. // Code for remove_current()
  2.  
  3. void Sequence::remove_current() {
  4.         if (is_item()) {
  5.  
  6.             node* temp;
  7.             temp = cursor;
  8.            
  9.             if (cursor == headPtr && cursor == tailPtr) {
  10.                 cursor = nullptr;
  11.                 precursor = nullptr;
  12.                 headPtr = nullptr;
  13.                 tailPtr = nullptr;
  14.             }
  15.             else if (cursor == headPtr) {
  16.                 headPtr = cursor->next;
  17.                 cursor = headPtr;
  18.                 precursor = nullptr;
  19.             }
  20.             else {
  21.                 if (cursor == tailPtr) {
  22.                     tailPtr = precursor;
  23.                     precursor = nullptr;
  24.                     cursor = nullptr;
  25.                 }
  26.                 else {      
  27.                     precursor->next = cursor->next;
  28.                     cursor = cursor->next;
  29.                    
  30.                 }    
  31.             }
  32.             delete temp;
  33.             numitems--;
  34.         }
  35.     }
  36.  
  37. // Code for copy(copyMe)
  38.  
  39. void Sequence::copy(const Sequence& copyMe) {
  40.  
  41.         numitems = 0;
  42.  
  43.         if (copyMe.headPtr == nullptr) {
  44.             cursor = precursor = headPtr = tailPtr = nullptr;
  45.         }
  46.         else {
  47.  
  48.             // allocate a new node for the new sequence
  49.             node* newPtr = new node;
  50.             newPtr -> data = copyMe.headPtr -> data;
  51.             numitems++;
  52.  
  53.             // start the new sequence with this node
  54.             headPtr = newPtr;
  55.             precursor = nullptr;
  56.             cursor = headPtr;
  57.             tailPtr = headPtr;
  58.  
  59.             // create a node to traverse the origin sequence
  60.             node* originPtr = copyMe.headPtr -> next;
  61.  
  62.             while (originPtr != nullptr) {
  63.                
  64.                 // add node to the new list, make it the current, & assign data
  65.                 newPtr->next = new node;
  66.                 newPtr = newPtr->next;
  67.                 newPtr->data = originPtr->data;
  68.                 numitems++;
  69.                
  70.            
  71.                 // Correct cursor and precursor positions
  72.                 if(originPtr == copyMe.cursor) {
  73.                     this->cursor = newPtr;
  74.                 }
  75.                 if (originPtr == copyMe.precursor) {
  76.                     this->precursor = newPtr;
  77.                 }
  78.                 originPtr = originPtr->next;
  79.  
  80.                 if (originPtr != nullptr) {
  81.                     cout << "newPtr is: " << newPtr << " and originPtr is: " << originPtr << endl;
  82.                 }
  83.                 else {
  84.                     cout << "NULL REACHED" << endl;
  85.                 }
  86.                
  87.  
  88.                 // if the thing being copied is the last thing, make it tailptr    
  89.                 if (originPtr == nullptr) {
  90.                    
  91.                     // tailPtr = newPtr->next;
  92.                     tailPtr = newPtr;
  93.                     cout << "tail assigned is: " << tailPtr->data<< endl;
  94.                    
  95.                     newPtr->next = nullptr;
  96.                     originPtr = nullptr;
  97.                 }        
  98.             }    
  99.         }
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment