[英]Is it wrong what I am doing in function “trier” which swaps the name of patient?
The exercise about creating a linked list for patient, then arrange it by their name. 关于为患者创建链接列表,然后按患者姓名排列的练习。 I'm trying to swap their name;
我正试图交换他们的名字; it seems that what I did doesn't work.
看来我没有做过。
I've tried to take the the previous pointer "prec" and compare the name of next pointer "ptr" then I tried to swap their name in function named "echangedeChaine" 我尝试使用上一个指针“ prec”并比较下一个指针“ ptr”的名称,然后尝试在名为“ echangedeChaine”的函数中交换它们的名称
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct patient{
int cin;
char nom[8];
char prenom[8];
int annee;
struct patient *suivant;
};
struct patient *tete=NULL;
void creationdePatient(){
struct patient* ptr;
char rep;
ptr = malloc(sizeof(struct patient));
tete =ptr;
printf("Saisir Numero de Cin de Nouveau Patient: ");
scanf("%d",&tete->cin);
printf("Saisir Nom de Patient: ");
scanf("%8s",&tete->nom);
printf("Saisir prenom de Patient: ");
scanf("%8s",&tete->prenom);
tete->suivant = NULL;
printf("\nVoulez vous Saisir un autre Patient ?: (O,N): \n");
scanf(" %c",&rep);
while(toupper(rep)=='O'){
ptr = malloc(sizeof(struct patient));
printf("Saisir Numero de Cin de Nouveau Patient: ");
scanf("%d",&ptr->cin);
printf("Saisir Nom de Patient: ");
scanf("%8s",&ptr->nom);
printf("Saisir prenom de Patient: ");
scanf("%8s",&ptr->prenom);
ptr->suivant = tete;
tete=ptr;
printf("\nVoulez vous Saisir un autre Patient ?: (O,N): \n");
scanf(" %c",&rep);
}
}
void echangedeChaine(char x[8] , char y[8]){
char temp[8];
strcpy(temp,y);
strcpy(y,x);
strcpy(x,temp);
}
void printtList(){
struct patient *temp = tete;
while(temp!=NULL){
printf("Cin: %d | Nom:%s | Prenom: %s\n", temp->cin, temp->nom, temp->prenom);
temp=temp->suivant;
}
}
void trier(){
struct patient *ptr = tete;
struct patient*prec;
int echange=0;
do{
while(ptr!=NULL){
prec=ptr;
ptr=ptr->suivant;
if(strcmp(prec->nom,ptr->nom)<0){
echangedeChaine(prec->nom,ptr->nom);
echange=1;
}
}
}while(echange==1);
}
int main()
{
creationdePatient();
printtList();
trier();
printtList();
}
It seems it doesn't work after I tried to execute it. 在我尝试执行它之后,它似乎不起作用。
The are several issues with your code, including (but not necessarily limited to): 您的代码有几个问题,包括(但不一定限于):
Your code in trier()
will dereference a NULL pointer at the last element - since its suivant
is NULL, and you're doing: 您在
trier()
代码将在最后一个元素处取消引用NULL指针-因为其suivant
为NULL,所以您正在执行以下操作:
ptr = ptr->suivant; if(strcmp(prec->nom,ptr->nom) < 0) { ... }
I think you're trying to sort in the wrong order: When strcmp(prec->nom,ptr->nom)
is negative, that means the first patient's name is lexicographically earlier than the following patient name - in which case they should not be exchanged. 我认为您正在尝试按错误的顺序排序:当
strcmp(prec->nom,ptr->nom)
为负数时,这意味着第一位患者的姓名在字典上比以下患者的姓名早-在这种情况下,他们不应被交换。
PS - for those not fluent in French, here's a little glossary for OP's program... PS-对于那些不懂法语的人,这是OP计划的一些词汇表...
tete = head tete =头
suivant = next suivant =下一个
nom = last/family name nom =姓氏/姓氏
prenom = first/given name Prenom =名字/名字
echange = change (or replace) 变更=变更(或替换)
chaine = list (or cain) chaine =列表(或该隐)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.