C++ 在c+中实现推送功能+;
我在创建推送函数(将节点添加到列表的前面)时遇到一些问题。 我知道,只要一个节点,你可以使用C++所提供的PUPH函数作为PuthSuffFrand(),但是我也能在这里使用它吗?p>C++ 在c+中实现推送功能+;,c++,linked-list,push,C++,Linked List,Push,我在创建推送函数(将节点添加到列表的前面)时遇到一些问题。 我知道,只要一个节点,你可以使用C++所提供的PUPH函数作为PuthSuffFrand(),但是我也能在这里使用它吗?p> struct Node { string val; Node* next; Node* prev; }; struct Stew { Node* first; Node* last; }; 其中,Stew结构定义为具有两个特殊指针,一个指向第一个元素,另一个指向最后一个元素。 节点结构在两
struct Node {
string val;
Node* next;
Node* prev;
};
struct Stew {
Node* first;
Node* last;
};
其中,Stew结构定义为具有两个特殊指针,一个指向第一个元素,另一个指向最后一个元素。
节点结构在两个方向上都有链接
在C++中使用这些类型的结构是新的,所以任何类型的帮助/提示都将非常感谢。我的尝试是:
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
q.first -> prev = inserted;
inserted -> next = q.first;
inserted -> val = q.first->val;
}
问题:我需要为此初始化头吗?以便:
Node *head = new Node();
head -> next = NULL;
head -> val = val;
感谢您的帮助。您的代码有几个问题:
void push (Stew& q, string val) {
Node *inserted = new Node();
// q.first can be NULL, so check before dereferencing it
q.first -> prev = inserted; // Looses any node that prev was
// pointing to before
inserted -> next = q.first;
inserted -> val = q.first->val; // you probably meant:
// inserted -> val = val;
}
您需要将
q.first
设置为第一个节点:
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
q.first -> prev = inserted;
inserted -> next = q.first;
inserted -> val = q.first->val; //although you maybe meant inserted -> val = val;
q.first=inserted;
}
我看不出你说的头在哪里 当然,类型为
Stew
的对象的数据成员first
和last
必须初始化为nullptr
或NULL
比如说
Stew q = {};
该函数可以如下所示
void push( Stew &q, const string &val )
{
Node *tmp = new Node { val, q.first, nullptr };
if ( q.first != nullptr ) q.first->prev = tmp;
q.first = tmp;
if ( q.last == nullptr ) q.last = q.first;
}
这里的函数是如何工作的,这是一个简单的例子
编辑:我在示例中添加了函数反转显示
#include <iostream>
#include <string>
struct Node {
std::string val;
Node* next;
Node* prev;
};
struct Stew {
Node* first;
Node* last;
};
void push( Stew &q, const std::string &val )
{
Node *tmp = new Node { val, q.first, nullptr };
if ( q.first != nullptr ) q.first->prev = tmp;
q.first = tmp;
if ( q.last == nullptr ) q.last = q.first;
}
void display( const Stew &q )
{
for ( Node *tmp = q.first; tmp; tmp = tmp->next )
{
std::cout << tmp->val << ' ';
}
}
void reverse_display( const Stew &q )
{
for ( Node *tmp = q.last; tmp; tmp = tmp->prev )
{
std::cout << tmp->val << ' ';
}
}
int main()
{
Stew q = {};
for ( char c = 'A'; c <= 'Z'; c++ )
{
push( q, std::string( 1, c ) );
}
display( q );
std::cout << std::endl;
reverse_display( q );
std::cout << std::endl;
return 0;
}
不要忘记编写一个函数,该函数将删除为节点分配的所有内存。创建Stew实例时,应将head和tail设置为NULL
Stew s;
s.first = NULL;
s.last = NULL;
如果使用类而不是结构,这将在构造函数中完成。
然后可以启动推送功能
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
inserted->val = val;
if (q.first == NULL) { // This list is currently empty
// Make the inserted element the only element
inserted->prev = NULL;
inserted->next = NULL;
q.first = inserted;
q.last = inserted;
}
else { // The list has elements
q.first->prev = inserted;
inserted->next = q.first;
inserted->prev = NULL;
q.first = inserted;
}
}
此代码可以简化一点(删除冗余代码)以
这里唯一的
c++
是引用和new
的使用。这是伪装成c++
的c
。。C没有参数的引用。以及引用和字符串。。。这是很好的C++。在<代码> PuxFrdAg/<代码>之后,代码应该是什么?为什么给定的val
没有指定inserted->val
?@user3358732:抓起一张纸,画一个包含多个元素的列表。然后绘制相同的列表,但前面还有一个元素。现在看看发生了什么变化(指针链接、项值、Stew的成员),并在代码中实现它。现在对接受新成员的空列表执行相同的操作。如果有新的事情需要考虑,请更新代码。这时你应该自己有答案了。我可以假设q.first
是头
,所以没有前面的。@πάνταῥεῖ 当列表为空时,q可以等于NULL。所以这个语句q.first->prev=inserted;将导致异常终止。@vladfrommoscoq
实际上是一个引用,因此不能为NULL
q.first
可以,这是代码中的另一个问题。@πάνταῥεῖ 我很抱歉。我是说q.first。我给了他a+1,但我认为这是因为他失去了q.first所指的任何东西?没有线索。然而,我更喜欢@πάνταῥεῖ '这是一个更好的解决方案。未做任何假设。@CantChooseServerNames问题中未指定它。也许q.first
是NULL
?那些投了反对票的人写评论有这么难吗?我想这只是一个人对这个问题的每个答案都投了反对票。@user3264405你是对的。有这样的业余爱好者喜欢下载每一篇文章。:)用现成的程序查看我的更新代码。最后一部分是你想要的答案-第一部分不会以任何方式添加到答案中。
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
inserted->val = val;
if (q.first == NULL) { // This list is currently empty
// Make the inserted element the only element
inserted->prev = NULL;
inserted->next = NULL;
q.first = inserted;
q.last = inserted;
}
else { // The list has elements
q.first->prev = inserted;
inserted->next = q.first;
inserted->prev = NULL;
q.first = inserted;
}
}
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
inserted->val = val;
inserted->prev = NULL;
inserted->next = q.first;
q.first = inserted;
if (q.first == NULL) { // This list is currently empty
// Make the inserted element the only element
q.last = inserted;
}
}