0
我接近c + +和我正在一个类内的链接列表。我试图将一个字符数组传递给链表。不知何故,当我打印试图打印出来。它是相反的。我试图为什么是我的代码这样做,但我似乎无法弄清楚。链接列表在一个类的私人C + +
class foo{
public:
foo(const char * s =""){
head = Node::toList(s);
}
void print(ostream & in){
for(ListNode *p = head; p!= nullptr;p=p->next)
out << p->info;
}
private:
struct Node{
char info;
Node *next;
Node(char newInfo, Node *newNext):info(newNext),next(newNewxt){
}
static Node *toList(const char *s){
Node *temp = nullptr;
int x=0;
for(;s[x] != '\0';x++){
temp = new Node(s[x],temp); // Part where I do understand why I am getting reverse
}
return temp;
}
Node *head;
};
ostream & operator << (ostream & out, foo src){
src.print(out);
return out;
};
任何提示或建议将是伟大的。
decale ostream运营商为朋友。在打印内部,您可以使用<<但您需要的是<<(您已声明变量)如果解决了您的问题,请告诉我将其作为答案 – KostasRim
8'{'and only 7'}'可能需要解决这个问题。 – user4581301
想想你的输入中最后一个字母会发生什么,这就是从to_list()返回的结果。事实上,你有一个LIFO列表 - 又名堆栈。只需使用调试器 – pm100