Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream> // for doing file I/O
  3. using namespace std;
  4.  
  5. int NumAllocations = 0;
  6.  
  7. class MyString
  8. {
  9. private:
  10.  
  11. void error(const char *s)
  12. {
  13. cerr << "Error: " << s << endl;
  14. throw 0;
  15. }
  16. struct ListNode
  17. {
  18. char info;
  19. ListNode * next;
  20. ListNode(char newInfo, ListNode * newNext)
  21. : info( newInfo ), next( newNext )
  22. {}
  23. static ListNode * insert(char c, ListNode *L)
  24. {
  25. NumAllocations += 1;
  26. return new ListNode(c, L);
  27. }
  28.  
  29. static ListNode * convertList(const char *s)
  30. {
  31. int length = 0;
  32. for (int i = 0; s[i] != '\0'; ++i)
  33. {
  34. length += 1;
  35. }
  36.  
  37. ListNode * result = nullptr;
  38.  
  39. for (length-=1; length >= 0; --length)
  40. {
  41. result = insert(s[length], result);
  42. }
  43. return result;
  44. }
  45. static ListNode * copy(ListNode * L)
  46. {
  47.  
  48. ListNode * result = nullptr;
  49. ListNode * actual = nullptr;
  50. for (ListNode *temp = L; temp != nullptr; temp = temp -> next)
  51. {
  52. result = insert(temp -> info, result);
  53. }
  54.  
  55. for (ListNode *temp = result; temp != nullptr; temp = temp -> next)
  56. {
  57. actual = insert(temp -> info, actual);
  58. }
  59. deleteList(result);
  60. return actual;
  61.  
  62. }
  63.  
  64. static ListNode * reverse(ListNode * L)
  65. {
  66. ListNode * result = nullptr;
  67. for (ListNode *temp = L; temp != nullptr; temp = temp -> next)
  68. {
  69. result = insert(temp -> info, result);
  70. }
  71. return result;
  72. }
  73. static ListNode * append(ListNode * L1, ListNode * L2); // for +
  74. static int compare(ListNode * L1, ListNode * L2)
  75. {
  76. for (ListNode *temp1 = L1; temp1 != nullptr; temp1 = temp1 -> next)
  77. {
  78. for (ListNode *temp2 = L2; temp2 != nullptr; temp2 = temp2 -> next)
  79. {
  80. if (temp1 -> info != temp2 -> info || temp1 == nullptr || temp2 == nullptr)
  81. {
  82. return temp1 -> info - temp2 -> info;
  83. }
  84. }
  85. }
  86. return 0;
  87. }
  88. static void deleteList(ListNode * L)
  89. {
  90. ListNode *temp;
  91. for (L; L != nullptr;)
  92. {
  93. NumAllocations -= 1;
  94. temp = L;
  95. L = L -> next;
  96. delete temp;
  97. }
  98. }
  99. static int length(ListNode * L)
  100. {
  101. int length = 0;
  102. for (ListNode *temp = L; temp != nullptr; temp = temp -> next)
  103. length += 1;
  104. return length;
  105. }
  106. };
  107. ListNode *head;
  108.  
  109. public:
  110. explicit MyString(const char * s = "")
  111. :head(ListNode::convertList(s))
  112. {}
  113.  
  114. MyString( const MyString & s )
  115. :head(ListNode::copy(s.head))
  116. {}
  117.  
  118. MyString & operator = ( const MyString & s )
  119. {
  120. ListNode::deleteList(head);
  121. head = ListNode::copy(s.head);
  122. return *this;
  123. }
  124.  
  125. int length() const
  126. {
  127. return ListNode::length(head);
  128. }
  129.  
  130. MyString reverse() const
  131. {
  132. MyString reversed;
  133. ListNode::deleteList(reversed.head);
  134. reversed.head = ListNode::reverse(head);
  135. return reversed;
  136. }
  137.  
  138. char & operator [] ( const int index )
  139. {
  140. ListNode *temp = head;
  141. if (index >= 0 && index < ListNode::length(head))
  142. {
  143. for (int i = 0; i < index; ++i)
  144. {
  145. temp = temp -> next;
  146. }
  147. return temp -> info;
  148. }
  149. else
  150. {
  151. error("Error, index out of bounds, returning item at index 0 instead: ");
  152. cout << head -> info << endl;
  153. }
  154. }
  155.  
  156. bool operator == ( const MyString & s ) const
  157. {
  158. int value = ListNode::compare(head, s.head);
  159. return value == 0;
  160. }
  161.  
  162. int indexOf( char c ) const
  163. {
  164. int index = 0;
  165. for (ListNode *temp = head; temp != nullptr; temp = temp -> next)
  166. {
  167. if (temp -> info == c)
  168. return index;
  169. ++index;
  170. }
  171. return -1;
  172. }
  173.  
  174. void print( ostream & out ) const
  175. {
  176. for (ListNode *temp = head; temp != nullptr; temp = temp -> next)
  177. out << temp -> info;
  178. }
  179.  
  180. void read( istream & in )
  181. {
  182. NumAllocations += 1;
  183. char *buf = new char[256];
  184. in.getline(buf, 255);
  185. head = ListNode::convertList(buf);
  186. delete[] buf;
  187. NumAllocations -= 1;
  188.  
  189. }
  190.  
  191. ~MyString()
  192. {
  193. ListNode::deleteList(head);
  194. }
  195.  
  196.  
  197. };
  198.  
  199. inline ostream & operator << ( ostream & out, const MyString & str )
  200. {
  201. str.print(out);
  202. return out;
  203. }
  204.  
  205. inline istream & operator >> ( istream & in, MyString & str )
  206. {
  207. str.read(in);
  208. return in;
  209. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement