Advertisement
Guest User

code

a guest
Jul 28th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. Implement this sequence class below with the same functions using linked list data structures instead of array.Implement the value semantics.
  2.  
  3. Create the template class for sequence.
  4.  
  5. Implement the erase_all_ occurrence which is similar to the erase_ occurrence but it erases all the occurrence of a specific number.
  6.  
  7. 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.
  8.  
  9. 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?)
  10.  
  11. Try to have two files.Implement the [] operator such that you can access to a specific element of the sequence using that.
  12. Example: s[5] = 10;
  13.  
  14. 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).
  15.  
  16. Write comments in the header.
  17. The sequence class with arrays before is here:
  18. -------------------------------------sequence.h----------------------------------------------------
  19. #ifndef ASSIGNMENT3_SEQUENCE_H
  20. #define ASSIGNMENT3_SEQUENCE_H
  21.  
  22. #include "iostream"
  23.  
  24. class sequence
  25. {
  26. public:
  27.  
  28. static const size_t CAPACITY = 30;
  29. // CONSTRUCTOR
  30. sequence(size_t capacity = CAPACITY);// here i am inittilializing the constructor the constr to the capac
  31. sequence(const sequence& seq); // copy constructor
  32. ~sequence();//destructor
  33.  
  34. void erase_First();
  35. void erase_Last();
  36. void erase_Val(int i);
  37. void erase_From(const int &index);
  38. void erase_Occurence(const int &);
  39.  
  40. void insert(const int &);
  41. void insert_at(const int &, const int &);
  42.  
  43. void count();
  44.  
  45.  
  46. friend std::ostream &operator<<(std::ostream &, const sequence &);
  47. friend std::istream &operator>>(std::istream &, sequence &);
  48.  
  49. friend sequence operator+(const sequence &, const sequence &);
  50. friend sequence operator-(const sequence &, const sequence &);
  51.  
  52.  
  53.  
  54. void print() const;
  55. size_t size() { return used; }
  56. size_t count_val(const int &) const;
  57.  
  58.  
  59. sequence &operator+=(const sequence &);
  60. sequence &operator=(const sequence &);
  61. //bool& operator==(const sequence&);
  62. sequence &operator+(const sequence &);
  63. sequence &operator-(const sequence &);
  64.  
  65. private:
  66. size_t used;
  67. size_t capacity;
  68.  
  69. int* data;
  70.  
  71. };
  72.  
  73. #endif //ASSIGNMENT3_SEQUENCE_H
  74. -----------------------------------------------------Sequence.cpp-----------------
  75. #include <cassert>
  76. #include "sequence.h"
  77. #include <algorithm>
  78.  
  79. using namespace std;
  80.  
  81. //below is the overloaded constructor in case the user want a specific size.
  82. sequence::sequence(size_t capacity){
  83. this->capacity = capacity;
  84. data = new int[capacity];
  85. used = 0;
  86. }
  87. //below is the copy constructor. 1st part of value semantics
  88. sequence::sequence(const sequence& seq) {
  89. if(capacity != seq.capacity){
  90. delete[] data;
  91. data = new int[seq.capacity];
  92. capacity = seq.capacity;
  93. }
  94. used = seq.used;
  95. std::copy(seq.data,seq.data + used, data);
  96. }
  97. //below is the destructor. The third part of value semantics
  98. sequence::~sequence(){
  99. delete[] data;
  100. }
  101. //@pre-condition:get the values from data
  102. //@post-condition: remove the first occurence of value
  103. void sequence::erase_First()
  104. {
  105. for(int i =0; i < used; i++) {
  106. data[i] = data [i+1];
  107. }
  108. --used;
  109. }
  110. //@pre-condition:get values from data
  111. //@post-condition: remove last occurence of value
  112. void sequence::erase_Last()
  113. {
  114. int index;
  115. index = data[--used];
  116. }
  117. //@pre-condition:gather elements
  118. //@post-condition: accept index of elments that need to be erased
  119. void sequence::erase_From(const int &index) {
  120. assert(used < capacity);
  121. for (int i = 0; i < used; i++) {
  122. data[index] = data [index + 1];
  123. }
  124.  
  125. --used;
  126. }
  127. //@pre-condition: gather values from data
  128. //@post-condition: remove occurence of value
  129. void sequence::erase_Occurence(const int& value)
  130. {
  131. int index = -1;
  132. for (int i = 0; i < used ; i++) {
  133. if (data[i] == value){
  134. index = i;
  135. break;
  136. }
  137. }
  138. if (index == -1)
  139. return;
  140. data[index] = data[--used];
  141. }
  142. //@post-condition: insert values into sequence
  143. void sequence::insert(const int& val)
  144. {
  145. assert(used<capacity);
  146. data[used] = val;
  147. ++used;
  148. }
  149. //@pre-condtion: gather indexes for sequence
  150. //@post-condition: insert element at specific index
  151. void sequence::insert_at(const int& value, const int& index)
  152. {
  153. assert(used < capacity);
  154. data[index] = value;
  155. used++;
  156. }
  157. //@post-condtion: count the values from data
  158. size_t sequence::count_val(const int& val) const
  159. {
  160. size_t count = 0;
  161. for (int i = 0; i < used; i++) {
  162. if(data[i] == val){
  163. count++;
  164. }
  165. }
  166. return count;
  167. }
  168. //@pre-condition: member function that will be overloaded
  169. //@post-condtion: overloaded member function that adds a number to sequence
  170. sequence& sequence::operator+=(const sequence& seq)
  171. {
  172. assert(seq.used+used<=capacity);
  173. used = seq.used;
  174. for (int i = 0; i < used; i++) {
  175. data[used+i] = seq.data[i];
  176. }
  177. return *this;
  178. }
  179. //prints data
  180. void sequence::print() const {
  181. for (int i = 0; i < used; i++)
  182. {
  183. std::cout << data[i] << std::endl;
  184. }
  185. }
  186. //@pre-condtion: gather two sequences
  187. //@post-condtion: determine equality of two sequences
  188. /*bool operator==(const sequence& seq1, const sequence& seq2) {
  189. if (seq1.count() != seq2.count()) {
  190. return false;
  191. }
  192. else (seq1.count_val() == seq2.count_val())
  193. {
  194. return true;
  195. }
  196. }*/
  197. //overloaded ostream and istream operators that became obsolete due to easiness print function but I am getting error
  198. /*std::ostream &operator<<(std::ostream& outs, const sequence& b) {
  199. outs<< b;
  200. return outs;
  201. }
  202.  
  203. std::istream &operator>>(std::istream& in, sequence& b) {
  204. in>>b;
  205. return in;
  206. }
  207. */
  208. ---------------------------------------------------------main.cpp--------------------
  209. #include <iostream>
  210. #include "sequence.h"
  211.  
  212. using namespace std;
  213.  
  214. int main() {
  215. sequence sequence1, sequence2;
  216. for (int i = 0; i < 10; i++) {
  217. sequence1.insert(i);
  218. }
  219. for (int i = 10; i < 20; i++) {
  220. sequence2.insert(i);
  221. }
  222.  
  223. sequence1.print();
  224. sequence2.print();
  225. cout << "Removing first element " << sequence1.size()<<" "<<sequence2.size()<<"\n";
  226. sequence1.erase_First();
  227. sequence2.erase_First();
  228. sequence1.print();
  229. sequence2.print();
  230.  
  231. cout << "Removing last element " <<sequence1.size() << " " << sequence2.size() << "\n";
  232. sequence1.erase_Last();
  233. sequence2.erase_Last();
  234. sequence1.print();
  235. sequence2.print();
  236.  
  237. cout << "Removing a element " << sequence1.size() << " " << sequence2.size() << "\n";
  238. sequence1.erase_From(1);
  239. sequence2.erase_From(1);
  240. sequence1.print();
  241. sequence2.print();
  242.  
  243. //cout << "What index to remove?\n";
  244. //sequence1.erase_Val(8);
  245. //cout << sequence1;
  246.  
  247. system("PAUSE");
  248. return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement