尝试在链接列表的末尾进行更简单的插入 program.Help 需要一些小问题
trying to make a simpler insertion at end of linked list program.Help needed with minor issues
我已经成功地在链表程序的开头创建了一个简单的插入,但现在我正在为链表末尾的插入而苦苦挣扎。
该程序似乎能够从用户那里获取值,但没有输出列表correct.Could你能帮帮我吗?
如果可能的话,请遵循我的程序,因为我是初学者,无法理解完全不同的方法。
我使用的逻辑-
如果列表为空则在开头插入值否则如果列表不为空则沿着列表移动直到指向的下一个值是 NULL 然后输入新值代替 NULL。
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *start=NULL;
void insertend(int x)
{
node* temp=new node;
if(start==NULL)
temp->data=x;
else
{
while(temp!=NULL)
{
temp=temp->next;
}
temp->next=x;
}
}
void display()
{
node* disp=new node;
while(disp!=NULL)
{
cout<<disp->data<<endl;
disp=disp->next;
}
}
int main()
{
int x;
char ch;
do
{
cout<<"Enter data";cin>>x;
cout<<endl;
insertend(x);
cout<<"Do you want to continue?(y/n)";cin>>ch;
cout<<endl;
}while(ch=='y');
cout<<"Your list:"<<endl;
display();
}
列表的入口点是变量 start
。但是你从来没有设置它。以用户输入的第一项为例。您将调用 insertend()
,它会检查 start == NULL
,但它永远不会设置 start
。您必须设置 start = temp
或类似的东西。您在 else
部分遇到了同样的问题——您遍历以 temp
开头的节点,但您应该从 start
开始。再次在函数 display()
中,您创建一个指向节点的指针并从它开始循环,但它没有数据——您应该使用 start
作为循环的起点。
struct node{
int data;
node* next;
};
node *first = NULL, *last = NULL;
void insert(int x){
if(first == NULL){
first = new node;
first->data = x;
first->next = NULL;
}else if(last == NULL){
last = new node;
last->data = x;
first->next = last;
last->next = NULL;
}else{
node *n = new node;
n->data = x;
n->next = NULL;
last->next = n;
last = n;
}
}
如您所见,我正在跟踪列表中的第一个和最后一个节点。 Insert 函数检查列表中是否有 if(first == NULL)
部分的内容。如果没有,它会创建第一个节点。 else if 也会发生类似的事情。最后,在 else 块中,我们创建了一个包含数据 x 的新节点。然后将存储在变量 last 中的节点指向我们的新节点,并将 last 设置为该节点。
这里是显示函数:
void display()
{
node *disp =first;
while(disp->next != NULL){
cout << disp->data << " ";
disp = disp->next;
}
cout << disp->data;
}
我还建议您在程序完成后进行清理运行,因为您正在创建新节点。
void cleanup(node* n)
{
if(n->next == NULL)return delete n;
cleanup(n->next);
delete n;
}
然后在主调用结束时 cleanup(first)
希望这是有道理的:)祝你有愉快的一天!
我已经成功地在链表程序的开头创建了一个简单的插入,但现在我正在为链表末尾的插入而苦苦挣扎。
该程序似乎能够从用户那里获取值,但没有输出列表correct.Could你能帮帮我吗?
如果可能的话,请遵循我的程序,因为我是初学者,无法理解完全不同的方法。
我使用的逻辑- 如果列表为空则在开头插入值否则如果列表不为空则沿着列表移动直到指向的下一个值是 NULL 然后输入新值代替 NULL。
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *start=NULL;
void insertend(int x)
{
node* temp=new node;
if(start==NULL)
temp->data=x;
else
{
while(temp!=NULL)
{
temp=temp->next;
}
temp->next=x;
}
}
void display()
{
node* disp=new node;
while(disp!=NULL)
{
cout<<disp->data<<endl;
disp=disp->next;
}
}
int main()
{
int x;
char ch;
do
{
cout<<"Enter data";cin>>x;
cout<<endl;
insertend(x);
cout<<"Do you want to continue?(y/n)";cin>>ch;
cout<<endl;
}while(ch=='y');
cout<<"Your list:"<<endl;
display();
}
列表的入口点是变量 start
。但是你从来没有设置它。以用户输入的第一项为例。您将调用 insertend()
,它会检查 start == NULL
,但它永远不会设置 start
。您必须设置 start = temp
或类似的东西。您在 else
部分遇到了同样的问题——您遍历以 temp
开头的节点,但您应该从 start
开始。再次在函数 display()
中,您创建一个指向节点的指针并从它开始循环,但它没有数据——您应该使用 start
作为循环的起点。
struct node{
int data;
node* next;
};
node *first = NULL, *last = NULL;
void insert(int x){
if(first == NULL){
first = new node;
first->data = x;
first->next = NULL;
}else if(last == NULL){
last = new node;
last->data = x;
first->next = last;
last->next = NULL;
}else{
node *n = new node;
n->data = x;
n->next = NULL;
last->next = n;
last = n;
}
}
如您所见,我正在跟踪列表中的第一个和最后一个节点。 Insert 函数检查列表中是否有 if(first == NULL)
部分的内容。如果没有,它会创建第一个节点。 else if 也会发生类似的事情。最后,在 else 块中,我们创建了一个包含数据 x 的新节点。然后将存储在变量 last 中的节点指向我们的新节点,并将 last 设置为该节点。
这里是显示函数:
void display()
{
node *disp =first;
while(disp->next != NULL){
cout << disp->data << " ";
disp = disp->next;
}
cout << disp->data;
}
我还建议您在程序完成后进行清理运行,因为您正在创建新节点。
void cleanup(node* n)
{
if(n->next == NULL)return delete n;
cleanup(n->next);
delete n;
}
然后在主调用结束时 cleanup(first)
希望这是有道理的:)祝你有愉快的一天!