So I am trying to program a mechanical program.
The program is long but here are the functions that are causing me problems: recherche_noe and creation_noe. No need to bother with the rest.
It's in french so bear with me but the idea is this: first in the main I ask the user the number of noe in lst_noe (which is a list of noe). With creation_noe he makes that while asking the user for info for the structure. Finally recherche_noe returns the noe that I am looking for. All the info is stored in struct maillage which you have other structures inside. Thank you for your help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*==============================================*/
/* Déclaration des structures */
/*==============================================*/
struct matrice
{
char nom[20];
int n,m;
double **tab;
struct matrice *next;
};
struct element
{
int num;
int n1, n2;
double k;
struct element *next;
};
struct noeud
{
int num;
double u;
double f;
struct noeud *next;
};
struct maillage
{
int nb_noe, nb_elt;
struct noeud *lst_noe;
struct element *lst_elt;
struct matrice *K, *U, *F;
};
typedef struct matrice* matrices;
typedef struct element* elements;
typedef struct noeud* noeuds;
typedef struct maillage* maillages;
char buffer[100];
/*==============================================*/
/* Recherche */
/*==============================================*/
noeuds recherche_noe(maillages mail,int num){
int i;
maillages temp=mail;
while(temp->lst_noe!=NULL){
if(temp->lst_noe->num == num)
return temp;
temp->lst_noe=temp->lst_noe->next;
}
printf("Le noeud recherche n'existe pas");
return temp;
}
elements recherche_elt(maillages mail,int num){
int i;
maillages temp=mail;
while(temp->lst_elt->num != num /*&& temp->lst_elt->next!=NULL*/){
temp->lst_elt=temp->lst_elt->next;
}
if(temp->lst_elt->num != num /*&& temp->lst_elt->next==NULL*/){
printf("L'element recherche n'existe pas");
}else{
return mail->lst_elt;
}
}
/*==============================================*/
/* creation */
/*==============================================*/
matrices creation_noeud(maillages mail){
int i;
for (i=0;i<mail->nb_noe;i++){
noeuds new = (noeuds)malloc(sizeof(struct noeud));
new->num = i+1;
printf("Deplacement du noeud %d: ",i+1);
buffer[0]='\0';
getchar(); //reinitialisation de buffer
scanf("%[^\n]",buffer);
if((int) strlen(buffer)){ //si la taille du buffer différente 0
new->u= (double)atof(buffer);
}
else{
printf("Donner l'effort %d du noeuds",i+1);
scanf("%lf", &new->f);
}
new->next=mail->lst_noe;
mail->lst_noe=new;
}
}
void creation_element(maillages mail)
{
int i;
for (i=0;i<mail->nb_elt;i++){
elements new= (elements)malloc(sizeof(struct element));
new->num=i+1;
printf("Donner le noeud 1 de l'element %d: ",i+1);
scanf("%d", &new->n1);
printf("Donner le noeud 2 de l'element %d: ",i+1);
scanf("%d", &new->n2);
printf("Donner la raideur de l'element %d: ",i+1);
scanf("%lf",&new->k);
new->next= mail->lst_elt;
mail->lst_elt=new;
}
}
matrices creation_mat(int n,int m, char *nom){
int i,j;
matrices new=(matrices)malloc(sizeof(struct matrice));
strcpy(new->nom,nom);
new->n = n;
new->m = m;
new->tab = (double**)malloc((n)*sizeof(double*));
for (i=0; i<n; i++)
new->tab[i] = (double*)malloc((n)*sizeof(double));
for (i=0;i<n;i++) /* mise a zero des composantes */
for (j=0;j<m;j++)
new->tab[i][j] =0;
return new;
}
/*==============================================*/
/* Assemblage */
/*==============================================*/
void assemblage(maillages mail){
int a,b,i,j,k;
mail->K = creation_mat(mail->nb_noe, mail->nb_noe,"K");
mail->U = creation_mat(mail->nb_noe, 1,"U");
for (j=0; j<mail->nb_noe; j++){ //Initialisation de K
for(k=0; k<mail->nb_noe; k++){
mail->K->tab[j][k]=0;
}
}
printf("%d",recherche_elt(mail,i+1)->n1);
for (i=0; i<mail->nb_elt; i++){ // Assemblage matrice K
a = recherche_elt(mail,i+1)->n1-1;
b = recherche_elt(mail,i+1)->n2-1;
mail->K->tab[a][a] +=recherche_elt(mail,i+1)->k;
mail->K->tab[a][b] -=recherche_elt(mail,i+1)->k;
mail->K->tab[b][a] -=recherche_elt(mail,i+1)->k;
mail->K->tab[b][b] +=recherche_elt(mail,i+1)->k;
}
for (i=0; i<mail->nb_noe; i++){ // Assemblage matrice U
mail->U->tab[i][0] = recherche_noe(mail,i+1)->u;
}
}
/*==============================================*/
/* Produit */
/*==============================================*/
matrices produit(matrices mat1,matrices mat2,char *nom){
int i,j,k;
matrices prod;
if(mat1->m!=mat2->n){
printf("Erreur, les matrices ne sont pas compatibles\n\n");
}else{
prod=malloc(sizeof(struct matrice));
strcpy(prod->nom,nom);
prod->next=NULL;
prod->n=mat1->n;
prod->m=mat2->m;
prod->tab= (double **)malloc(prod->n * sizeof(double *));
for (i=0; i<prod->n; i++)
prod->tab[i] = (double *)malloc(prod->m * sizeof(double));
for (i=0;i<prod->n;i++){
for (j=0;j<prod->m;j++){
prod->tab[i][j]=0;
for (k=0;k<mat1->m;k++){
prod->tab[i][j]+=mat1->tab[i][k] * mat2->tab[k][j];
}
}
}
return prod;
}
}
/*==============================================*/
/* Affichage */
/*==============================================*/
void affiche_mat(matrices mats){
int i,j;
printf("Matrice %s de dimensions %d*%d:\n",mats->nom,mats->n,mats->m);
for (i=0;i<mats->n;i++){
for (j=0;j<mats->m;j++){
printf("%s[%d][%d]: %lf\n",mats->nom,i,j,mats->tab[i][j]);
}
}
printf("\n");
}
int main(){
int i;
elements lst_elt;
noeuds lst_noe;
maillages mail=malloc(sizeof(struct maillage));
mail->lst_noe=NULL;
mail->lst_elt=NULL;
printf("Donner le nombre de noeuds voulu: ");
scanf("%d",&mail->nb_noe);
printf("Donner le nombre d'elements voulu: ");
scanf("%d",&mail->nb_elt);
creation_noeud(mail);
creation_element(mail);
printf("%d",recherche_elt(mail,2+1)->n1+45);
assemblage (mail);
produit(mail->K,mail->U,"F");
/*affiche_mat(mail->K);
printf("\n");
affiche_mat(mail->U);
printf("\n");
affiche_mat(mail->F);
printf("\n");*/
}
When walking the linked list, you are altering the contents of the linked list, instead of advancing the temp.
elements recherche_elt(maillages mail,int num){
elements temp;
for (temp = mail->lst_elt; temp; temp = temp->next ) {
if (temp->num == num) return temp;
}
printf("L'element recherche n'existe pas\n");
return NULL;
}
And, after removal of the typedefs, it becomes:
struct element *recherche_elt(struct maillage *mail,int num){
struct element *this;
for (this = mail->lst_elt; this; this = this->next ) {
if (this->num == num) return this;
}
printf("L'element recherche n'existe pas\n");
return NULL;
}
[The same kind of error is reproduced at other places in the code]
Related
i am trying to make a program that create a list of custommers and then editing the list like i want by deleting , adding a customer before another etc , for some reason i didnt find out yet why my program crash after displaying custommers exact after using function afficher();
test:main
#include <stdio.h>
#include <stdlib.h>
#include "liste.h"
#include<string.h>
int main()
{
liste data;
printf("creation liste . . .\n"); /*list creation*/
creer_liste(&data);
if (data.premier == NULL)
{
printf("done!\n");
}
int nb, i;
printf("combien de personnes souhaitez ajouter a la liste?\n");
scanf("%d", &nb); /*reading number of custommers*/
personne p1;
for (i=0; i < nb; i++)
{
printf("donner le nom de la personne %d:\n", i+1);
scanf("%20s", p1.nom);
printf("donner l'annee de naissance de la personne %d:\n", i+1);
scanf("%d", &p1.annee_naissance);
printf("donner le numero de telephone de la personne %d:\n", i+1);
scanf("%20s", p1.tel);
ajouter_en_tete(p1, &data); /*add custommer info in the begenning of list*/
}
afficher(data); /*displaying list*/
int pos;
printf("veuillez ajouter un element avant un autre?\n");
printf("donner la position de cette element:\n");
scanf("%d", &pos); /*reading position of the custommer that you want to insert before it*/
cellule *add_elt;
add_elt = data.premier;
for (i = 1; i < pos; i++)
{
add_elt = add_elt->suivant;
}
printf("donner le nom de la nouvelle personne:\n");
scanf("%s", p1.nom);
printf("donner l'annee de naissance de la nouvelle personne %d:\n", i+1);
scanf("%d", &p1.annee_naissance);
printf("donner le numero de telephone de la nouvelle personne %d:\n", i+1);
scanf("%s", p1.tel);
ajouter_avant(add_elt, p1, &data); /*adding the new customer info*/
afficher(data);
}/*displaying*/
Prototypes:
#ifndef LISTE_H_INCLUDED
#define LISTE_H_INCLUDED
typedef struct
{
char nom[20];
int annee_naissance;
char tel[20];
} personne; /*every customer has a name , date of birth and phone */
typedef struct noeud
{
personne p;
struct noeud *suivant;
} cellule; /*node contains the custommer info and the next address*/
typedef struct
{
cellule *premier;
} liste; /*list that has one entry*/
void creer_liste(liste *); /*creation*/
void ajouter_en_tete(personne, liste *); /*adding at begenening of list*/
void ajouter_en_queue(personne, liste *); /*adding to the end of list*/
void supprimer_element(cellule *, liste *); /*deleting element*/
void afficher(liste); /*displaying list*/
void ajouter_apres(cellule *, personne, liste *); /*adding after a custommer*/
void ajouter_avant(cellule *, personne, liste *); /*adding before a custommer*/
#endif // LISTE_H_INCLUDED
Implementation:
#include "liste.h"
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
void creer_liste(liste *ll)
{
assert(ll);
ll->premier=NULL;
}
void ajouter_en_tete(personne pp, liste *ll)
{
assert(ll);
cellule *nouveau;
nouveau = (cellule *) malloc(sizeof(cellule)); /*allocating the new node to add*/
nouveau->p = pp; /*getting the new info*/
if (ll->premier == NULL)
ll->premier = nouveau; /*case list is empty only list start's pointer updated*/
else
{
nouveau->suivant = ll->premier; /*the next pointer of the new custommer take the old list start's pointer */
ll->premier = nouveau; /*list's start pointer get updated by th new node*/
}
}
void ajouter_en_queue(personne pp, liste *ll)
{
assert(ll);
cellule *nouveau;
nouveau = (cellule *) malloc(sizeof(cellule));
nouveau->p = pp;
if (ll->premier == NULL)
ll->premier = nouveau;
else
{
while (ll->premier->suivant != NULL)
ll->premier = ll->premier->suivant;
ll->premier = nouveau;
}
}
void supprimer_element(cellule *sup_elt, liste *ll)
{
assert(sup_elt && ll);
if (ll->premier)
{
cellule *temp;
if (sup_elt)
{
if (sup_elt->suivant == NULL)
{
free(sup_elt);
}
else
{
temp = sup_elt->suivant;
*sup_elt = *temp;
free(temp);
}
}
}
}
void ajouter_avant(cellule *q, personne pp, liste *ll)
{
assert(ll);
cellule *temp;
temp = (cellule *) malloc(sizeof(cellule));
temp = q->suivant;
*temp = *q;
q->p = pp;
}
void afficher(liste ll)
{
assert(ll.premier);
if (ll.premier != NULL)
{
cellule *actuel;
actuel = ll.premier;
while (actuel != NULL)
{
printf("%s\t%d\t%s\n", actuel->p.nom, actuel->p.annee_naissance, actuel->p.tel);
actuel = actuel->suivant;
}/*parcouring the list and displaying every custommer till reaching adress NULL*/
}
}
In ajouter_en_tete*(), when you allocate nouveau, malloc() does not initialise any field, so suivante might not be NULL unless you explicitly set it.
More specifically, you check if (ll->premier == NULL), and:
set ll->premier = nouveau in both cases (move that out of the if block).
Only update nouveau->suivante in the second case, so it's never set to NULL. Might as well move that out of the if block too, so it will always be set (and in the case of NULL, it's set to NULL as it should be.
I'll show what I mean.
1 nouveau = (cellule *) malloc(sizeof(cellule));
2 nouveau->p = pp;
3 nouveau->suivant = ll->premier;
4 ll->premier = nouveau;
After line 1 and 2, nouveau->suivant is probably not NULL. If the list is empty, ll->premier is NULL, so in line 3 nouveau->suivant also gets set to NULL. In line 4 ll->premier is set to nouveau.
Next time through, same as before nouveau->suivant is not NULL at line 2, at line 3 nouveau->suivant is set to ll->premier which was the node allocated last time, and ll->premier is set to nouveau. The node from last time is unchanged, at this point ll->premier->suivant is the previous node, ll->premier->suivant->suivant is still NULL.
In all cases, it works this way.
Im a begginer developer and i need help!
I have a txt file of 2287 lines
Contains ID (int), Name (string), Number1, Number2, Number3 (all 3 floats).
"
1234 Jose 10 11 12
...
...
"
I need to get the data from this file and implement a Hash table in separate chain WITH static list.
Also needs to have Insert, Remove and Search functions.
I have so many problems until now.
I dont know how to put it with a static list, and before that i cant hash it properly, its all NULLs..
Anyone can help?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max 2287
int main(){
FILE* fp;
fp = fopen("dados.txt","r");
if (fp==NULL){
printf("Erro na abertura\n");
system("pause");
exit(1);
}
struct aluno{
int matricula;
char nome[30];
float n1,n2,n3;
struct aluno *next;
};
struct aluno aluno[max];
struct aluno *hash[max];
//povoando com os dados
int i=0;
for(i=0;i<max;i++){
fscanf(fp,"%d %s %f %f %f\n", &aluno[i].matricula, &aluno[i].nome, &aluno[i].n1, &aluno[i].n2, &aluno[i].n3);
}
//teste pra ver se o fscanf funcionava
printf("%s %d %f" , aluno[3].nome, aluno[2].matricula, aluno[1].n1);
void inicializa()
{
int i;
for(i = 0; i < max; i++)
hash[i] = NULL;
}
void insere(int valor){
//cria a newAluno com o valor
struct aluno *newAluno = malloc(sizeof(struct aluno));
newAluno->matricula = valor;
newAluno->next = NULL;
//calcula a chave hash
int chave = valor % max;
//checa se esta null
if(hash[chave] == NULL)
hash[chave] = newAluno;
else{
struct aluno *temp = hash[chave];
while(temp->next){
temp = temp->next;
}
temp->next = newAluno;
}
}
int busca(int valor){
int chave = valor % max;
struct aluno *temp = hash[chave];
while(temp){
if(temp->matricula == valor)
return 1;
temp = temp->next;
}
return 0;
}
void print(){
for(i = 0; i < max; i++)
{
struct aluno *temp = hash[i];
printf("hash[%d]-->",i);
while(temp)
{
printf("%d -->",temp->matricula);
temp = temp->next;
}
printf("NULL\n");
}
}
print();
return 0;
}
I'm learning C. I tried to solve the following problem, but I had several problems.
I do not control the chain linked list with pointers.
Here's the problem:
We want to write functions for managing the employees of a company. An employee is defined by
his name (character string), his personnel number (integer), the number of hours worked (real), and the hourly rate (actual).
Define the Employee data type
Functions:
saisirEmploye; which allows you to enter an employee from the keyboard
affichEmploye; which displays an employee on the screen
calculSalaire; which calculates and returns the employee's salary (salary = nbh * rate_h)
We now want to store a set of employees in a linked list, define for this the node and ListEmp data types.
ajouterEmploye; which allows you to add a new employee to the list (the addition can be
do at the beginning or at the end of the list, your choice)
saisirListEmploye; which allows you to enter from the keyboard a list of n employees.
affichListEmploye; which displays a list of employees on the screen.
totalSalaire; which calculates and returns the total salary of all the employees on the list
Write a main function performing the following operations:
declare a list of employees
enter the number of employees using the keyboard
enter list
calculate the total salary of all employees
display the contents of the list, as well as the total salary
My code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char nom[20];
int mat;
float ht;
float tx ;
}Employe;
typedef struct
{
Employe *employe;
struct ListEmp *suivant;
}ListEmp;
typedef struct
{
ListEmp *premier;
}Elements;
void saisirEmploye(Employe *e)
{
printf("Sasir le nom \n");
scanf("%s",e->nom);
printf("Saisir le matricule \n");
scanf("%d",&e->mat);
printf("Saisir le nombre d’heures travaillees \n");
scanf("%f",&e->ht);
printf("Saisir le taux horaire \n");
scanf("%f",&e->tx);
}
void afficheEmp(Employe e)
{
printf("Nom : %s | Matricule : %d | Nombre d’heures travaillees : %f | Taux horaire : %f \n",e.nom,e.mat,e.ht,e.tx);
}
float calculSalaire(Employe e)
{
float salaire = 0;
salaire = (e.ht)*(e.tx) ;
return salaire;
}
Elements *init()
{
ListEmp *liste = malloc(sizeof(ListEmp));
Elements *elements = malloc(sizeof(Elements));
if (liste == NULL || elements == NULL)
{
exit(EXIT_FAILURE);
}
liste->employe = NULL;
liste->suivant = NULL;
elements->premier = liste;
return elements;
}
void ajouterEmp(Elements *elements, Employe *employe)
{
/* Création du nouvel élément */
ListEmp *nouveau = malloc(sizeof(*nouveau));
if (elements == NULL || nouveau == NULL)
{
exit(EXIT_FAILURE);
}
nouveau->employe = employe;
/* Insertion de l'élément au début de la liste */
nouveau->suivant = elements->premier;
elements->premier = nouveau;
}
int main()
{
// Employes e;
// e.tete=NULL;
// int n=0;
// float t=0;
// printf("Donner le nombre des Emplyes \n"); // saisie du nbr des employes
// scanf("%d ",&n);
// saisirListEmp(&e,n); // saisie de la liste
// t=totalSalaire(e); //calcule du salaire totale
// afficheList(e); // affichage du contenu
// printf("le salaire totale=%f ",t); // affichage du salaire
Employe e;
printf("Saisr un emplye \n");
saisirEmploye(&e);
//printf("Nom emplye : %s \n", e.nom);
afficheEmp(e);
printf("Salaire : %f",calculSalaire(e));
Elements *maListe = init();
ajouterEmp(maListe,e)
return 0;
}
Edited code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char nom[20];
int mat;
float ht;
float tx;
} Employe;
typedef struct Elem
{
Employe employe;
struct Elem *prev;
struct Elem *next;
}Elem;
typedef struct
{
Elem *first;
Elem *last;
}ListEmp;
void saisirEmploye(Employe *e)
{
printf("Sasir le nom \n");
scanf("%s",e->nom);
printf("Saisir le matricule \n");
scanf("%d",&e->mat);
printf("Saisir le nombre d’heures travaillees \n");
scanf("%f",&e->ht);
printf("Saisir le taux horaire \n");
scanf("%f",&e->tx);
}
void afficheEmp(Employe e)
{
printf("Nom : %s | Matricule : %d | Nombre d’heures travaillees : %f | Taux horaire : %f \n",e.nom,e.mat,e.ht,e.tx);
printf("\n");
}
float calculSalaire(Employe e)
{
float salaire = 0;
salaire = (e.ht)*(e.tx) ;
return salaire;
}
ListEmp *init()
{
ListEmp *listEmp = malloc(sizeof(ListEmp));
Elem *elem = malloc(sizeof(Elem));
Employe employe;
if (listEmp == NULL || elem == NULL)
{
exit(EXIT_FAILURE);
}
strcpy(employe.nom, "Hamza");
employe.mat = 123;
elem->employe = employe;
elem->next = NULL;
listEmp->first = elem;
return listEmp;
}
void auDebut(ListEmp *listEmp, Employe employe)
{
printf("1 Au debut \n");
Elem *elem = (Elem*)malloc(sizeof(Elem));
elem->employe = employe;
elem->next = listEmp->last;
elem->prev = NULL;
if(listEmp->last)
listEmp->last->prev = elem;
else
listEmp->last = elem;
listEmp->first = elem;
}
void aLaFin(ListEmp *listEmp, Employe employe)
{
Elem *elem = (Elem*)malloc(sizeof(Elem));
elem->employe = employe;
elem->prev = listEmp->first;
elem->next = NULL;
if(listEmp->first)
listEmp->first->next = elem;
else
listEmp->first = elem;
listEmp->last = elem;
// Elem *elem = (Elem*)malloc(sizeof(Elem));
// if(!elem) exit(EXIT_FAILURE);
// elem->employe = employe;
// elem->next = listEmp->first;
// listEmp->first = elem;
}
void ajouterEmploye(ListEmp *listEmp, Employe employe)
{
char element;
printf("Voulez-vous ajouter le nouvel employe au début de la liste d ou a la fin de la liste f ? \n");
scanf(" %c", &element);
if (element == 'd')
//printf("Au debut \n");
auDebut(listEmp,employe);
else if (element == 'f')
//printf("A la fin \n");
aLaFin(listEmp,employe);
}
void saisirListEmploye(ListEmp *listEmp, int n)
{
Employe employe;
for(int i=1;i<=n;i++)
{
printf("Employe %d\n",i);
saisirEmploye(&employe);
ajouterEmploye(listEmp,employe);
}
}
void affichListEmploye(ListEmp *listEmp)
{
Elem *i = listEmp->first;
while(i != NULL)
{
afficheEmp(i->employe);
i = i->next;
}
}
float totalSalaire(ListEmp *listEmp)
{
float total = 0;
Elem *i = listEmp->first;
while (i != NULL)
{
total += calculSalaire(i->employe);
i = i->next;
}
return total;
}
int main()
{
Employe e;
int n;
//printf("Saisr un emplye \n");
//saisirEmploye(&e);
//printf("Nom emplye : %s \n", e.nom);
//afficheEmp(e);
//printf("Salaire : %f",calculSalaire(e));
//printf("\n\n");
ListEmp *listEmp = init();
//ajouterEmploye(listEmp,e);
printf("Combien d'employes souhaitez-vous ajouter ?\n");
scanf("%d",&n);
saisirListEmploye(listEmp,n);
affichListEmploye(listEmp);
printf("Le salaire total de tous les employés de la liste est : %f", totalSalaire(listEmp));
return 0;
}
The error:
main.c: In function ‘ajouterEmp’:
main.c:87:22: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
nouveau->suivant = elements->premier;
^
main.c: In function ‘main’:
main.c:114:21: error: incompatible type for argument 2 of ‘ajouterEmp’
ajouterEmp(maListe,e)
^
main.c:76:6: note: expected ‘Employe * {aka struct *}’ but argument is of type ‘Employe {aka struct }’
void ajouterEmp(Elements *elements, Employe *employe)
^~~~~~~~~~
main.c:116:2: error: expected ‘;’ before ‘return’
return 0;
^~~~~~
Can you help me please?
Thanks!
About your type errors: There is defined
typedef struct {
char nom[20];
int mat;
float ht;
float tx;
} Employe;
but later you used Employes instead of Employe, for example in
void ajouterEm (Employes *e, Elem nemp) { /* ... */ }
Same for Elements and Elem here. This is why you get unknown type errors. In
ajouterEmp(maListe, e)
there must also be an semicolon after the instruction. Attention should also be paid to where pointers (for example Employes*) and where entire structures (without *) are passed to the function.
Edit after this errors has been fixed:
Within
typedef struct{
Employe *employe;
struct ListEmp *suivant;
} ListEmp;
the type name ListEmp is already used in the definition in suivant, but is only defined in the line below. Use
typedef struct _ListEmp {
Employe *employe;
struct _ListEmp *suivant;
} ListEmp;
instead. The struct gets the name _ListEmp and then you define struct _ListEmp as ListEmp. For the third error at
ajouterEmp(maListe, e)
a pointer must be passed, but Employe e is a whole data structure. Write
ajouterEmp(maListe, &e);
instead and add the semicolon.
I'm trying to arrange the linked list data by name so I'm comparing each node name with the next one. and yes want to swap nodes data, to have a linked list arranged by name.
I've tried to swap each data of node with the other in case the test in function "trier" is true the test is (strcmp(prec, ptr) < 0). it seems working well until the last one.
#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);
printf("Saisir annee de naissance de Patient: ");
scanf("%d", &tete->annee);
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);
printf("Saisir annee de naissance de Patient: ");
scanf("%d", &ptr->annee);
ptr->suivant = tete;
tete = ptr;
printf("\nVoulez vous Saisir un autre Patient ?: (O,N): \n");
scanf(" %c", &rep);
}
}
void echangedeNom(struct patient *x, struct patient *y) {
char temp[8];
strcpy(temp, y->nom);
strcpy(y->nom, x->nom);
strcpy(x->nom, temp);
}
void echangedePrenom(struct patient *x, struct patient *y) {
char temp[8];
strcpy(temp, y->prenom);
strcpy(y->prenom, x->prenom);
strcpy(x->prenom, temp);
}
void echangedesentiers(struct patient *x, struct patient *y) {
int temp = 0;
temp = y->cin;
y->cin = x->cin;
x->cin = temp;
}
void echangedesannes(struct patient *x, struct patient *y) {
int temp = 0;
temp = y->annee;
y->annee = x->annee;
x->annee = temp;
}
void printtList() {
struct patient *temp = tete;
while (temp != NULL) {
printf("Cin: %d | Nom:%s | Prenom: %s |Anne de naissance: %d\n",
temp->cin, temp->nom, temp->prenom, temp->annee);
temp = temp->suivant;
}
}
void trier() {
struct patient *ptr = tete;
struct patient *prec = NULL;
int echange;
do {
echange = 0;
while (ptr != NULL && ptr->suivant != NULL) {
prec = ptr;
ptr = ptr->suivant;
if (strcmp(prec->nom, ptr->nom) < 0) {
echangedeNom(prec, ptr);
echangedePrenom(prec, ptr);
echangedesentiers(prec, ptr);
echangedesannes(prec, ptr);
echange = 1;
}
}
} while (echange == 1);
}
int main() {
creationdePatient();
printtList();
trier();
printf("=======================\n");
printtList();
}
some data while swapping get the wrong information like the cin number after swapping is not the same anymore.
You are swapping twice an integer here:
while(ptr!=NULL && ptr->suivant!=NULL){
// [...]
echangedesentiers(prec,ptr);
echangedesentiers(prec,ptr);
// [...]
}
By the way, here is a first step to learn to debug your code : How to debug small programs.
Hello guys i got a problem while running this code:
trying to run the printf in the comment i got a segfault, also without that i dont see my listed printed ad the function Stampa should do.
Probably i am missing something with pointers
#include <stdio.h>
#include <stdlib.h>
struct nodo { // double linked list
int info;
struct nodo *prec;
struct nodo *succ;
};
typedef struct nodo nodo;
struct nodo *Crealista(void);
void Stampa (struct nodo *nodo);
int main(int argc, const char * argv[]) {
struct nodo *p;
p = Crealista();
// printf("%d",p->succ->info);
Stampa(p);
return 0;
}
// this funct should print the whole list
void Stampa (struct nodo *p) {
while (p->succ != NULL ) {
printf("Value : %d \n", p->info);
p = p->succ;
}
}
// this funct should create list with n members and return a pointer to the first element
struct nodo *Crealista(void) {
struct nodo *p, *primo, *back;
int i, n;
p = NULL;
primo = NULL;
back = NULL;
printf("Numero di elementi: ");
scanf("%d", &n);
printf("inserisci %d numeri interi positivi: ", n);
for (i=0; i<n; i++) {
p = malloc(sizeof(struct nodo));
scanf("%d", &p->info);
p->prec = back;
p->succ = NULL;
back = p;
}
primo = p;
while (primo->prec != NULL) { primo = primo->prec;}
return(primo);
}
Stampa()
Let's look at what we want to do when we print the entire list:
If the current element is valid, then we want to print it.
Then, we want to iterate to the next element, and continue this loop.
That's not what your code does. Your code looks to see if the current element has a successor, and if it does, we print the current element's value.
That function should actually be:
void Stampa (struct nodo *p) {
while (p != NULL ) {
printf("Value: %d\n", p->info);
p = p->succ;
}
}
Crealista()
If you tell Crealista() to create a list of 0 elements, your final while loop will exhibit undefined behavior.
If you tell Crealista() to create a list of less than 2 elements, your commented printf() in main() will cause undefined behavior (if it was uncommented).
Doubly Linked Lists
You never update the value of nodo->succ. You only update the value of nodo->prev. Here's one example of how to do that:
if (p->prec)
p->prec->succ = p;
Putting all of this together
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct nodo {
int info;
struct nodo *prec;
struct nodo *succ;
};
typedef struct nodo nodo;
struct nodo *Crealista(void);
void Stampa (struct nodo *nodo);
int main(int argc, const char * argv[]) {
struct nodo *p;
p = Crealista();
Stampa(p);
}
// Print the list
void Stampa (struct nodo *p) {
while (p != NULL ) {
printf("Value: %d \n", p->info);
p = p->succ;
}
}
// Create a list of n elements
struct nodo *Crealista(void) {
int i, n;
struct nodo *p = NULL;
struct nodo *primo = NULL;
struct nodo *back = NULL;
printf("Numero di elementi: ");
scanf("%d", &n);
assert(n != 0);
printf("inserisci %d numeri interi positivi: ", n);
for (i=0; i<n; i++) {
p = malloc(sizeof(struct nodo));
scanf("%d", &p->info);
p->prec = back;
p->succ = NULL;
if (p->prec)
p->prec->succ = p;
back = p;
}
primo = p;
while (primo != NULL && primo->prec != NULL)
primo = primo->prec;
return primo;
}
Which when run...
Numero di elementi: 5
inserisci 5 numeri interi positivi: 1 2 3 4 5
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
The printf() call you have commented out will always exhibit undefined behavior when p->succ is NULL, as it will be when p points to the last (or only) element of the list. In that case, although the behavior is formally "undefined", it is quite likely to manifest as a segmentation fault.
In stampa add check
while(p!=NULL)
{
printf("Value %d",p->info);
p=p->succ;
}
You are probably accessing it when it is not allocated. Check it this way there is no problem if it reaches a null value also. And initial null value will not cause problem also(if used).
I have modified your code
// this funct should create list with n members and return a pointer to the first element
struct nodo *Crealista(void) {
struct nodo *p, *primo, *back;
int i, n;
p = NULL;
primo = NULL;
back = NULL;
printf("Numero di elementi: ");
scanf("%d", &n);
printf("inserisci %d numeri interi positivi: ", n);
p = (struct nodo *)malloc(sizeof(struct nodo));
scanf("%d", &p->info);
p->prec = back;
p->succ = NULL;
back = p;
for (i=1; i<n; i++) {
p = (struct nodo *)malloc(sizeof(struct nodo));
scanf("%d", &p->info);
p->prec = back;
p->prec->succ=p;
p->succ = NULL;
back = p;
}
primo = p;
while (primo->prec != NULL) { primo = primo->prec;}
return(primo);
}
You are not setting the succ pointer of previous node correctly. That is what causing you the problem. Check now.