gpt4 book ai didi

c++ - 如何在C++中将对象转换为链表?

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:44 25 4
gpt4 key购买 nike

在我的 .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] != '\0'; ++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 )

我怎么可能那样做?我对链表有点困惑/此外,此函数之后的所有函数都使用 (const String & s) 参数。在链接列表上操作变得更难,我很困惑如何通过链接列表完成对数据的操作,而调用的所有参数都是“const String & s”。字符串对象

我的整个 link.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] != '\0'; ++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] != '\0'; ++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] != '\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] != '\0'; ++i)
{
c->next = new ListNode(s[i], NULL);
c = c->next;
}
}
return h;
}

But in the case of a string object,

String::String( const String & s )

how can I possibly do that?

它与其他构造函数没有太大区别。

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;
}
}
}

关于c++ - 如何在C++中将对象转换为链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com