fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct ListNode{
  6. char info;
  7. ListNode * next;
  8. ListNode(char newInfo, ListNode * newNext)
  9. : info( newInfo ), next( newNext ){}
  10. };
  11.  
  12. class String {
  13.  
  14. private:
  15. ListNode* head;
  16.  
  17. public:
  18. String( const char * s = "");
  19. String( const String & s );
  20. String& operator = ( const String & s );
  21. ~String();
  22. int length(){
  23. ListNode* current = head;
  24. int i = 0;
  25. while(current != NULL){
  26. current = current->next;
  27. i++;
  28. }
  29. return i;
  30. }
  31.  
  32. char operator[](int i){
  33. ListNode* current = head;
  34. for(;i > 0; i--){
  35. current = current->next;
  36. }
  37. return current->info;
  38. }
  39. };
  40. ostream & operator << ( ostream & out, String& str );
  41. istream & operator >> ( istream & in, String & str );
  42.  
  43. String::String( const char * s) {
  44. if (s == "") {
  45. head = NULL;
  46. return;
  47. }
  48.  
  49. ListNode* newNode = new ListNode(s[0], NULL);
  50. head = newNode;
  51. ListNode* current = head;
  52. for (int i = 1; s[i] != 0; current = current->next) {
  53. current->next = new ListNode(s[i], NULL);
  54. ++i;
  55. }
  56. }
  57.  
  58. String::String(const String& s ) {
  59. ListNode* current = new ListNode((s.head)->info, NULL); //make all next's null just in case
  60. head = current;
  61. for(ListNode* sstart = s.head->next; sstart != NULL; sstart = sstart->next) {
  62. current->next = new ListNode(sstart->info, NULL);
  63. current = current->next;
  64. }
  65. }
  66.  
  67. String& String::operator = ( const String & s ) {
  68. ListNode* start = head;
  69. ListNode* tmp;
  70. while(start != NULL) {
  71. tmp = start->next;
  72. delete start;
  73. start = tmp;
  74. }
  75.  
  76. head = NULL;
  77.  
  78. if (s.head == NULL)
  79. return *this;
  80. ListNode* current = new ListNode((s.head)->info, NULL); //make all next's null just in case
  81. head = current;
  82. for(ListNode* sstart = s.head->next; sstart != NULL; sstart = sstart->next) {
  83. current->next = new ListNode(sstart->info, NULL);
  84. current = current->next;
  85. }
  86.  
  87. return *this;
  88. }
  89.  
  90. String::~String() {
  91. ListNode* nextNode = head;
  92. ListNode* tmp;
  93. while(nextNode) {
  94. tmp = nextNode->next;
  95. delete nextNode;
  96. nextNode = tmp;
  97. }
  98. }
  99.  
  100. ostream & operator << ( ostream & out, String& str) {
  101. for (int i = 0; i < str.length(); ++i) {
  102. out << str[i];
  103. }
  104. return out;
  105. }
  106.  
  107. istream & operator >> ( istream & in, String & str ) {
  108. int len = in.gcount();
  109. char* buf = new char[len];
  110. char inChar;
  111. for(int i = 0; in >> inChar; ++i) {
  112. buf[i] = inChar;
  113. }
  114. String tmp(buf);
  115. str = tmp;
  116. }
  117.  
  118. int main(){
  119. String s("Hello");
  120. String t("Goodbye");
  121. s = t;
  122. cout<<s;
  123. return 0;
  124. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Goodbye