cout<<L->data<<" ";
时间: 2023-10-30 17:40:31 浏览: 32
This line of code will output the value of the "data" attribute of the node pointed to by the pointer variable L. The "<<" operator is the output stream operator in C++, and "data" is assumed to be a variable or attribute within the node structure that holds the data value for that node. The output will be printed to the console or standard output.
相关问题
检查代码:#include <iostream> using namespace std; struct node { int data; struct node *next; }; void show(node * l) { l=l->next; while(l!=NULL) { cout<<l->data<<" "; l=l->next; } } void create(node * l,int a[],int n) { l=new node; l->next=NULL; node *p; for(int i=0;i<n;i++) { p=new node; p->data=a[i]; p->next=l->next; l->next=p; } } int main() { int a[100],A,a1=0; for(int i=0;;i++) { cin>>A;if(A==0)break; else {a[i]=A;a1++;} } node *l; l=new node; create(l,a,a1); show(l); return 0; }
这段代码已经解决了之前提到的问题,可以正常运行。但是,还有一个小的改进建议:
在 create 函数中,你使用了 l->next=p; 这句话,这会将新建的节点 p 插入到链表的第一个位置,导致链表的顺序与输入数组的顺序相反。如果你想要链表的顺序与输入数组的顺序相同,应该将新建的节点插入到链表的最后一个位置。修改的方式是,首先找到链表的最后一个节点,然后将新建的节点插入到它的后面。修改后的 create 函数如下所示:
```cpp
void create(node * l,int a[],int n) {
l->next=NULL;
node *p=l;
for(int i=0;i<n;i++)
{
node *new_node=new node;
new_node->data=a[i];
new_node->next=NULL;
p->next=new_node;
p=new_node;
}
}
```
这样,你就可以保证链表的顺序与输入数组的顺序相同了。
//添加功能 void Create_LinkList(LinkList& L) { system("cls"); cout << "\t\t\t添加联系人功能" << endl; Node* t = L; int i = 1, flag = 1; while(t->next) { i++; t = t->next; } while (flag) { Node* p = new Node; cout<< "\t\t\t输入姓名:"; cin>> p->data.name; cout<< "\t\t\t输入性别:"; cin>> p->data.sex; cout<< "\t\t\t输入年龄:"; cin>>p->data.age; cout<< "\t\t\t输入电话:"; int m=1; while(m) { cin>>p->data.telephone; int n=p->data.telephone.length(); if(n!=11) { cout<< "\t\t\t输入格式错误,请重新输入:"; m=1; } else { for(int j=0;j<n;j++) { if(!(p->data.telephone[j]>='0' && p->data.telephone[j]<='9')) { cout<< "\t\t\t输入格式错误,请重新输入:"; m=1; break; } } m=0; } } cout << "\t\t\t输入地址:"; cin >> p->data.address; cout << "\t\t\t输入分组:"; cin >> p->data.relation; p->data.num = i++; p->next = NULL; t->next = p; t = t->next; Save(L); cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl; cout << "\t\t\t请选择【0-1】:"; cin >> flag; } }修改一下
void Create_LinkList(LinkList& L) {
system("cls");
cout << "\t\t\t添加联系人功能" << endl;
Node* t = L;
int i = 1, flag = 1;
while (t->next) {
i++;
t = t->next;
}
while (flag) {
Node* p = new Node;
cout << "\t\t\t输入姓名:";
cin >> p->data.name;
cout << "\t\t\t输入性别:";
cin >> p->data.sex;
cout << "\t\t\t输入年龄:";
cin >> p->data.age;
cout << "\t\t\t输入电话:";
int m = 1;
while (m) {
cin >> p->data.telephone;
int n = p->data.telephone.length();
if (n != 11) {
cout << "\t\t\t输入格式错误,请重新输入:";
m = 1;
}
else {
for (int j = 0; j < n; j++) {
if (!(p->data.telephone[j] >= '0' && p->data.telephone[j] <= '9')) {
cout << "\t\t\t输入格式错误,请重新输入:";
m = 1;
break;
}
}
m = 0;
}
}
cout << "\t\t\t输入地址:";
cin >> p->data.address;
cout << "\t\t\t输入分组:";
cin >> p->data.relation;
p->data.num = i++;
p->next = NULL;
t->next = p;
t = t->next;
Save(L);
cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl;
while (1) {
cout << "\t\t\t请选择【0-1】:";
cin >> flag;
if (flag == 0 || flag == 1) break;
else cout << "\t\t\t输入错误,请重新输入!" << endl;
}
}
}
修改后的代码加入了一个循环,可以防止用户输入非0和1的值。同时,将flag的赋值放到循环内部,可以保证每次循环结束时flag都被赋值为0或1,避免出现不确定的情况。