我正在尝试按升序在我的列表中添加整数!!有什么帮助吗?
i am trying to add integers in my list in ascendant order!!any help pleasee?
我正在尝试制作一个列表,在插入阶段按升序排列整数,出于某种原因,在将列表按正确顺序排列后,它给了我一个奇怪的数字,有人能告诉我我哪里搞砸了吗?或者如果你有这个函数的任何优化版本ajout_liste_croissant()
?
#include"liste.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void ajout_liste_croissant(element**l,int info)
{assert(*l);
element*nouv;
nouv=(element*)malloc(sizeof(element));
if(vide(*l))/*if list is empty will add the new info without comparing */
{
*l=nouv;
(*l)->cle=info;
(*l)->suivant=NULL;}
else
{element*courant,*q;
int inter;
courant=*l;
int trouve=0;
while(courant!=NULL&&!trouve)/*parcouring the list till the end or when finding a value bigger than the entered one*/
{if(info<courant->cle)
trouve=1;
else{q=courant;/*this pointer get the adress of the previous current position in the list*/
courant=courant->suivant;}}/*the current pointer move to next adress*/
if(trouve)
{inter=courant->cle;
courant->cle=info;/*exchanging values in case current positon is bigger than the entered value*/
info=inter;
nouv->cle=info;
nouv->suivant=courant->suivant;
courant->suivant=nouv;
}
else
/*if the list reaches end*/
{
nouv->cle=info;
q->suivant=nouv;/*adding the new element at the end of the list*/
nouv->suivant=NULL;}
}
}
void afficher(element*l)
{assert(!vide(l));
while(l!=NULL)
{printf("%d\n",l->cle);
l=l->suivant;}
}
没有太多错误。但是您的代码 绝对不可读 。我不知道您如何阅读它并因此对其进行调试。
因此,我添加了空格、缩进、换行......并且它只需要稍作改动。我让你发现这些变化。这个答案不一定是最好的方法,但是,从您的代码开始,我尝试进行最少的更改。此外,当您在 Whosebug 上提问时,请避免在您的代码中使用法语(文本、函数名称...)。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct element {
int cle;
struct element *suivant;
} element;
void creer_liste(element **l) {
assert(l);
*l=NULL;
}
int vide(element *l) {
return(l==NULL);
}
void ajout_liste_croissant(element **l,int info) {
assert(l);
element *nouv;
nouv=(element*)malloc(sizeof(element));
if( vide(*l) ) {
*l=nouv;
(*l)->cle=info;
(*l)->suivant=NULL;
} else {
element *courant,*q;
int inter;
courant=*l;
int trouve=0;
while ( courant != NULL && !trouve ) {
if(info<courant->cle)
trouve=1;
else {
q=courant;
courant=courant->suivant;
}
}
if(trouve) {
inter=courant->cle;
courant->cle=info;
info=inter;
nouv->cle=info;
nouv->suivant=courant->suivant;
courant->suivant=nouv;
} else {
nouv->cle=info;
q->suivant=nouv;
nouv->suivant=NULL;
}
}
}
void afficher(element *l) {
assert(!vide(l));
while(l!=NULL) {
printf("%d\n",l->cle);
l=l->suivant;
}
}
int main() {
element *data;
int nb,info;
creer_liste(&data);
printf("donner la taille de la liste:\n");
scanf("%d",&nb);
for(int i=0;i<nb;i++) {
printf("donner un entier:\n");
scanf("%d",&info);
ajout_liste_croissant(&data,info);
}
printf("_____________________\n");
afficher(data);
}
我正在尝试制作一个列表,在插入阶段按升序排列整数,出于某种原因,在将列表按正确顺序排列后,它给了我一个奇怪的数字,有人能告诉我我哪里搞砸了吗?或者如果你有这个函数的任何优化版本ajout_liste_croissant()
?
#include"liste.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void ajout_liste_croissant(element**l,int info)
{assert(*l);
element*nouv;
nouv=(element*)malloc(sizeof(element));
if(vide(*l))/*if list is empty will add the new info without comparing */
{
*l=nouv;
(*l)->cle=info;
(*l)->suivant=NULL;}
else
{element*courant,*q;
int inter;
courant=*l;
int trouve=0;
while(courant!=NULL&&!trouve)/*parcouring the list till the end or when finding a value bigger than the entered one*/
{if(info<courant->cle)
trouve=1;
else{q=courant;/*this pointer get the adress of the previous current position in the list*/
courant=courant->suivant;}}/*the current pointer move to next adress*/
if(trouve)
{inter=courant->cle;
courant->cle=info;/*exchanging values in case current positon is bigger than the entered value*/
info=inter;
nouv->cle=info;
nouv->suivant=courant->suivant;
courant->suivant=nouv;
}
else
/*if the list reaches end*/
{
nouv->cle=info;
q->suivant=nouv;/*adding the new element at the end of the list*/
nouv->suivant=NULL;}
}
}
void afficher(element*l)
{assert(!vide(l));
while(l!=NULL)
{printf("%d\n",l->cle);
l=l->suivant;}
}
没有太多错误。但是您的代码 绝对不可读 。我不知道您如何阅读它并因此对其进行调试。
因此,我添加了空格、缩进、换行......并且它只需要稍作改动。我让你发现这些变化。这个答案不一定是最好的方法,但是,从您的代码开始,我尝试进行最少的更改。此外,当您在 Whosebug 上提问时,请避免在您的代码中使用法语(文本、函数名称...)。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct element {
int cle;
struct element *suivant;
} element;
void creer_liste(element **l) {
assert(l);
*l=NULL;
}
int vide(element *l) {
return(l==NULL);
}
void ajout_liste_croissant(element **l,int info) {
assert(l);
element *nouv;
nouv=(element*)malloc(sizeof(element));
if( vide(*l) ) {
*l=nouv;
(*l)->cle=info;
(*l)->suivant=NULL;
} else {
element *courant,*q;
int inter;
courant=*l;
int trouve=0;
while ( courant != NULL && !trouve ) {
if(info<courant->cle)
trouve=1;
else {
q=courant;
courant=courant->suivant;
}
}
if(trouve) {
inter=courant->cle;
courant->cle=info;
info=inter;
nouv->cle=info;
nouv->suivant=courant->suivant;
courant->suivant=nouv;
} else {
nouv->cle=info;
q->suivant=nouv;
nouv->suivant=NULL;
}
}
}
void afficher(element *l) {
assert(!vide(l));
while(l!=NULL) {
printf("%d\n",l->cle);
l=l->suivant;
}
}
int main() {
element *data;
int nb,info;
creer_liste(&data);
printf("donner la taille de la liste:\n");
scanf("%d",&nb);
for(int i=0;i<nb;i++) {
printf("donner un entier:\n");
scanf("%d",&info);
ajout_liste_croissant(&data,info);
}
printf("_____________________\n");
afficher(data);
}