-2
이 질문은 알고 있지만 내 문제는 그냥 역방향 인쇄와 다릅니다.LinkedList에서 역방향 인쇄 C++
void printReverse(ListNode* p){
if(!p)
return;
printReverse(p->next);
cout << (p->val) << "->";
return;
}
을 감안할 때 출력은 I가 원하는 것은
3->2->1->3->2->3->
입니다
3->2->1->
3->2->
3->
그것은 하나의 연결리스트이며
cout<< endl;
을 넣을 수있는 위치에 대한 내가 혼란 스러워요 나는 "#include string"또는 다른 어떤 것을 선언 할 수 없다. 이 파일에 ader. 나는이
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 : 나는 내 "reverseprint.ccp"다른 헤더 파일에 선언 할 수 없습니다. 그것은 규칙입니다. EDIT3 : 첫 번째 예상 출력 : 당신은 "표준 : : 목록"그
#include <iostream>
#include <list>
int main()
{
std::list<int> mylist;
for (int i=1; i<=5; ++i) mylist.push_back(i);
std::cout << "mylist backwards:";
for (std::list<int>::reverse_iterator rit=mylist.rbegin(); rit!=mylist.rend(); ++rit)
std::cout << ' ' << *rit;
std::cout << '\n';
return 0;
}
그리고 당신 같은 역 반복자를 사용한다
printReverse(a);
printReverse(b);
printReverse(c);
// I believe it is like that.
당신이 종이에 쓰여진 코드를 쳐다 보는지 알아내는 것은 까다로울 수 있습니다. 하지만 당신 앞에 컴퓨터가있어서 디버거를 가동시켜 코드가 어디로 갈 필요가 있는지보십시오! –
'cout << "\ n"'마지막'return;'의 직전에? –
내가 편집 한 것처럼, 만약 내가 마지막 반환 직전에 << "\ n"; 그것은 위와 같습니다. 뭔가를 놓치거나 뭔가 다른 것을해야합니까? – doodley