在C++中创建一个反向LinkedList从给定的 LinkedList
我在从给定的链接列表以相反的顺序创建链接列表时遇到一些问题。
我有java背景,刚刚开始做一些C++。
你能检查一下我的代码并看看有什么问题吗?我猜我只是在操纵指针而不是创建任何新内容。
//this is a method of linkedlist class, it creates a reverse linkedlist
//and prints it
void LinkedList::reversedLinkedList()
{
Node* revHead;
//check if the regular list is empty
if(head == NULL)
return;
//else start reversing
Node* current = head;
while(current != NULL)
{
//check if it's the first one being added
if(revHead == NULL)
revHead = current;
else
{
//just insert at the beginning
Node* tempHead = revHead;
current->next = tempHead;
revHead = current;
}
current = current->next;
}//end while
//now print it
cout << "Reversed LinkedList: " << endl;
Node* temp = revHead;
while(temp != NULL)
{
cout << temp->firstName << endl;
cout << temp->lastName << endl;
cout << endl;
temp = temp->next;
}
}//end method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
更简单的方法:遍历链表,保存上一个和下一个节点,然后让当前节点指向前一个节点:
Easier one: Go through your linked list, save the previous and the next node and just let the current node point at the previous one:
您不初始化
revHead
,但您使用它。(我希望您已经清楚
revHead
是一个用于存储内存地址的局部变量,而不是存在于方法/过程之外的东西)revHead
的存储类code> 是自动的(也称为本地作用域主体)。在C++
中,当您进行这样的声明时,不能保证该值将为0
。(除非存储类是
static
类型或变量是global
,如果没有提供其他值,它会自动初始化为0
。您的情况是,变量具有auto
类型的存储类,这意味着它是在函数中本地定义的,并且在声明局部变量时,如果不指定值,则该值是垃圾。接下来的 C++ 标准C++0x
关键字auto
有了新的含义)。您的情况中的值是垃圾,这会使
if
失败。请在此处查看更多信息:链接请记住
,也许您的代码的其他部分也可能出现类似的错误。
You don't initialize
revHead
but you use it.(I hope it is already clear to you that
revHead
is a local variable used to store a memory address, and not something that exists outside the method/procedure)The Storage Class of
revHead
is automatic (aka in the local scope-body). InC++
when you do a declaration like that, there is not guarantee that the value will be0
.(unless the storage class is of type
static
or the variable isglobal
where it is automatically initialized to0
if no other value is provided. In your case the variable has storage class of typeauto
which means it is locally defined in a function, and when declaring a local variable, without specifying a value, the value is garbage. Keep in mind that with the next C++ StandardC++0x
the keywordauto
has a new meaning).The value in your case is garbage which makes the
if
fail. See more Information here : LinkDo a
Keep in mind that maybe you may have errors like that in other part of your code as well.
只需使用两个临时变量即可完成此操作。
另外,您可以使用递归来完成此操作。
This is done using just two temporary variables.
Also, you can do this using recursion.
另一种方法是首先遍历列表并将所有数据存储在堆栈中,然后创建一个新列表并从堆栈顶部插入数据。堆栈是 LIFO 将以相反的顺序为您提供数据,因此您将得到一个颠倒列表。
Another method would be to first traverse the list and store all data in a stack,then create a new list and insert data in it from top of the stack.Stack being LIFO will give you the data in reverse order and hence you will have a reversed list.
我不确定,但我认为你想要一个双向链表,其中节点有下一个和上一个。使用指向列表的外部指针将无法工作。您将不会获得前一个节点的地址。
如果不使用上面的方法与堆栈,这是一个很好的建议。
I'm not sure, but I think you want a doubly linked list where the node has a next and previous. It will not work using an external pointer to the list. You will not have the address of the previous node.
If not use the method above with a stack it's a good suggestion.
下面的示例使用递归来反转链接列表。我在面试时问了这个问题。这已经过测试并且有效。 ListElem 是节点。
The sample below use the recursion for reversing a linklist. I asked this Qs at a job interview. This has been tested and works. ListElem is the node.
上面是Link List的反转
The above is a reverse of Link List
我使用类的反向链表的完全可执行解决方案,因为人们发现的大多数示例仅使用结构:
我对生产代码的建议是使用
std::list 因为它是一个链接列表
或 std::vector 如果您需要高效的数组实现。
My fully executable solution for a reversed linked list using a class since most examples one finds are using just a struct:
My recommendation for production code is using
the std::list since it is a linked list
or a std::vector if you need an efficient array implementation.