我知道这个问题被问到,但我的问题不同于反向打印。
void printReverse(ListNode* p){
if(!p)
return;
printReverse(p->next);
cout << (p->val) << "->";
return;
}
给定输出
3->2->1->3->2->3->
我想要的是
3->2->1->
3->2->
3->
这是一个单一的链表,我对于放在哪里感到困惑
cout<< endl;
我不能声明&#34; #include string&#34;或该文件的任何其他标题。
如果我喜欢这个
void printReverse(ListNode* p){
if(!p)
return;
printReverse(p->next);
cout << (p->val) << "->";
cout << endl; //LOOK AT HERE, DIFFERENCE IS HERE
return;
}
然后我的输出是这样的:
3->
2->
1->
3->
2->
3->
编辑:对于想要查看其他输出的人: MyOutput中:
5->6->7->8->9->5->6->7->8->5->6->7->5->6->5->
我想要的是什么:
5->6->7->8->9->
5->6->7->8->
5->6->7->
5->6->
5->
这是主文件:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
int main(int argc, char const *argv[]){
ListNode a(1);
ListNode b(2);
ListNode c(3);
a.next = &b;
b.next = &c;
ListNode* d = new ListNode(9);
ListNode* e = new ListNode(8);
ListNode* f = new ListNode(7);
ListNode* g = new ListNode(6);
ListNode* h = new ListNode(5);
d->next = e;
e->next = f;
f->next = g;
g->next = h;
// The Program continues to another functions
// ....
// In somewhere here the program calls reverseprint function
return 0;
}
EDIT2:我无法向我的&#34; reverseprint.ccp&#34;提交任何其他标题。这是规则。 EDIT3:第一个预期输出:
3->2->1->
3->2->
3->
适用于:
printReverse(a);
printReverse(b);
printReverse(c);
// I believe it is like that.