只显示链表的最后一个元素
Only the last element of the linked list is displayed
我一直在尝试用 C++ 创建链表。但是只显示链表的最后一个元素。我已经搜索了错误,但我找不到它。我已经实现了我从 c 语言中学到的逻辑。所有节点都正确连接。但我仍然找不到错误。
这个逻辑适用于c语言。
请帮忙。
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
}*head,*newnode,*temp;
node* getnode();
node* create(int);
void display(node*);
int main()
{
int n;
head=getnode();
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *getnode()
{
head=new node();
head->next=nullptr;
return(head);
}
node *create(int n)
{
head=getnode();
cout<<"Enter the value of node 1: ";
cin>>head->data;
temp=getnode();
temp=head;
for(int i=1;i<n;i++)
{
newnode=getnode();
cout<<"Enter the value of node "<<i+1<<": ";
cin>>newnode->data;
newnode->next=nullptr;
temp->next=newnode;
temp=newnode;
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
使用局部变量
*head,*newnode,*temp
是全局变量。每次调用函数时,都会覆盖它们。使它们成为局部变量。
内存泄漏
您还通过以下方式在 main() 中泄漏了内存:
head=getnode();
并在 create() 中使用:
temp=getnode();
把它们放在一起
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
node(int x)
{
data=x;
next=nullptr;
}
}*head,*newnode,*temp;
node* create(int);
void display(node*);
int main()
{
int n;
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *create(int n)
{
for(int i=0;i<n;i++)
{
int x;
cout<<"Enter the value of node "<<i+1<<": ";
cin>>x;
newnode=new node(x);
if(i==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
我刚刚创建了一个用于创建新节点的构造函数,并使用了一个临时指针来跟踪列表中最后插入的元素。请记住,始终最好固定头指针并使用另一个指针进行遍历。您的代码的问题是您的头指针始终指向最后插入的元素。
我一直在尝试用 C++ 创建链表。但是只显示链表的最后一个元素。我已经搜索了错误,但我找不到它。我已经实现了我从 c 语言中学到的逻辑。所有节点都正确连接。但我仍然找不到错误。 这个逻辑适用于c语言。 请帮忙。
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
}*head,*newnode,*temp;
node* getnode();
node* create(int);
void display(node*);
int main()
{
int n;
head=getnode();
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *getnode()
{
head=new node();
head->next=nullptr;
return(head);
}
node *create(int n)
{
head=getnode();
cout<<"Enter the value of node 1: ";
cin>>head->data;
temp=getnode();
temp=head;
for(int i=1;i<n;i++)
{
newnode=getnode();
cout<<"Enter the value of node "<<i+1<<": ";
cin>>newnode->data;
newnode->next=nullptr;
temp->next=newnode;
temp=newnode;
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
使用局部变量
*head,*newnode,*temp
是全局变量。每次调用函数时,都会覆盖它们。使它们成为局部变量。
内存泄漏
您还通过以下方式在 main() 中泄漏了内存:
head=getnode();
并在 create() 中使用:
temp=getnode();
把它们放在一起
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
node(int x)
{
data=x;
next=nullptr;
}
}*head,*newnode,*temp;
node* create(int);
void display(node*);
int main()
{
int n;
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *create(int n)
{
for(int i=0;i<n;i++)
{
int x;
cout<<"Enter the value of node "<<i+1<<": ";
cin>>x;
newnode=new node(x);
if(i==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
我刚刚创建了一个用于创建新节点的构造函数,并使用了一个临时指针来跟踪列表中最后插入的元素。请记住,始终最好固定头指针并使用另一个指针进行遍历。您的代码的问题是您的头指针始终指向最后插入的元素。