Node* p1,*p2;这样定义对吗
时间: 2023-08-10 20:03:57 浏览: 61
是的,`Node* p1, *p2;` 这样的语法是正确的。这条语句定义了两个指向 `Node` 类型的指针变量 `p1` 和 `p2`。使用 `*` 表示指针类型,而 `Node` 表示指针所指向的对象类型。
需要注意的是,在这个定义中,`p1` 和 `p2` 都是指针变量,它们存储的是内存地址,而不是实际的对象。你可能需要在使用这些指针之前为它们分配内存,或者将它们指向已经存在的对象。
另外,为了避免悬挂指针(dangling pointers)的问题,建议在定义指针后,要么立即为其分配内存,要么将其初始化为 `nullptr`,然后在使用之前进行适当的赋值。
相关问题
创建动态链表并打印链表内容。要求通过键盘读入学生的学号和分数,当输入的学号为0时表示链表创建结束,并将读入的学生学号 和成绩打印输出。下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。改错时不能改变程序原有 的意图。 注意:将修改后的完整的源程序写在答题区内。 #包括< stdio。h> #定义struct节点的SIZE) 结构节点 { 长num; 浮动得分; Struct节点*next; } int主要() { int n = 0; struct node *head =NULL, *p1, *p2; p1 =p2 =(struct node*) malloc(SIZE); printf("输入%d节点数据:\n", n +1); Scanf ("%d%f", &p1->num, &p1->score); 而全国矿工工会(p1 - > !=0) { 如果(+ + n == 1) 头= p1; 其他的 p2 - > = p1; P1 = p2; p1 =(struct node*) malloc(SIZE); printf("输入%d节点数据:\n", n +1); Scanf ("%d%f", &p1->num, &p1->score); } 免费的(p1); printf("坑列表:\ n");
程序存在以下问题:
1. 结构体定义中的“长”应该改为“int”;
2. 变量head的类型应为“struct node*”;
3. 在给p2赋值时,应该先将p1赋给p2的next,再将p2赋给p1,否则会导致链表断掉;
4. 打印输出时应该用“\n”而不是“\ n”。
修改后的程序如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define SIZE sizeof(struct node)
struct node {
int num;
float score;
struct node *next;
};
int main() {
int n = 0;
struct node *head = NULL, *p1, *p2;
p1 = p2 = (struct node*)malloc(SIZE);
printf("输入%d节点数据:\n", n + 1);
scanf("%d%f", &p1->num, &p1->score);
while (p1->num != 0) {
++n;
if (n == 1) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (struct node*)malloc(SIZE);
printf("输入%d节点数据:\n", n + 1);
scanf("%d%f", &p1->num, &p1->score);
}
free(p1);
p2->next = NULL;
printf("链表内容:\n");
p1 = head;
while (p1 != NULL) {
printf("学号:%d,分数:%.2f\n", p1->num, p1->score);
p1 = p1->next;
}
return 0;
}
```
创建动态链表并打印链表内容。要求通过键盘读入学生的学号和分数,当输入的学号为0时表示链表创建结束,并将读入的学生学号和成绩打印输出。下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。改错时不能改变程序原有的意图。 注意:将修改后的完整的源程序写在答题区内。 #include <stdio.h> #define SIZE sizeof(struct node) struct node { long num; float score; struct node *next; } int main() { int n = 0; struct node *head = NULL, *p1, *p2; p1 = p2 = (struct node*)malloc(SIZE); printf("Input %d node data:\n", n + 1); scanf("%d%f", &p1->num, &p1->score); while (p1->num != 0) { if (++n == 1) head = p1; else p2->next = p1; p1 = p2; p1 = (struct node*)malloc(SIZE); printf("Input %d node data:\n", n + 1); scanf("%d%f", &p1->num, &p1->score); } free(p1); printf("Prit list:\n"); p2->next = NULL; if (head != NULL) { p2=head; while (p1 != NULL) { printf("num:%d\tscore:%.2f\n", p1->num, p1->score); p1 = p1->next; } } return 0; }
修改后的程序如下:
```
#include <stdio.h>
#include <stdlib.h>
#define SIZE sizeof(struct node)
struct node {
long num;
float score;
struct node *next;
};
int main() {
int n = 0;
struct node *head = NULL, *p1, *p2;
p1 = p2 = (struct node*)malloc(SIZE);
printf("Input %d node data:\n", n + 1);
scanf("%ld%f", &p1->num, &p1->score);
while (p1->num != 0) {
if (++n == 1) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (struct node*)malloc(SIZE);
printf("Input %d node data:\n", n + 1);
scanf("%ld%f", &p1->num, &p1->score);
}
free(p1);
printf("Print list:\n");
p2->next = NULL;
if (head != NULL) {
p1 = head;
while (p1 != NULL) {
printf("num:%ld\tscore:%.2f\n", p1->num, p1->score);
p1 = p1->next;
}
}
return 0;
}
```
主要的修改如下:
1. 在头文件中添加 stdlib.h,以便使用 malloc 和 free 函数。
2. 在结构体定义后添加分号。
3. 在输入学号和分数时,修改为用 %ld 格式符读入 long 类型的数据。
4. 在创建链表时,将 p2 赋值为 p1,遍历链表时用 p1 变量。
5. 在遍历链表前,将 p2->next 设为 NULL,以防止输出链表时出现不可预知的错误。