Created
November 12, 2014 01:42
-
-
Save dddiaz/cbcab5717d5c12acb013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Im tring to implement a string with a linked list | |
using namespace std; | |
class String | |
{ | |
public: | |
/// Both constructors should construct | |
/// from the parameter s | |
String( const char * s = ""); | |
String( const String & s ); | |
String operator = ( const String & s ); | |
char & operator [] ( const int index ); | |
int length() const {return ListNode::length(head);} | |
int indexOf( char c ) const; | |
bool operator == ( const String & s ) const; | |
bool operator < ( const String & s ) const; | |
/// concatenates this and s | |
String operator + ( const String & s ) const; | |
/// concatenates s onto end of this | |
String operator += ( const String & s ); | |
void print( ostream & out ); | |
void read( istream & in ); | |
~String(); | |
private: | |
bool inBounds( int i ) | |
{ | |
return i >= 0 && i < length(); | |
} | |
struct ListNode | |
{ | |
char info; | |
ListNode * next; | |
ListNode(char newInfo, ListNode * newNext) | |
: info( newInfo ), next( newNext ) | |
{ | |
}// HINT: some primitives you *must* write and use, recursion? | |
static ListNode * stringToList(char *s);//Yes, I just fixed this | |
static ListNode * copy(ListNode * L); | |
static bool equal(ListNode * L1, ListNode * L2); | |
static ListNode * concat(ListNode * L1, ListNode * L2); | |
static int compare(ListNode * L1, ListNode * L2); // just fixed | |
static int length(ListNode * L); // just added, O(N) so call rarely | |
}; | |
ListNode * head; // no other data members!! - especially no len! | |
}; | |
ostream & operator << ( ostream & out, String str ); | |
istream & operator >> ( istream & in, String & str ); | |
//LIST NODE | |
String::ListNode * String::ListNode::stringToList(char * s){ | |
if (s[0] != '\0'){ | |
ListNode * tempHead = new ListNode(*s,stringToList(s++)); | |
return tempHead; | |
} else { | |
return nullptr; | |
} | |
} | |
//STRING | |
String::String( const char * s ){ | |
head = nullptr; | |
head = ListNode::stringToList((char *)s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment