所以我是新来的C++对不起,如果这不是清除。C++指针链表
我有一个类:
class Item
{
int noItem;
int qItem;
public:
Item(int noItem, int qItem)
{
this->noItem = noItem;
this->qItem = qItem;
}
int getNoItem()
{
return noItem;
}
int getQntItem()
{
return qItem;
}
};
然后下面的类:
class Element
{
public:
Element()
{
data = NULL;
}
//to set and access data hold in node
void setElement(Item *data)
{
this->data = data;
}
Item* getElement(void)
{
return(data);
}
private:
Item *data;
};
这其中也:
class ListeChainee
{
public:
ListeChainee()
{
courant = NULL;
}
void ajoutListe(Item *data)
{
Element *newData;
//set data
newData->setElement(data);
//check if list is empty
if(courant == NULL)
{
//set current pointer
courant = newData;
}
}
//get data from element pointed at by current pointer
Item* elementCourant(void)
{
if(courant != NULL)
{
return courant->getElement();
}
else
{
return NULL;
}
}
private:
//data members
Element *courant; //pointer to current element in list
};
代码缺少一些东西,其他的事情,但我的问题是这样的:
int main(int argc, char* argv[])
{
ListeChainee listeCH;
Item i1(123,456);
listeCH.ajoutListe(&i1);
cout << listeCH.elementCourant()->getNoItem();
system("pause");
return 0;
}
我期待123被输出,但我看到一些其他的数字。不知道为什么。 谢谢。
在链接列表中,您有一个节点结构或类,它通常包含一个指向列表中“下一个”节点的“下一个”指针,和/或可选地指向前一个指针节点。 “list”本身将包含一个指向列表中第一个和/或最后一个节点的指针。 – rcgldr 2014-10-06 00:41:35
@imreal我在这里替换了真正的变量名,对不起应该选择了新的东西旁边的其他东西.. – Enriquev 2014-10-06 00:43:38
@rcgldr是的,我确实拥有所有这些,为了清楚起见,我只是在这里删除了它们。 – Enriquev 2014-10-06 00:44:41