在我的.h文件中,我有两个函数:
String( const char * s = "");
String( const String & s );
我的目标是将这两个构造函数创建的对象转换为链表。所以我写了一个转换函数:
static ListNode * stringToList(const char *s)
{
ListNode * h = new ListNode(s[0], NULL);
ListNode * c = h;
for(int i = 0; s[i] != ' '; ++i)
{
c->next = new ListNode(s[i], NULL);
c = c->next;
}
return h;
}
我知道,也许在char*s的情况下,我们可以使用
String::String( const char * s)
{
ListNode::stringToList(s);
}
但在字符串对象的情况下,
String::String( const String & s )
我怎么可能那样做?我有点被链表弄糊涂了/此外,此函数之后的所有函数都使用(constString&s)参数。对链接列表进行操作变得更加困难,我很困惑如何通过链表完成对数据的操作,而中调用的所有参数都是"constString&s"。字符串对象
我的整个链接.h文件:
#include <iostream>
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(const char *s)
{
ListNode * h = new ListNode(s[0], NULL);
ListNode * c = h;
for(int i = 0; s[i] != ' '; ++i){
c->next = new ListNode(s[i], NULL);
c = c->next;
}
return h;
}
static ListNode * copy(ListNode * L)
{
return !L ? nullptr : new ListNode(L->info, copy(L->next));
}
static bool equal(ListNode * L1, ListNode * L2)
{
return (L1->info != L2->info) ? false : equal(L1->next, L2->next);
}
static ListNode * concat(ListNode * L1, ListNode * L2)
{
return !L1 ? copy(L2) : new ListNode(L1->info, concat(L1->next, L2));
}
static int compare(ListNode * L1, ListNode * L2)
{
return (!L1 && !L2) ? 0 : (L1->info > L2->info) ? 1 : (L1->info < L2->info) ? -1 : compare(L1->next, L2->next) ;
}
static int length(ListNode * L) // O(N) so call rarely
{
return L == nullptr ? 0 : 1 + length(L->next);
}
};
ListNode * head; // no other data members!! especially no len!
};
首先,stringToList
的实现需要进行一些更改。
static ListNode * stringToList(const char *s)
{
// What happens when the `s` is an empty string?
// You end up storing the null character.
ListNode * h = new ListNode(s[0], NULL);
ListNode * c = h;
// And then, you add s[0] again to the linked list.
for(int i = 0; s[i] != ' '; ++i)
{
c->next = new ListNode(s[i], NULL);
c = c->next;
}
return h;
}
更改为:
static ListNode * stringToList(const char *s)
{
ListNode * h = NULL;
if ( s[0] != ' ' )
{
h = new ListNode(s[i], NULL);
ListNode * c = h;
// Use 1 as the starting index for the iteration
for(int i = 1; s[i] != ' '; ++i)
{
c->next = new ListNode(s[i], NULL);
c = c->next;
}
}
return h;
}
但在字符串对象的情况下,
String::String( const String & s )
我怎么可能那样做?
它与其他构造函数没有太大区别。
String::String( const String & s ) : head(NULL)
{
ListNode* hr = s.head;
if ( hr != NULL )
{
head = new ListNode(hr->info, NULL);
List* cl = head;
ListNode* cr = hr->next;
for( ; cr != NULL; cr = cr->next)
{
cl->next = new ListNode(cr->info, NULL);
cl = cl->next;
}
}
}