SlideShare a Scribd company logo
Implement the sequence class from Section 3.2 of the textbook. The documentation and
definition portion of this sequence ADT is provided in textbook (pp.124-133), and is posted in
BlackBoard of this class too.
Also implement two operators + and += for the ADT. For the + operator, x+y (a new sequence)
contains all the items of x, followed by all the items of Y.
The statement x += y appends all of the items of y to the end of what’s already in X.
For simplicity, let’s assume the sequence contains integer items and the capacity is 30.
Write a simple application to manage a playlist of your favorite music, where the sequence class
is used to insert, delete, search and print the title of music.
Your application should read in integral numbers (an ID, instead of title, for simplicity) of each
piece of music and insert them onto a sequence. It should be able to insert an item at a specified
location, find and display the music currently at play, delete a specified item from the sequence
as well as display the entire list of music in the sequence.
// FILE: sequence1.h
// CLASS PROVIDED: sequence (part of the namespace main_savitch_3)
// There is no implementation file provided for this class since it is
// an exercise from Section 3.2 of "Data Structures and Other Objects Using C++"
//
// 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.
//
// static const size_type CAPACITY = _____
// sequence::CAPACITY is the maximum number of items that a sequence can hold.
//
// 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)
// Precondition: size( ) < CAPACITY.
// 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)
// Precondition: size( ) < CAPACITY.
// 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_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include // Provides size_t
namespace main_savitch_3
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
sequence( );
// MODIFICATION MEMBER FUNCTIONS
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current( );
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}
#endif
Solution
#include "sequence1.h"
namespace main_savitch_3{
sequence::sequence(){
current_index = -1;
used = 0;
}
// MODIFICATION MEMBER FUNCTIONS
void sequence::start(){
if(used == 0){
current_index = -1;
}
else{
current_index = 0;
}
}
void sequence::advance(){
if(current_index == used - 1){
current_index = -1;
}
else{
++current_index;
}
}
void sequence::insert(const value_type& entry){
int pos;
if(current_index == -1){
pos = 0;
}
else{
pos = current_index;
}
for(int i = used; i > pos; --i){
data[i] = data[i - 1];
}
data[pos] = entry;
current_index = pos;
}
void sequence::attach(const value_type& entry){
int pos;
if(current_index == -1){
pos = used;
}
else{
pos = current_index;
}
for(int i = used; i > pos; --i){
data[i] = data[i - 1];
}
data[pos] = entry;
current_index = pos;
}
void sequence::remove_current(){
if(used > 0){
for(int i = current_index; i < used - 1; ++i){
data[i] = data[i + 1];
}
used--;
}
}
// CONSTANT MEMBER FUNCTIONS
sequence::size_type sequence::size() const{
return used;
}
bool sequence::is_item() const{
return current_index >= 0 && current_index < used;
}
sequence::value_type sequence::current() const{
return data[current_index];
}
}

More Related Content

PDF
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
PDF
Files that you must write and turn in Please do not turn in.pdf
PDF
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
PDF
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
PPTX
Object Oriented Design and Programming Unit-05
PPTX
Data structures
PDF
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Given below is the code for the question. Since the test files (ment.pdf
Files that you must write and turn in Please do not turn in.pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
Object Oriented Design and Programming Unit-05
Data structures
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf

Similar to Implement the sequence class from Section 3.2 of the textbook. The d.pdf (20)

PDF
1- The design of a singly-linked list below is a picture of the functi (1).pdf
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
PDF
(C++) Change the following program so that it uses a dynamic array i.pdf
PPT
Standard Template Library (STL) in Object Oriented Programming
PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
PDF
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
ODT
(3) cpp abstractions more_on_user_defined_types_exercises
PDF
Lab 13 Practicing STLVector Container1a. Write a template func.pdf
DOCX
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
PDF
Ds lab handouts
DOCX
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
PDF
#include iostream     Provides cout. #include cstdlib   .pdf
PPTX
Standard Template Library
PDF
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
PPT
Computer notes - Josephus Problem
PPTX
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
PPT
C++lecture9
PDF
This assignment and the next (#5) involve design and development of a.pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
Standard Template Library (STL) in Object Oriented Programming
include ltfunctionalgt include ltiteratorgt inclu.pdf
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
(3) cpp abstractions more_on_user_defined_types_exercises
Lab 13 Practicing STLVector Container1a. Write a template func.pdf
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
Ds lab handouts
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
#include iostream     Provides cout. #include cstdlib   .pdf
Standard Template Library
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
Computer notes - Josephus Problem
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
C++lecture9
This assignment and the next (#5) involve design and development of a.pdf
Ad

More from info961251 (20)

PDF
With which portion of an epithelial cell in the stomach would food be.pdf
PDF
Write the interface (.h file) of a class Accumulator containing A d.pdf
PDF
You are carrying out PCRs in lab. Why do you not need helicase, SSB,.pdf
PDF
Write your own definition of cloning. No plagarism!SolutionClo.pdf
PDF
Write appropriate SQL DDL statements (Create Table Statements) for d.pdf
PDF
Write a class called Student that extends the provided Person class..pdf
PDF
why NADH and FADH2 are a type of energy currencySolutionEnergy.pdf
PDF
Who is D.L ShellSolutionHi friend,Donald L. Shell (March 1,.pdf
PDF
Which of the following correctly states the functions of the roug.pdf
PDF
What is isomerism How many kinds are there Define each kind and.pdf
PDF
What is the definition of the words, and state the diffe.pdf
PDF
Two sources of radio waves (call them 1 and 2) are separated by a dis.pdf
PDF
Use the information in the table to select the image that represents .pdf
PDF
Triggers can only be used to update table values. PLSQL blocks have.pdf
PDF
TB is a major worldwide disease with new cases arising at an alarmin.pdf
PDF
7. How did printing technology impact literacy and educationSol.pdf
PDF
Question 1Theories of Law Subject__________ is a collaborative.pdf
PDF
Q Describe the 2 types of homologs. (1) Orthologs (2) ParalogsS.pdf
PDF
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
PDF
Overview You are tasked with writing a program called Social Security.pdf
With which portion of an epithelial cell in the stomach would food be.pdf
Write the interface (.h file) of a class Accumulator containing A d.pdf
You are carrying out PCRs in lab. Why do you not need helicase, SSB,.pdf
Write your own definition of cloning. No plagarism!SolutionClo.pdf
Write appropriate SQL DDL statements (Create Table Statements) for d.pdf
Write a class called Student that extends the provided Person class..pdf
why NADH and FADH2 are a type of energy currencySolutionEnergy.pdf
Who is D.L ShellSolutionHi friend,Donald L. Shell (March 1,.pdf
Which of the following correctly states the functions of the roug.pdf
What is isomerism How many kinds are there Define each kind and.pdf
What is the definition of the words, and state the diffe.pdf
Two sources of radio waves (call them 1 and 2) are separated by a dis.pdf
Use the information in the table to select the image that represents .pdf
Triggers can only be used to update table values. PLSQL blocks have.pdf
TB is a major worldwide disease with new cases arising at an alarmin.pdf
7. How did printing technology impact literacy and educationSol.pdf
Question 1Theories of Law Subject__________ is a collaborative.pdf
Q Describe the 2 types of homologs. (1) Orthologs (2) ParalogsS.pdf
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
Overview You are tasked with writing a program called Social Security.pdf
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Classroom Observation Tools for Teachers
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
Presentation on HIE in infants and its manifestations
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Computing-Curriculum for Schools in Ghana
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Chinmaya Tiranga quiz Grand Finale.pdf
Anesthesia in Laparoscopic Surgery in India
Classroom Observation Tools for Teachers
Microbial diseases, their pathogenesis and prophylaxis
Cell Structure & Organelles in detailed.

Implement the sequence class from Section 3.2 of the textbook. The d.pdf

  • 1. Implement the sequence class from Section 3.2 of the textbook. The documentation and definition portion of this sequence ADT is provided in textbook (pp.124-133), and is posted in BlackBoard of this class too. Also implement two operators + and += for the ADT. For the + operator, x+y (a new sequence) contains all the items of x, followed by all the items of Y. The statement x += y appends all of the items of y to the end of what’s already in X. For simplicity, let’s assume the sequence contains integer items and the capacity is 30. Write a simple application to manage a playlist of your favorite music, where the sequence class is used to insert, delete, search and print the title of music. Your application should read in integral numbers (an ID, instead of title, for simplicity) of each piece of music and insert them onto a sequence. It should be able to insert an item at a specified location, find and display the music currently at play, delete a specified item from the sequence as well as display the entire list of music in the sequence. // FILE: sequence1.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_3) // There is no implementation file provided for this class since it is // an exercise from Section 3.2 of "Data Structures and Other Objects Using C++" // // 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. // // static const size_type CAPACITY = _____ // sequence::CAPACITY is the maximum number of items that a sequence can hold. // // CONSTRUCTOR for the sequence class: // sequence( ) // Postcondition: The sequence has been initialized as an empty sequence.
  • 2. // // 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) // Precondition: size( ) < CAPACITY. // 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) // Precondition: size( ) < CAPACITY. // 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
  • 3. // 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_SEQUENCE_H #define MAIN_SAVITCH_SEQUENCE_H #include // Provides size_t namespace main_savitch_3 { class sequence { public: // TYPEDEFS and MEMBER CONSTANTS typedef double value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR sequence( ); // MODIFICATION MEMBER FUNCTIONS void start( ); void advance( ); void insert(const value_type& entry); void attach(const value_type& entry); void remove_current( ); // CONSTANT MEMBER FUNCTIONS size_type size( ) const; bool is_item( ) const; value_type current( ) const; private:
  • 4. value_type data[CAPACITY]; size_type used; size_type current_index; }; } #endif Solution #include "sequence1.h" namespace main_savitch_3{ sequence::sequence(){ current_index = -1; used = 0; } // MODIFICATION MEMBER FUNCTIONS void sequence::start(){ if(used == 0){ current_index = -1; } else{ current_index = 0; } } void sequence::advance(){ if(current_index == used - 1){ current_index = -1; } else{ ++current_index; } } void sequence::insert(const value_type& entry){ int pos; if(current_index == -1){ pos = 0;
  • 5. } else{ pos = current_index; } for(int i = used; i > pos; --i){ data[i] = data[i - 1]; } data[pos] = entry; current_index = pos; } void sequence::attach(const value_type& entry){ int pos; if(current_index == -1){ pos = used; } else{ pos = current_index; } for(int i = used; i > pos; --i){ data[i] = data[i - 1]; } data[pos] = entry; current_index = pos; } void sequence::remove_current(){ if(used > 0){ for(int i = current_index; i < used - 1; ++i){ data[i] = data[i + 1]; } used--; } } // CONSTANT MEMBER FUNCTIONS sequence::size_type sequence::size() const{ return used;
  • 6. } bool sequence::is_item() const{ return current_index >= 0 && current_index < used; } sequence::value_type sequence::current() const{ return data[current_index]; } }