最后一个节点未打印在链接列表中
我试图从列表的开头学习链接列表并执行插入操作。在打印节点时,未打印第一个节点。这是我写的核心功能。有人可以帮我吗?
struct Node //basic structure for a node
{
ll data; //data which we want to store
Node* link; //address of the next node;
};
Node* head=NULL;
void Insert(ll x) //insertion at beginning
{
Node* temp=new Node();
temp->data=x;
temp->link=head; //we are linking new node with previously connected node
head=temp;
}
void Print()
{
Node* temp=head;
while(temp->link!=NULL) //traversing the list until last element(last element.link = NULL)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
f(i,0,5)
{
ll x;cin>>x;
Insert(x);
}
Print();
return 0;
}
I was trying to learn the Linked list and perform insertion operations from beginning of the list. while printing the nodes, the first node is not printed. Here is the core functions which I have written. Can someone help me?
struct Node //basic structure for a node
{
ll data; //data which we want to store
Node* link; //address of the next node;
};
Node* head=NULL;
void Insert(ll x) //insertion at beginning
{
Node* temp=new Node();
temp->data=x;
temp->link=head; //we are linking new node with previously connected node
head=temp;
}
void Print()
{
Node* temp=head;
while(temp->link!=NULL) //traversing the list until last element(last element.link = NULL)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
f(i,0,5)
{
ll x;cin>>x;
Insert(x);
}
Print();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
打印
函数要求链接最后一个节点,否则不会打印。由于最后一个节点是从未链接的,因此永远不会打印。Your
Print
function requires that the last node is linked or it won't be printed. Since the last node is never linked, it will never be printed.这是因为您的检查时间。该节点将将链接集作为null,因此它将在不打印的情况下退出。我的建议是将while检查更改为(temp!= null),但您也可以通过放置cout&lt;&lt;也可以修复它。临时数据;循环之后
It's because of your check in the while. The node will have link set as NULL, and therefore it will exit the while without printing it. My recommendation is changing the while check to (temp != NULL), but you can also fix it by putting a cout << temp->data; after the loop
通常,函数
print
可以调用未定义的行为,因为它被称为空列表,因为expressiontemp-&gt; link
使用空指针来访问内存。另一个副作用是,由于段循环中的条件(如果列表只有一个节点,那么将不会输出其值),将跳过最后
节点
一个 一样称呼
可以像功能灵活 。您可以使用它在提供相应的文件流的文件中写入数据。
函数
插入
可以通过以下方式关注以下方式注意,在全局名称空间中声明指针
head
是一个坏主意。在这种情况下,所有函数都取决于全局变量,例如,您无法在程序中使用两个列表。因此,您应该在Main中声明指针。
例如,在这种情况
下
In general the function
Print
can invoke undefined behavior when it is called for an empty list due to the expressiontemp->link
that uses a null pointer to access memory.Another side effect is that the last node will be skipped due to the condition in the while loop (if the list has only one node then its value will not be outputted)
The function can be declared and defined the following way
And in main the function can be called like
The function is flexible. You can use it to write data in a file providing a corresponding file stream.
The function
Insert
can be simplified the following wayPay attention to that it is a bad idea to declare the pointer
head
in the global namespace. In this case all functions depend on the global variable and you can not for example to use two lists in your program.So you should declare the pointer in main.
In this case for example the function
Insert
can look the following wayand called in main like