/ Published in: C++
This version is complete version
Expand |
Embed | Plain Text
// This is the implementation file /********************************************************************************************* USE TEMPLATE FUNCTION AND SHORT CUT WORKS !! PTEMLATE FUNCTION MANIPULATE ALMOST EVERYTHING FOR US !! **********************************************************************************************/ #include <iostream> #include <iomanip> #include <cassert> //#include "node2.h" #include "sequence4.h" using namespace std; namespace CISP430_A3 { // TEMPLATE CLASS PROVIDED: // sequence<Item> (part of the namespace CISP430_A3) // This is a container class for a sequence of items, // where each List may have a designated item called the current item). // The template parameter <value_type> is the data type of the items // in the List. It may be any of the C++ built-in types (int, char, etc.), // or a class with a default constructor, an assignment operator, // and a copy constructor. // // TYPEDEFS and MEMBER CONSTANTS for the sequence class: // typename sequence<Item>::value_type // Thisis the data type of the items in the sequence. It // may be any of the C++ built-in types (int, char, etc.), or a class with a // default constructor, an assignment operator, and a copy constructor. // // typename sequence<Item>::size_type // This is the data type of any variable that keeps track of // how many items are in a sequence. // // CONSTRUCTOR for the sequence<Item> class: template <class Item> sequence<Item>::sequence( ) // Postcondition: The sequence has been initialized as an empty sequence. { head_ptr = NULL; precursor = NULL; tail_ptr = NULL; cursor = NULL; many_nodes = 0; } //COPY CONSTRACTOR template <class Item> sequence<Item>::sequence(const sequence& source) { operator =(source); } // DESTRACTOR template <class Item> sequence<Item>::~sequence( ) { list_clear(head_ptr); many_nodes = NULL; } // MODIFICATION MEMBER FUNCTIONS for the sequence<Item> class: // START ALSO GOOD template <class Item> void sequence<Item>::start( ) // Postcondition: The first item on the sequence becomes the current item // (but if the sequence is empty, then there is no current item). { cursor = head_ptr; precursor = NULL; } template <class Item> void sequence<Item>::advance( ) // Precondition: is_item returns true. // Postcondition: If the current item was already the last item in the // sequence, then there is no longer any current item. Otherwise, the new // current item is the item immediately after the original current item. { if(!is_item()) return; precursor = cursor; if(cursor == tail_ptr) { cursor = NULL; } else { cursor = cursor->link(); } } // INSERT SEMMS RIGHT SO FAR template <class Item> void sequence<Item>::insert(const value_type& entry) // Postcondition: A new copy of entry has been inserted in the sequence // before the current item. If there was no current item, then the new entry // has been inserted at the front of the sequence. In either case, the newly // inserted item is now the current item of the sequence. // YOU NEED TO USE list_copy() TO COPY ENTIRE LIST TO NEW PLACE WHEN YOU INSERT NEW ITEM IN THE MIDDLE OF SEQUENCE !! // WHICH MEANS YOU NEED TO CREATE FO LOOP PART HERE. // BE CAREFUL !! THE PARAMETER RECEVE ENTRY AS VALUE TYPE NOT ITEM { node<Item> *target_ptr = new node<Item>(entry); if((cursor == NULL) && (head_ptr != NULL)) { target_ptr->set_link(head_ptr); cursor = target_ptr; precursor = NULL; head_ptr = target_ptr; } else if((cursor != NULL) && (precursor!= NULL)) { precursor->set_link(target_ptr); target_ptr->set_link(cursor); } else if((cursor != NULL) && (cursor == head_ptr)) { target_ptr->set_link(cursor); head_ptr = target_ptr; precursor = NULL; head_ptr = target_ptr; } else { tail_ptr = target_ptr; head_ptr = tail_ptr; } cursor = target_ptr; many_nodes++; } template <class Item> void sequence<Item>::attach(const value_type& entry) // Postcondition: A new copy of entry has been inserted in the sequence after // the current item. If there was no current item, then the new entry has // been attached to the end of the sequence. In either case, the newly // inserted item is now the current item of the sequence. { node<Item> *target_tpr = new node<Item>(entry); if((cursor == NULL) && (tail_ptr != NULL)) { tail_ptr->set_link(target_tpr); list_head_insert(tail_ptr,target_tpr->data()); } else if(((cursor != NULL) && (cursor == tail_ptr)) ||((cursor != NULL) && (cursor == head_ptr) && (cursor == tail_ptr))) { precursor = cursor; cursor->set_link(target_tpr); tail_ptr = target_tpr; cursor = target_tpr; many_nodes++; } else if(((cursor != NULL) && (cursor == head_ptr)) ||((cursor != tail_ptr) && (cursor != head_ptr))) { list_head_insert(precursor,cursor->data()); target_tpr->set_link(cursor->link()); cursor->set_link(target_tpr); many_nodes++; } else { tail_ptr = target_tpr; head_ptr = tail_ptr; many_nodes++; } cursor =target_tpr; } template <class Item> void sequence<Item>::remove_current( ) // Precondition: is_item returns true. // Postcondition: The current item has been removed from the sequence, and // the item after this (if there is one) is now the new current item. { if(!is_item()) return; if((cursor != tail_ptr) && (cursor != head_ptr)){ precursor->set_link(cursor->link()); list_head_remove(cursor); cursor = precursor->link(); } else if((cursor != tail_ptr) && (cursor == head_ptr)) { head_ptr = cursor->link(); list_head_remove(cursor); cursor = head_ptr; } else if((cursor == tail_ptr) && (precursor != NULL)) { precursor->set_link(NULL); list_head_remove(cursor); cursor = NULL; tail_ptr = precursor; } else if((cursor == tail_ptr) && (cursor == head_ptr)) { list_head_remove(cursor); head_ptr = NULL; tail_ptr = NULL; cursor = NULL; } many_nodes--; } // CONSTANT MEMBER FUNCTIONS for the sequence<Item> class: template <class Item> typename sequence<Item>::value_type sequence<Item>::current( ) const // Precondition: is_item( ) returns true. // Postcondition: The item returned is the current item in the sequence. { if(is_item()) { return cursor->data( ); } else return 0; } template <class Item> void sequence<Item>::operator =(const sequence& source) { value_type counter=0; cursor =source.cursor; tail_ptr = source.tail_ptr; while((cursor != tail_ptr)&&(cursor!=NULL)) { advance(); counter++; } list_copy(source.head_ptr,head_ptr,tail_ptr); many_nodes = source.many_nodes; start(); if(source.cursor == NULL) cursor = NULL; else { for(value_type counter2 =1;counter2<(source.many_nodes-counter); counter2++) advance(); } } }// namespace end
You need to login to post a comment.
