我如何使用C ++以相反的顺序打印此单个链表中的元素
how i can print the elements in this single linked list in reverse order using c++
#include <iostream>
#include<string>
using namespace std;
struct nodeType
{
int info;
nodeType *next;
};
class linkedListType
{
private:
nodeType *first, *last;
int length;
public:
linkedListType()//constructor
{
first = last = NULL;
length = 0;
}
void print() // normal print
{
nodeType * current = first;
while (current != NULL)
{
cout << current->info <<" ";
// update statement
current = current ->next;
}
}
void insertEnd(int item) //insert item to the end of the list
{ // forward insertion
nodeType* newNode = new nodeType;
newNode ->info = item;
if (length == 0)
{
first = last = newNode;
newNode->next = NULL;
}//if
else
{
last->next = newNode;
last = newNode;
newNode->next = NULL;
}// else
length++;
}
}
void clearList()
{
nodeType * current;
while ( first != NULL)
{
current = first;
first = first->next;
delete current;
length--;
}// while
~linkedListType() //destroctor
{
clearList();
}
> `
//
块引用我不能写这个方法实现请任何人帮助我并解释为什么/// 谁能帮我写信给我,解释为什么/ ////////////////////////////////////////////////////////////////////
`
void printReverse() /*this is the function that i cant understand it or complete it. this function print elements in the list in reverse*/
{
nodeYype* current=last ,*newnode =new nodType ;
for(int i=length;i>=0;i--)
//i cant complete this method
}
};
void main()
{
linkedListType list1;
list1.insertEnd(12); //insert item
list1.insertEnd(25);//insert item
list1.insertEnd(18);//insert item
list1.insertEnd(37);//insert item
list1.insertEnd(60);//insert item
list1.insertEnd(100);//insert item
list1.insertEnd(37);//insert item
list1.insertEnd(37);//insert item
list1.insertEnd(37);//insert item
list1.insertEnd(60);//insert item
list1.insertEnd(25);//insert item
list1.insertEnd(100);//insert item
list1.insertEnd(25);//insert item
cout <<"Printing the linked list elementsn";
list1.print();
cout <<"nPrinting the list elements in reverse ordern";
list1.printReverse();
}
void nodeType::PrintListReverse()
{
if (next)
next->PrintListReverse();
std::cout << info << std::endl;
}
递归地查找列表的末尾,在返回时打印。
(我只是因为无聊才启用你)
或者:
void linkedListType::PrintList()
{
std::vector<int> info(length);
nodeType* curNode = first;
for (int i = 0; curNode != NULL; i++, curNode = curNode->next)
{
info[i] = curNode->info;
}
for (int i = length-1; i >=0; i--)
{
std::cout << info[i] << std::endl;
}
}
如果可以编写递归函数以正确的顺序遍历列表,则以相反的顺序打印它很容易。
有两种可能性。要么编写递归函数,要么以相反的顺序重建列表。也就是说,在打印列表之前,您可以在现有的基础上创建一个新列表,也可以重建原始列表本身。
你已经有一个循环,它将 i 从长度递减到 0。 基于 i,您可以遍历列表并打印您到达的节点。 微调 1 个错误,以便您实际从最后一个打印到第一个,并且在列表为空时不打印。
相关文章:
- 如何在C++中递归地按相反顺序打印集合
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 按字母顺序打印存储在字符数组中的信息
- 视觉C++未按所需顺序打印
- C++使用相反的顺序打印带有逗号的列表
- C/C++ - 用于按顺序打印数字的 sem_t 类型的单个信号量
- 对象指针打印结果以相反的顺序进行
- 链表缺少节点并在打印时颠倒顺序
- 如何以相反顺序C 打印整数的单个数字
- 我不知道如何创建一个以数字/字母顺序打印出我的整数或字符串的程序
- 在 C Linux 中使用三个线程使用信号量同步按顺序打印 3 4 5 50 次
- 以相反的顺序打印链表的后半部分
- 我如何附加双链接以指向正确的节点,以相反的顺序打印
- 为什么在打印东西时没有保留顺序,先用cerr,然后用cout
- 按字母顺序打印结构输入C
- 按顺序打印时遇到问题,二叉树的后顺序等
- 打印所有可能的组合,包括重复,顺序也很重要
- 按特定顺序打印队列
- C++ 中的打印顺序
- 为什么这段代码的打印顺序不像我预期的那样工作?