Question

The purpose of this program is to help reinforce container class concepts and linked list concepts...

The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible.

Please make sure code can pass any tests.

sequence3.h

// FILE: sequence3.h
// CLASS PROVIDED: sequence (part of the namespace main_savitch_5)
// This is the header file for the project described in Section 5.4
// of "Data Structures and Other Objects Using C++"
// This is called "sequence3" because some students already implemented
// sequence1 (with a fixed array) and sequence2 (with a dynamic array).
//
// TYPEDEFS and MEMBER CONSTANTS for the sequence class:
//   typedef ____ value_type
//     sequence::value_type is 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.
//
//   typedef ____ size_type
//     sequence::size_type is the data type of any variable that keeps track of
//     how many items are in a sequence.
//
// CONSTRUCTOR for the sequence class:
//   sequence( )
//     Postcondition: The sequence has been initialized as an empty sequence.
//
// MODIFICATION MEMBER FUNCTIONS for the sequence class:
//   void start( )
//     Postcondition: The first item on the sequence becomes the current item
//     (but if the sequence is empty, then there is no current item).
//
//   void 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.
//
//   void 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.
//
//   void 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.
//
//   void 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.
//
// CONSTANT MEMBER FUNCTIONS for the sequence class:
//   size_type size( ) const
//     Postcondition: The return value is the number of items in the sequence.
//
//   bool is_item( ) const
//     Postcondition: A true return value indicates that there is a valid
//     "current" item that may be retrieved by activating the current
//     member function (listed below). A false return value indicates that
//     there is no valid current item.
//
//   value_type current( ) const
//     Precondition: is_item( ) returns true.
//     Postcondition: The item returned is the current item in the sequence.
//
// VALUE SEMANTICS for the sequence class:
//    Assignments and the copy constructor may be used with sequence objects.

#ifndef MAIN_SAVITCH_SEQUENCE3_H
#define MAIN_SAVITCH_SEQUENCE3_H
#include <cstdlib>  // Provides size_t
#include "node1.h"  // Provides node class

namespace main_savitch_5
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef std::size_t size_type;
        // CONSTRUCTORS and DESTRUCTOR
        sequence( );
        sequence(const sequence& source);
        ~sequence( );
        // MODIFICATION MEMBER FUNCTIONS
        void start( );
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void operator =(const sequence& source);
        void remove_current( );
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const { return many_nodes; }
        bool is_item( ) const { return (cursor != NULL); }
        value_type current( ) const;
    private:
        node *head_ptr;
        node *tail_ptr;
        node *cursor;
        node *precursor;
        size_type many_nodes;
    };
}

#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <cstdlib>
#include <cassert>
#include "node1.h"
#include "sequence3.h"

using namespace std;

namespace main_savitch_5 {
        sequence::sequence() {
           head_ptr= NULL;
           tail_ptr=NULL;
           cursor= NULL;
           precursor=NULL;
           many_nodes=0;
        }
        
        sequence::sequence(const sequence& source) {
           node *tail_ptr;
           list_copy(source.head_ptr, head_ptr, tail_ptr);
           many_nodes = source.many_nodes;
        }
        
        sequence::~sequence() {
                list_clear(head_ptr);
                many_nodes = 0;
        }
        
        void sequence::start() {
           if(many_nodes != 0){
                        cursor = head_ptr;
                        precursor = NULL;
           }
        }
        
        void sequence::advance() {
                if(is_item()) {
                        precursor = cursor;
                        cursor = cursor->link();
                }
        }
        
        void sequence::insert(const value_type& entry) {
                if (is_item( )){
                if(precursor==NULL || cursor==NULL){
                    list_head_insert(head_ptr, entry);
                    cursor=head_ptr;
                    precursor=NULL;
                }else{
                    list_insert(precursor, entry);
                    cursor= precursor->link();
                }
            }else{
                list_head_insert(head_ptr, entry);
                cursor = head_ptr;
                precursor=NULL;
            }
                ++many_nodes;
        }
        
        void sequence::attach(const value_type& entry) {
                if (is_item( )){
                precursor = cursor;
                list_insert(cursor, entry);
                cursor=cursor->link();
            }else{
                    if(head_ptr == NULL){
                        list_head_insert(head_ptr, entry);
                        cursor = head_ptr;
                        precursor=NULL;
                    }else{
                        precursor = list_locate(head_ptr,list_length(head_ptr));
                        list_insert(precursor,entry);
                        cursor = precursor->link();
                    }
            }
                ++many_nodes;
        }
        
        void sequence::remove_current () {
                if (is_item( )){
                        if(cursor == head_ptr){
                    if(many_nodes == 1){
                        list_head_remove(head_ptr);
                        precursor = NULL;
                        cursor = NULL;
                        head_ptr = NULL;
                    }else{
                        node* tmp = head_ptr;
                        head_ptr = head_ptr->link();
                        delete (tmp);
                        cursor = head_ptr;
                        precursor = NULL;
                    }
                }else{
                    cursor = cursor -> link();
                    list_remove(precursor);
                }
                }
            --many_nodes;
        }
        
        void sequence::operator =(const sequence& source) {
           if(this == &source){
                return;
            }else{
                list_copy(source.head_ptr, head_ptr, tail_ptr);
                if(source.precursor == NULL){
                    if(source.cursor == NULL){
                        cursor = NULL;
                        precursor = NULL;
                    }else{
                        cursor = head_ptr;
                        precursor = NULL;
                    }
                }else{
                    node *tmp_ptr = head_ptr;
                    node *source_ptr = source.head_ptr;
                    while(source_ptr != source.precursor){
                        source_ptr = source_ptr -> link();
                        tmp_ptr = tmp_ptr -> link();
                    }
                    cursor = tmp_ptr -> link();
                    precursor = tmp_ptr;
                }
            }
                many_nodes = source.many_nodes;
        }
        
        sequence::size_type sequence::size() const {
                return many_nodes;
        }
        
        sequence::value_type sequence::current() const {
                if(is_item()){
                        return cursor->data();
                }
        }
        
        bool sequence:: is_item() const {
                return (cursor != NULL);
        }
}
Add a comment
Know the answer?
Add Answer to:
The purpose of this program is to help reinforce container class concepts and linked list concepts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • c++ Sequence class is a class that supports sequence of integers. Run the project ad verify...

    c++ Sequence class is a class that supports sequence of integers. Run the project ad verify that project works for integers. Next convert your sequence class into a template class. Demonstrate in your main function by using a sequence of int, float, string. sequence.cxx #include // Provides assert using namespace std; namespace main_savitch_3 { sequence::sequence() { used = 0; current_index = 0; } void sequence::start() { current_index = 0; } void sequence::advance() // Library facilities used: assert.h { assert(is_item()); current_index++;...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

  • Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides...

    Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT