[英]How to convert an object to linked list in C++?
In my .h file, I have two functions: 在我的.h文件中,我有两个功能:
String( const char * s = "");
String( const String & s );
My aim is to convert objects created by these two constructors into linked list. 我的目标是将这两个构造函数创建的对象转换为链接列表。 So I write a function for converting:
所以我写了一个转换函数:
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;
}
I am aware that maybe in the case of char*s, we can use 我知道也许在char * s的情况下,我们可以使用
String::String( const char * s)
{
ListNode::stringToList(s);
}
But in the case of a string object, 但是对于字符串对象,
String::String( const String & s )
how can I possibly do that? 我该怎么做? I am a bit confused by linked list/ Besides, all the functions after this one are using ( const String & s ) parameter.
我对链表/感到有些困惑,此外,此后的所有函数都使用(const String&s)参数。 It becomes harder to operate on link list, I am very confused on how to accomplish operations on data by link list while all the parameters called in are "const String & s " .
在链接列表上操作变得越来越困难,我对如何通过链接列表完成对数据的操作感到困惑,而所调用的所有参数都是“ const String&s” 。 a string object
字符串对象
My entire link.h file: 我的整个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!
};
First of all, your implementation of stringToList
needs a little change. 首先,您对
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;
}
Change it to: 更改为:
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?
我该怎么做?
It's not too different from the other constructor. 与其他构造函数没有太大区别。
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.