【发布时间】:2019-08-21 07:29:26
【问题描述】:
我试图在列表的开头插入节点,但无法插入,我认为插入的逻辑似乎是正确的
void display(Node *t)
{
Node *p = t;
while (p != 0)
{
cout << p->data << ",";
p = p->next;
}
}
void Insert(Node *t,int pos,int x)
{
Node *temp=NULL;
if(pos==0)
{
temp=new Node;
temp->data=x;
temp->next=t;
t=temp;
}
}
int main()
{
Node *p = new Node;
Node *q = new Node;
p->data = 4;
p->next = q;
q->data=6;
q->next=NULL;
Insert(p,0,89);
Insert(p,0,80);
display(p);
}
我希望输出 80,89,4,6,
但我得到的实际输出是 4,6,
【问题讨论】:
-
Node在哪里定义?请提供minimal reproducible example -
Insert(t,0,89);t是什么? -
“但我得到的实际输出是 4,6”,不。请确保您发布的代码确实符合您的要求
-
您的详细信息有误,但您在发布的代码中省略了详细信息。细节很重要,请发布完整代码。
-
了解指针最重要的一点是,指针没有什么特别之处。分配给
Node*与分配给int没有区别。