C++ push_front()作为链表成员函数
如何将单个链表的C++ push_front()作为链表成员函数,c++,linked-list,C++,Linked List,如何将单个链表的push_front()方法作为其成员函数来实现?下面的代码无法编译(错误:赋值的左操作数需要左值),因为您无法赋值给此指针。这有什么办法 #include<algorithm> using namespace std; class ListElem{ public: ListElem(int val): _val(val){} ListElem *next() const { return _next; } void next(ListEl
push_front()
方法作为其成员函数来实现?下面的代码无法编译(错误:赋值的左操作数需要左值),因为您无法赋值给此
指针。这有什么办法
#include<algorithm>
using namespace std;
class ListElem{
public:
ListElem(int val): _val(val){}
ListElem *next() const { return _next; }
void next(ListElem *elem) { _next = elem; }
void val(int val){ _val = val; }
int val() const { return _val;}
void print();
void push_front(int);
private:
ListElem *_next;
int _val;
};
void ListElem::push_front(int val)
{
ListElem *new_elem = new ListElem(val); //new node
new_elem->next( this ); // new node points to old head
this = new_elem; // make new node the new head, error!
return;
}
void ListElem::print()
{
ListElem *pelem = this;
while(ListElem *pnext_elem = pelem->next())
{
cout << pelem->val() << ' ';
pelem = pnext_elem;
}
cout << pelem->val() << endl;
}
int main()
{
//initialization
ListElem *head = new ListElem(1);
ListElem *elem = head;
for (int ix = 2; ix < 10; ++ix)
{
ListElem *elem_new = new ListElem(ix);
elem -> next(elem_new);
elem = elem_new;
}
head->print();
//insert at the beginning
head->push_front(7);
head->print();
}
#包括
使用名称空间std;
类列表{
公众:
列表项(int val):_val(val){}
ListElem*next()常量{return\u next;}
void next(ListElem*elem){{u next=elem;}
void val(int val){u val=val;}
int val()常量{return_val;}
作废打印();
前推空(int);
私人:
李斯特伦*下一步;
国际价值;
};
void listlem::向前推(int val)
{
ListElem*new_elem=new ListElem(val);//新节点
new_elem->next(this);//新节点指向旧头部
this=new_elem;//将新节点设为新头,错误!
返回;
}
void listlem::print()
{
ListElem*pelem=这个;
while(ListElem*pnext\u elem=pelem->next())
{
cout val()打印();
//在开头插入
头部->向前推(7);
打印头->打印();
}
您不正确地使用了此
。您希望调用静态
成员,例如,ListElem*head
,并在使用此
的地方使用它。您还必须对其进行初始化。您不正确地使用了此
。您希望调用静态
成员,例如,李stElem*head
并在您使用this
的地方使用它。您还必须初始化它。逻辑上,push_front()必须是List
类的方法,而不是liselement
类的方法。逻辑上,push_front()必须是列表
类的方法,而不是列表元素
类的方法如果您确实想这样做,可以这样做:
void ListElem::push_front(int val)
{
ListElem *new_elem = new ListElem(_val);
_val = val;
new_elem->next(_next);
_next = new_elem;
}
这将用新数据替换“当前”节点中的数据,并将“当前”数据移动到新节点,从而产生相同的列表内容。
但是,将列表与其节点合并在一起是不正确的
<>你链接的书对整个事情都采用了非常非面向对象的方法(java和C++示例看起来都像音译C),并且把列表的类型与它的节点类型相混淆是很容易导致bug的。
例如,如果你这样做
ListElem* x = head;
head->push_front(99);
然后,*x
的内容将发生变化,这并不是您所期望的。如果您真的想这样做,您可以这样做:
void ListElem::push_front(int val)
{
ListElem *new_elem = new ListElem(_val);
_val = val;
new_elem->next(_next);
_next = new_elem;
}
这将用新数据替换“当前”节点中的数据,并将“当前”数据移动到新节点,从而产生相同的列表内容。
但是,将列表与其节点合并在一起是不正确的
<>你链接的书对整个事情都采用了非常非面向对象的方法(java和C++示例看起来都像音译C),并且把列表的类型与它的节点类型相混淆是很容易导致bug的。
例如,如果你这样做
ListElem* x = head;
head->push_front(99);
然后,*x
的内容将发生更改,这并不是您所期望的。push\u-front是列表上的一个方法,我看到的只是一个列表节点类。@Borgeader,我以为链表的头就是列表本身()。这不是一个好方法吗?我不会说“经常”。这当然不是std库中的做法。而且它不支持推送前端。您希望看到什么head->next()->push\u-front(0)
怎么办?@molbdnilo,你说得对,把这个方法作为节点的成员来实现是没有意义的。push_front是列表上的一个方法,我看到的只是一个列表节点类。@Borgeader,我以为链表的头就是列表本身()。这不是一个好方法吗?我不会说“经常”。这当然不是std库中的做法。而且它不支持push_-front。您希望head->next()->->push_-front(0)
做什么?@molbdnilo,您是对的,作为节点的成员实现此方法没有意义。我明白了,所以我无法实现push_-front()如果我的链表被定义为头节点?你应该有一个类列表,它使用这个对象作为它的节点。我明白了,所以我不能实现push_front()如果我的链表被定义为头节点?你应该有一个类列表,它使用这个对象作为它的节点。这意味着你在整个程序中只能有一个列表。这意味着你在整个程序中只能有一个列表。