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.
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.
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 have to create a list where each node contains the information of a game. Data are acquired by keyboard. So I have to create an insert function(inserisciTestaLista) in the list,but when I run the program stops. I have no compilation errors.
This's output:
...\Debug\progetto1.exe (processo 7356) terminato. Codice restituito: -1073741819.
Premere un tasto qualsiasi per chiudere questa finestra...
this's the code
#include <stdio.h>
#include <stdlib.h>
typedef struct s_partita {
char team1[32], team2[32];
int set1, set2;
char data[11];
}t_partita;
typedef struct nodo {
t_partita info;
struct nodo *next;
}t_nodo, *lista;
void inserisciTestaLista(lista *L, t_partita partita) {
lista aux;
aux = (lista)malloc(sizeof(t_nodo));
if (aux == NULL)
exit(1);
aux->info = partita;
aux->next = *L;
*L = aux;
}
int main() {
int scelta = 0;
lista L = NULL;
t_partita partita;
do {
printf("*****************MENU**************\n");
printf("0. ESCI\n");
printf("1. inserisci partita\n");
printf("2. stampa lista\n");
scanf("%d", &scelta);
switch (scelta) {
case 1: {
printf("Inserisci team 1: ");
scanf("%s", partita.team1);
printf("Inserisci team 2: ");
scanf("%s", partita.team2);
printf("Inserisci punteggio (esempio 3-1): ");
scanf("%d%d", &partita.set1, &partita.set2);
printf("Inserisci data (esempio 2020-01-01): ");
scanf("%s", partita.data);
inserisciTestaLista(L, partita);
break;
}
case 2: {
break;
}
}
} while (scelta != 0);
}
You can pass the memory address from the list in line 49.
inserisciTestaLista(&L, partita);
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]
i have a problem with adding an string type inside of a node list.
I am using the command strncpy(), but i am not sure how this works with the pointer.
This is the code block i have now.
My specific problem is in the function "Insertar" because it expects a type char *variable but i an inserting just a char.
#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_
#include <stdio.h>
typedef struct
{
int dia;
int mes;
int ano;
}fecha;
struct NODEL
{
float valor;
char variable [10];
int resol;
fecha f;
struct NODEL* nextNode;
};
typedef struct NODEL LISTNODE;
typedef LISTNODE * LISTNODEPTR;
void insertar(LISTNODEPTR * lista, float newvalor, char *variable, int newresol,
#endif
void insertar(LISTNODEPTR * lista, float newvalor, char *variable, int newresol, int newdia, int newmes, int newano)
{
LISTNODEPTR newPtr, prevPtr, currPtr;
newPtr = malloc(sizeof(LISTNODE));
if(newPtr != NULL)
{
newPtr->valor = newvalor;
strncpy(newPtr->variable,newPtr->variable,10);
newPtr->resol = newresol;
newPtr->f.dia = newdia;
newPtr->f.mes = newmes;
newPtr->f.ano = newano;
newPtr->nextNode = NULL;
prevPtr = NULL;
currPtr = *lista;
while(currPtr != NULL && newvalor > currPtr->valor)
{
prevPtr = currPtr;
currPtr = currPtr->nextNode;
}
if(prevPtr == NULL)
{
newPtr->nextNode = *lista;
*lista = newPtr;
}
else
{
prevPtr->nextNode = newPtr;
newPtr->nextNode = currPtr;
}
}
else
{
printf("Elemento no insertado, no hay memoria disponible\n");
}
}
int main(void)
{
LISTNODEPTR data_base;
float valorADC;
char varIngresada;
int resolucion;
int diaIng;
int mesIng;
int anoIng;
printf("Valor de ADC: ");
scanf("%f", &valorADC);
fflush(stdin);
printf("Tipo variable 'Presion'/'Temperatura'/'Aceleracion': ");
scanf("%s", &varIngresada);
fflush(stdin);
printf("Resolución: ");
scanf("%d", &resolucion);
fflush(stdin);
printf("Fecha (dia): ");
scanf("%d", &diaIng);
fflush(stdin);
printf("Fecha (mes): ");
scanf("%d", &mesIng);
fflush(stdin);
printf("Fecha (año): ");
scanf("%d", &anoIng);
fflush(stdin);
insertar(&data_base, valorADC, varIngresada, resolucion, diaIng, mesIng, anoIng);
imprimir(data_base);
}