Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Implement this sequence class below with the same functions using linked list data structures instead of array.Implement the value semantics.
- Create the template class for sequence.
- Implement the erase_all_ occurrence which is similar to the erase_ occurrence but it erases all the occurrence of a specific number.
- For example for sequence 1 2 4 3 2 3 3 1 3 2 erase_ occurrence(3, 2) removes only the third occurrence of 2 but erase_all_ occurrence(2) erases all the occurrences of 2.
- What is the benefit of using linked list instead of array in this case? (Optional: how you may implement the erase_all_ occurrence for arrays such that you only shift each number one time?)
- Try to have two files.Implement the [] operator such that you can access to a specific element of the sequence using that.
- Example: s[5] = 10;
- Write down the running time of each function as a comment in from of that in the header as O notation (e.x. O(n) means linear on input size n).
- Write comments in the header.
- The sequence class with arrays before is here:
- -------------------------------------sequence.h----------------------------------------------------
- #ifndef ASSIGNMENT3_SEQUENCE_H
- #define ASSIGNMENT3_SEQUENCE_H
- #include "iostream"
- class sequence
- {
- public:
- static const size_t CAPACITY = 30;
- // CONSTRUCTOR
- sequence(size_t capacity = CAPACITY);// here i am inittilializing the constructor the constr to the capac
- sequence(const sequence& seq); // copy constructor
- ~sequence();//destructor
- void erase_First();
- void erase_Last();
- void erase_Val(int i);
- void erase_From(const int &index);
- void erase_Occurence(const int &);
- void insert(const int &);
- void insert_at(const int &, const int &);
- void count();
- friend std::ostream &operator<<(std::ostream &, const sequence &);
- friend std::istream &operator>>(std::istream &, sequence &);
- friend sequence operator+(const sequence &, const sequence &);
- friend sequence operator-(const sequence &, const sequence &);
- void print() const;
- size_t size() { return used; }
- size_t count_val(const int &) const;
- sequence &operator+=(const sequence &);
- sequence &operator=(const sequence &);
- //bool& operator==(const sequence&);
- sequence &operator+(const sequence &);
- sequence &operator-(const sequence &);
- private:
- size_t used;
- size_t capacity;
- int* data;
- };
- #endif //ASSIGNMENT3_SEQUENCE_H
- -----------------------------------------------------Sequence.cpp-----------------
- #include <cassert>
- #include "sequence.h"
- #include <algorithm>
- using namespace std;
- //below is the overloaded constructor in case the user want a specific size.
- sequence::sequence(size_t capacity){
- this->capacity = capacity;
- data = new int[capacity];
- used = 0;
- }
- //below is the copy constructor. 1st part of value semantics
- sequence::sequence(const sequence& seq) {
- if(capacity != seq.capacity){
- delete[] data;
- data = new int[seq.capacity];
- capacity = seq.capacity;
- }
- used = seq.used;
- std::copy(seq.data,seq.data + used, data);
- }
- //below is the destructor. The third part of value semantics
- sequence::~sequence(){
- delete[] data;
- }
- //@pre-condition:get the values from data
- //@post-condition: remove the first occurence of value
- void sequence::erase_First()
- {
- for(int i =0; i < used; i++) {
- data[i] = data [i+1];
- }
- --used;
- }
- //@pre-condition:get values from data
- //@post-condition: remove last occurence of value
- void sequence::erase_Last()
- {
- int index;
- index = data[--used];
- }
- //@pre-condition:gather elements
- //@post-condition: accept index of elments that need to be erased
- void sequence::erase_From(const int &index) {
- assert(used < capacity);
- for (int i = 0; i < used; i++) {
- data[index] = data [index + 1];
- }
- --used;
- }
- //@pre-condition: gather values from data
- //@post-condition: remove occurence of value
- void sequence::erase_Occurence(const int& value)
- {
- int index = -1;
- for (int i = 0; i < used ; i++) {
- if (data[i] == value){
- index = i;
- break;
- }
- }
- if (index == -1)
- return;
- data[index] = data[--used];
- }
- //@post-condition: insert values into sequence
- void sequence::insert(const int& val)
- {
- assert(used<capacity);
- data[used] = val;
- ++used;
- }
- //@pre-condtion: gather indexes for sequence
- //@post-condition: insert element at specific index
- void sequence::insert_at(const int& value, const int& index)
- {
- assert(used < capacity);
- data[index] = value;
- used++;
- }
- //@post-condtion: count the values from data
- size_t sequence::count_val(const int& val) const
- {
- size_t count = 0;
- for (int i = 0; i < used; i++) {
- if(data[i] == val){
- count++;
- }
- }
- return count;
- }
- //@pre-condition: member function that will be overloaded
- //@post-condtion: overloaded member function that adds a number to sequence
- sequence& sequence::operator+=(const sequence& seq)
- {
- assert(seq.used+used<=capacity);
- used = seq.used;
- for (int i = 0; i < used; i++) {
- data[used+i] = seq.data[i];
- }
- return *this;
- }
- //prints data
- void sequence::print() const {
- for (int i = 0; i < used; i++)
- {
- std::cout << data[i] << std::endl;
- }
- }
- //@pre-condtion: gather two sequences
- //@post-condtion: determine equality of two sequences
- /*bool operator==(const sequence& seq1, const sequence& seq2) {
- if (seq1.count() != seq2.count()) {
- return false;
- }
- else (seq1.count_val() == seq2.count_val())
- {
- return true;
- }
- }*/
- //overloaded ostream and istream operators that became obsolete due to easiness print function but I am getting error
- /*std::ostream &operator<<(std::ostream& outs, const sequence& b) {
- outs<< b;
- return outs;
- }
- std::istream &operator>>(std::istream& in, sequence& b) {
- in>>b;
- return in;
- }
- */
- ---------------------------------------------------------main.cpp--------------------
- #include <iostream>
- #include "sequence.h"
- using namespace std;
- int main() {
- sequence sequence1, sequence2;
- for (int i = 0; i < 10; i++) {
- sequence1.insert(i);
- }
- for (int i = 10; i < 20; i++) {
- sequence2.insert(i);
- }
- sequence1.print();
- sequence2.print();
- cout << "Removing first element " << sequence1.size()<<" "<<sequence2.size()<<"\n";
- sequence1.erase_First();
- sequence2.erase_First();
- sequence1.print();
- sequence2.print();
- cout << "Removing last element " <<sequence1.size() << " " << sequence2.size() << "\n";
- sequence1.erase_Last();
- sequence2.erase_Last();
- sequence1.print();
- sequence2.print();
- cout << "Removing a element " << sequence1.size() << " " << sequence2.size() << "\n";
- sequence1.erase_From(1);
- sequence2.erase_From(1);
- sequence1.print();
- sequence2.print();
- //cout << "What index to remove?\n";
- //sequence1.erase_Val(8);
- //cout << sequence1;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement