C++ 从用户输入C+;创建和打印链接列表+; #包括 使用名称空间std; int main() { 结构节点 { int数据; 节点*下一步; }; 节点*头; 节点*n; 节点*温度; 节点*q; 整数; 数量; n=新节点; n->data=number; 水头=n; 温度=n; 而(cin>>编号) { 而(数字!=-500) { n=新节点; n->data=number; 温度->下一步=n; 温度=n; } } while(head!=NULL) { cout数据; 头部=头部->下一步; } }
我不明白为什么这样不行。程序创建一个新节点,然后将用户输入的任何值设置为该新节点的变量数据,然后使head和temp指向该新节点。然后,它获取用户的第二个输入并将其与-500进行比较,如果它的计算结果为真,它将创建一个新节点,将第二个输入的数据放入变量数据中,然后将第一个节点和第二个节点链接在一起,然后将临时点指向第二个节点。如果第二个while循环的条件为false,则它将转到第三个while循环,在第三个while循环中,它将打印列表 谁将最后一个节点的next设置为NULL 在n=新节点;n->next不是NULL,但未定义,在调试版本中,它通常是0xCCCC或类似的值,以显示它未初始化。如果您尝试取消引用它,您将获得访问冲突C++ 从用户输入C+;创建和打印链接列表+; #包括 使用名称空间std; int main() { 结构节点 { int数据; 节点*下一步; }; 节点*头; 节点*n; 节点*温度; 节点*q; 整数; 数量; n=新节点; n->data=number; 水头=n; 温度=n; 而(cin>>编号) { 而(数字!=-500) { n=新节点; n->data=number; 温度->下一步=n; 温度=n; } } while(head!=NULL) { cout数据; 头部=头部->下一步; } },c++,C++,我不明白为什么这样不行。程序创建一个新节点,然后将用户输入的任何值设置为该新节点的变量数据,然后使head和temp指向该新节点。然后,它获取用户的第二个输入并将其与-500进行比较,如果它的计算结果为真,它将创建一个新节点,将第二个输入的数据放入变量数据中,然后将第一个节点和第二个节点链接在一起,然后将临时点指向第二个节点。如果第二个while循环的条件为false,则它将转到第三个while循环,在第三个while循环中,它将打印列表 谁将最后一个节点的next设置为NULL 在n=新节点;
#include <iostream>
using namespace std;
int main()
{
struct node
{
int data;
node * next;
};
node * head;
node * n;
node * temp;
node * q;
int number;
cout << "Enter numbers";
cin >> number;
n = new node;
n->data = number;
head = n;
temp = n;
while (cin >> number)
{
while (number != -500)
{
n = new node;
n->data = number;
temp->next = n;
temp = n;
}
}
while (head != NULL)
{
cout << head->data;
head = head->next;
}
}
如果在检查数字是否为-500后要中断,则可以执行以下操作:
while (cin >> number) { // this loop is endless, because you can always read the user input data (unless some exception happen)
while (number != -500)
{
n = new node; // you already have data allocated. no need to allocate it once more
n->data = number;
temp->next = n;
temp = n;
// as was already mentioned: set n->next to NULL
} }
顺便说一下,你有内存泄漏。如果使用扁平C指针,则考虑<代码>删除> /代码>运算符以清除内存。为此,您需要知道列表的起始位置(基本上是head),并通过调用delete node
遍历整个列表
也请考虑,编写一个类似的代码模式:
而(cin>>编号) 你也可以试试这个- 此代码在用户输入时生成一个链表,并打印LinkedList(函数)打印创建的链表 while (cin >> number) {
if (number != -500) { ...; // do your stuff
break;
}
}
#包括
使用名称空间std;
结构节点
{
int数据;
节点*下一步;
};
void printlinkedlist(节点*节点)
{
int c=0;//仅为好看的输出而采用
while(节点!=NULL)
{
如果(c>0)
{
cout>num;
head->data=num;
对于(int i=2;i>num;
温度->数据=num;
head->next=temp;//指向第二个节点的头部点,即temp
C++;
}
其他的
{
cin>>num;
node*temp1=new node;//为每个值初始化其他临时节点
temp1->data=num;
temp->next=temp1;//指向temp1到temp
temp=temp1;//将temp设置为temp1
}
}
打印链接列表(头);
}
解决此类问题的正确工具是调试器。在询问堆栈溢出问题之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您应该[编辑]您的问题将包括一个重现您的问题的示例,以及您在调试器中所做的观察。我已经在调试器中一步一步地运行了它。它获取了输入,但没有对其进行任何处理。即使第三次输入不等于-500,它也会在第三次输入后中断。while(number!=-500)
如果循环体中的number
没有更改,则永远循环。将while(number!=-500)
更改为if(number!=-500)
@TenealaSpencer您需要学习如何以更熟练的方式使用调试器。通常,如果答案中包含对代码的用途的解释,以及在不介绍其他内容的情况下解决问题的原因,则会更有帮助。
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* next;
};
void printlinkedlist(node* node)
{
int c=0; //taken just for good looking output
while(node!=NULL)
{
if(c>0)
{
cout<<"->"<<node->data;
node = node->next;
}
else
{
cout<<node->data;
node = node->next;
c++;
}
}
}
int main() {
int n;
cout<<"Enter no. of nodes=";
cin>>n; //User enters number of nodes he want.
int num,c=0; //initialized c for setting head with second node..
node* head = new node; //initialized head node
node * temp = new node; //initialized temp node
cin>>num;
head->data=num;
for(int i=2;i<=n;i++)
{
if(c==0)
{
cin>>num;
temp->data=num;
head->next=temp; //head point to second node i.e. temp
c++;
}
else
{
cin>>num;
node * temp1 = new node; //initialize other temp node for every value
temp1->data=num;
temp->next=temp1; //point to temp1 to temp
temp=temp1; //set temp as temp1
}
}
printlinkedlist(head);
}