LinkedList C 中的反向打印
Reverse Printing in LinkedList C++
本文关键字:打印 LinkedList 更新时间:2023-10-16
我知道这个问题是问的,但我的问题与反向打印不同。
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;
我无法声明" #include String"或此文件的任何其他标题。
如果我喜欢这个
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:第一个预期输出:
3->2->1->
3->2->
3->
是:
printReverse(a);
printReverse(b);
printReverse(c);
// I believe it is like that.
您应该使用" std :: list"和一个像这样的反向迭代器
#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;
}
您将有此输出:
mylist backwards: 5 4 3 2 1
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- C++:正在检查LinkedList中的回文-递归方法-错误
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 回溯C++不打印函数,因此文件
- LinkedList C 中的反向打印
- 使用C++第2部分递归打印LinkedList
- 使用C++递归打印LinkedList
- 试图打印LinkedList的节点时出现代码错误