[英]Printing Linked List in C++
我的以下代码仅打印第一个元素。 在print_list()
函数中,它在打印第一个元素后停止。 它表示在第一个元素之后, head->next
为0
。 不应该指向第二个元素?
我想简单地打印整个列表。
#include<iostream>
#include<cstdlib>
using namespace std;
struct node {
int x;
node *next;
};
node* add_element(node*);
bool is_empty(node*);
void print_list(node*);
node* search(node*);
int main()
{
node *head;
head=NULL;
node* current=head;
for(int i=0;i<5;i=i+1)
{
if (current==NULL)
{
current=add_element(current);
head=current;
}
else{
current=add_element(current);
}
}
cout<<head->next<<endl;
// DOUBT: head->next gives NULL value. It should give me pointer to 2nd node
print_list(head);
}
node* add_element(node* current)
{
node* temp;
temp=new node;
temp->next=NULL;
cout<<"enter element"<<endl;
cin>>temp->x;
current=temp;
return current;
}
bool is_empty(node* temp)
{
return temp==NULL;
}
void print_list(node* temp)
{
if (is_empty(temp)==false)
{
cout<<"here temp(head)"<<temp->next<<endl;
while(temp!=NULL)
{
cout<<temp->x<<endl;
temp = temp->next;
}
}
}
你的问题在这里:
node* add_element(node* current)
{
node* temp; //You created a new node
temp=new node; //You allocated it here
temp->next=NULL; //You set its next property to null
cout<<"enter element"<<endl; //
cin>>temp->x;
current=temp; //This should be current->next = temp. You are overwriting it!
return current; //And now you are returning essentially the temp object that
//You created and you set its next property to NULL
}
您将在temp = new node
创建的节点分配给传入的当前节点。您要做的是将刚刚创建的节点分配给当前节点的下一个属性。 它应该是current->next = temp
打印功能打印第一个元素,因为链接列表中只有一个节点! 实际上在add_element(node*)
函数中存在错误,你用新节点覆盖head
节点的地址(因此有内存泄漏),如下所示:
node* add_element(node* current)
{
node* temp;
temp = new node; <---" You allocated memory"
temp->next = NULL; <---" Set next NULL"
cout<< "enter element" << endl;
cin>> temp->x; <---" Assign a value in new node"
// Replace below two line with suggested
current = temp; <---"MISTAKE: Overwrite first node"
"temp next is NULL so losing address of other nodes"
return current; <--- "return first node"
}
新节点(因此第一个节点)的下一个是NULL,因此打印功能仅打印第一个节点的值。
建议:
您应该如下更正以将新节点添加为链接列表中的第一个节点:
temp -> next = current; // new nodes next if present first node
return temp; // new code becomes first node
小心current
最初应为NULL。
我在add_element()
函数中的建议也改变了main()
的for循环代码,如下所示:
for(int i=0; i < 5; i = i + 1){
current = add_element(current);
}
head = current;
并检查Codepade上的工作代码(而不是用户输入我使用y = 100
变量添加值)。
编辑添加新节点:
您需要检查新节点是否是第一个节点(读取注释)。
// returns first node address in linked list = head
node* add_element(node* head){
node *temp, *new_nd;
// Create new node
new_nd = new node;
new_nd->next = NULL;
cout<<"enter element"<<endl;
cin>>new_nd->x;
// Is new node is the first node?
if(!head)
return new_nd;
// move to last
temp = head;
while(temp->next) temp = temp->next;
// add new node at last
temp->next = new_nd;
// return old head
return head;
}
还简单的main()如下:
int main(){
node *head = NULL;
for(int i = 0; i < 5; i = i + 1){
head = add_element(head);
}
print_list(head);
}
检查这个工作代码 。
head-> next是NULL,因为你在add_element()中设置了它。 要有一个链表,你应该设置current-> next = temp。
当您使用C ++时,您可能会考虑使用std :: list而不是实现自己的链接列表。
if (current==NULL)
{ current=add_element(current);
head=current;
}
else
{ current->next=add_element(current);
current=current->next;
}
正确的代码。 你必须在循环中做一个小的修正。 您必须添加一个新节点,然后使其指向当前节点的下一个节点。 所以简化的代码是current-> next = add_element(current),然后使当前指向新的当前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.