Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream> // for doing file I/O
- using namespace std;
- int NumAllocations = 0;
- class MyString
- {
- private:
- void error(const char *s)
- {
- cerr << "Error: " << s << endl;
- throw 0;
- }
- struct ListNode
- {
- char info;
- ListNode * next;
- ListNode(char newInfo, ListNode * newNext)
- : info( newInfo ), next( newNext )
- {}
- static ListNode * insert(char c, ListNode *L)
- {
- NumAllocations += 1;
- return new ListNode(c, L);
- }
- static ListNode * convertList(const char *s)
- {
- int length = 0;
- for (int i = 0; s[i] != '\0'; ++i)
- {
- length += 1;
- }
- ListNode * result = nullptr;
- for (length-=1; length >= 0; --length)
- {
- result = insert(s[length], result);
- }
- return result;
- }
- static ListNode * copy(ListNode * L)
- {
- ListNode * result = nullptr;
- ListNode * actual = nullptr;
- for (ListNode *temp = L; temp != nullptr; temp = temp -> next)
- {
- result = insert(temp -> info, result);
- }
- for (ListNode *temp = result; temp != nullptr; temp = temp -> next)
- {
- actual = insert(temp -> info, actual);
- }
- deleteList(result);
- return actual;
- }
- static ListNode * reverse(ListNode * L)
- {
- ListNode * result = nullptr;
- for (ListNode *temp = L; temp != nullptr; temp = temp -> next)
- {
- result = insert(temp -> info, result);
- }
- return result;
- }
- static ListNode * append(ListNode * L1, ListNode * L2); // for +
- static int compare(ListNode * L1, ListNode * L2)
- {
- for (ListNode *temp1 = L1; temp1 != nullptr; temp1 = temp1 -> next)
- {
- for (ListNode *temp2 = L2; temp2 != nullptr; temp2 = temp2 -> next)
- {
- if (temp1 -> info != temp2 -> info || temp1 == nullptr || temp2 == nullptr)
- {
- return temp1 -> info - temp2 -> info;
- }
- }
- }
- return 0;
- }
- static void deleteList(ListNode * L)
- {
- ListNode *temp;
- for (L; L != nullptr;)
- {
- NumAllocations -= 1;
- temp = L;
- L = L -> next;
- delete temp;
- }
- }
- static int length(ListNode * L)
- {
- int length = 0;
- for (ListNode *temp = L; temp != nullptr; temp = temp -> next)
- length += 1;
- return length;
- }
- };
- ListNode *head;
- public:
- explicit MyString(const char * s = "")
- :head(ListNode::convertList(s))
- {}
- MyString( const MyString & s )
- :head(ListNode::copy(s.head))
- {}
- MyString & operator = ( const MyString & s )
- {
- ListNode::deleteList(head);
- head = ListNode::copy(s.head);
- return *this;
- }
- int length() const
- {
- return ListNode::length(head);
- }
- MyString reverse() const
- {
- MyString reversed;
- ListNode::deleteList(reversed.head);
- reversed.head = ListNode::reverse(head);
- return reversed;
- }
- char & operator [] ( const int index )
- {
- ListNode *temp = head;
- if (index >= 0 && index < ListNode::length(head))
- {
- for (int i = 0; i < index; ++i)
- {
- temp = temp -> next;
- }
- return temp -> info;
- }
- else
- {
- error("Error, index out of bounds, returning item at index 0 instead: ");
- cout << head -> info << endl;
- }
- }
- bool operator == ( const MyString & s ) const
- {
- int value = ListNode::compare(head, s.head);
- return value == 0;
- }
- int indexOf( char c ) const
- {
- int index = 0;
- for (ListNode *temp = head; temp != nullptr; temp = temp -> next)
- {
- if (temp -> info == c)
- return index;
- ++index;
- }
- return -1;
- }
- void print( ostream & out ) const
- {
- for (ListNode *temp = head; temp != nullptr; temp = temp -> next)
- out << temp -> info;
- }
- void read( istream & in )
- {
- NumAllocations += 1;
- char *buf = new char[256];
- in.getline(buf, 255);
- head = ListNode::convertList(buf);
- delete[] buf;
- NumAllocations -= 1;
- }
- ~MyString()
- {
- ListNode::deleteList(head);
- }
- };
- inline ostream & operator << ( ostream & out, const MyString & str )
- {
- str.print(out);
- return out;
- }
- inline istream & operator >> ( istream & in, MyString & str )
- {
- str.read(in);
- return in;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement