[英]How to convert an object to linked list in C++?
在我的.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;
}
但是對於字符串對象,
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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.