while ( pr->next!= NULL) { pr = pr->next; } pr->next = p;和下面的是否等价 while ( pr != NULL) { pr = pr->next; } pr = p;
时间: 2023-06-27 12:05:25 浏览: 55
这两段代码并不等价。
第一段代码是将指针 `pr` 移动到链表的尾部,然后将新节点 `p` 添加到链表的尾部。
第二段代码将指针 `pr` 移动到链表的尾部,然后将指针 `pr` 指向新节点 `p`。这样做的结果是新节点 `p` 成为了链表的最后一个节点,但是原来的最后一个节点并没有被删除,仍然存在于链表中,只是无法访问到了。
因此,第一段代码是正确的链表插入操作,而第二段代码会导致链表出现问题。
相关问题
void *Sort(STU *head) { p = head->next; now = head; pr = head; while (p != NULL) { while (p->next != NULL) { if ((p->score[0] + p->score[1] + p->score[2]) < (p->next->score[0] + p->next->score[1] + p->next->score[2])) { strcpy(pr->num, p->num); strcpy(pr->name, p->name); strcpy(pr->major, p->major); pr->classNo = p->classNo; pr->score[0] = p->score[0]; pr->score[1] = p->score[1]; pr->score[2] = p->score[2]; strcpy(p->num, p->next->num); strcpy(p->name, p->next->name); strcpy(p->major, p->next->major); p->classNo = p->next->classNo; p->score[0] = p->next->score[0]; p->score[1] = p->next->score[1]; p->score[2] = p->next->score[2]; strcpy(p->next->num, pr->num); strcpy(p->next->name, pr->name); strcpy(p->next->major, pr->major); p->next->classNo = pr->classNo; p->next->score[0] = pr->score[0]; p->next->score[1] = pr->score[1]; p->next->score[2] = pr->score[2]; } p = p->next; } p = now->next; now = now->next; } }
你提供的`Sort`函数实现了一个简单的冒泡排序算法来按照学生总分从高到低对链表进行排序。代码中使用了结构体成员变量之间的赋值和字符串拷贝操作。
但是你的代码存在一个问题,就是在内层循环中,你没有更新`pr`指针。这会导致每次交换后,`pr`指针都保持不变,而应该向前移动一个位置。
以下是修复这个问题的修改后的代码:
```c
void Sort(STU *head) {
STU *p, *now, *pr;
p = head->next;
now = head;
pr = head;
while (p != NULL) {
while (p->next != NULL) {
if ((p->score[0] + p->score[1] + p->score[2]) < (p->next->score[0] + p->next->score[1] + p->next->score[2])) {
strcpy(pr->num, p->num);
strcpy(pr->name, p->name);
strcpy(pr->major, p->major);
pr->classNo = p->classNo;
pr->score[0] = p->score[0];
pr->score[1] = p->score[1];
pr->score[2] = p->score[2];
strcpy(p->num, p->next->num);
strcpy(p->name, p->next->name);
strcpy(p->major, p->next->major);
p->classNo = p->next->classNo;
p->score[0] = p->next->score[0];
p->score[1] = p->next->score[1];
p->score[2] = p->next->score[2];
strcpy(p->next->num, pr->num);
strcpy(p->next->name, pr->name);
strcpy(p->next->major, pr->major);
p->next->classNo = pr->classNo;
p->next->score[0] = pr->score[0];
p->next->score[1] = pr->score[1];
p->next->score[2] = pr->score[2];
}
p = p->next;
pr = pr->next; // 添加这一行来更新pr指针
}
p = now->next;
now = now->next;
}
}
```
这样,`pr`指针在内层循环中会向前移动一个位置,确保下一次交换时指向正确的位置。
请记得在调用`Sort`函数时传入合适的参数。
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char xm[20]; char xb; int cj; struct node *next; }; struct node * create(int n) { struct node *head,*p; head=(struct node*)malloc(sizeof (struct node)); head->next=NULL;int i; for(i=0;i<n;i++) { p=(struct node*) malloc (sizeof (struct node)); scanf("%s,%c,%d",p->xm,&p->xb,&p->cj); p->next=head->next;head->next=p; } p->next=NULL; return (head); } int main() { int n;scanf("%d\n",&n); struct node *pr; pr=create(n); print(pr); while(pr->next!=NULL) { if(pr->xm[0]=='z'&&pr->xm[1]=='h'&&pr->xm[2]=='a'&&pr->xm[3]=='n'&&pr->xm[4]=='g'&&pr->cj>=90) { printf("%s,%c,%d",pr->xm,pr->xb,pr->cj); } pr=pr->next; } return 0; } void print(struct node *head) { struct node *tem; tem=head; while(tem!=NULL) { printf("%s,%c,%d",tem->xm,tem->xb,tem->cj); tem=tem->next; } }
这段代码存在一些问题,例如:
1. `print`函数没有在程序之前声明,需要加上函数声明。
2. `print`函数中的`printf`语句没有换行符,需要加上换行符。
3. `main`函数中的`print`函数调用没有加上参数,需要传入链表头节点。
4. `print`函数和`main`函数中的`printf`语句格式不一致,需要保持一致。
5. `print`函数和`main`函数中的`scanf`函数格式不一致,需要保持一致。
6. `create`函数中的`p->next`应该指向`head->next`,而不是`head`本身。
以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char xm[20];
char xb;
int cj;
struct node* next;
};
void print(struct node* head);
struct node* create(int n) {
struct node* head, * p;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
int i;
for (i = 0; i < n; i++) {
p = (struct node*)malloc(sizeof(struct node));
scanf("%s %c %d", p->xm, &p->xb, &p->cj);
p->next = head->next;
head->next = p;
}
return head;
}
int main() {
int n;
scanf("%d", &n);
struct node* pr;
pr = create(n);
print(pr->next);
while (pr->next != NULL) {
if (strcmp(pr->xm, "zhang") == 0 && pr->cj >= 90) {
printf("%s %c %d\n", pr->xm, pr->xb, pr->cj);
}
pr = pr->next;
}
return 0;
}
void print(struct node* head) {
struct node* tem;
tem = head;
while (tem != NULL) {
printf("%s %c %d\n", tem->xm, tem->xb, tem->cj);
tem = tem->next;
}
}
```
修改后的程序中,`print`函数在程序之前进行了声明,函数中的`printf`语句加上了换行符。`main`函数中调用`print`函数时传入了链表的第一个节点。
将`create`函数中的`p->next`指向了`head->next`,保证了新节点插入到链表头部。
在`main`函数中,将字符串`"zhang"`与姓名进行比较时,使用了`strcmp`函数来比较字符串。
最后,所有的`printf`语句都保持了一致的格式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)