简体   繁体   中英

date structure and linked list printing problem

I am trying to creat a linked list in c language, the code compiles with no error but when I run it , it won't run.

the data structure is a task which contains the label it's number, duration , number of linked tasks which will be put in an array and a pointer to the next task.

here's my code and the test code

#include <stdio.h>
#include <stdlib.h>

typedef struct tache { //linked listes structure
    char label;
    int num; //task number
    int dure;
    int *inter;
    int n; //nombre d'antécedents
    struct tache * suivant;
}strTache,*pTask;


pTask creerVide(){ //créer une tache vide
    return NULL;
}

pTask firstTask(int d){
    //créer la première tache qui prend la durée en argument
    pTask first = (strTache*)(malloc(sizeof(strTache)));
    first->num = 1;
    first->suivant = NULL;
    first->label = 'A';
    first->dure = d;
    first->n = 0;
    return first;
}

Bool vide(pTask t){return t==NULL;} //check if a liste of tasks is empty this is predefined in a header included in the program

pTask ajouteTask(pTask t, int d){
    pTask new = (strTache*)(malloc(sizeof(strTache)));
    
    if(vide(t)){
        new = firstTask(d);
        t->suivant = new;
        
    }else
    {
        pTask parc = t;
        while(parc->suivant != NULL){parc=parc->suivant;}
            new->num = parc->num+1;
            new->label = parc->label+1;
            new->suivant = parc->suivant;
            parc->suivant = new;
            new->dure=d;
           
    }
    return new;

}

void ajouteInter(pTask p,int numero, int taille, int tab[]){
    int i,j;
    pTask move = p;

    while(move->suivant!=NULL){
        if(move->num != numero){
            move=move->suivant;
        }//fin if
        else 
        {
            move->inter = (int*) malloc(taille*sizeof(int));
            move->n = taille;
        }//fin else
    }//fin while
    for(i=0;i<taille;i++){
        move->inter[i] = tab[i];
    }//fin for
}//fin function


    void affichmat2(pTask p){
    int i = 0;
    pTask parc = p;
    while(parc!= NULL){
        
        printf("la tache %c a %d antécédents : ",parc->label,parc->n);
        for(int j=0;j<parc->n;j++){
            printf("%d ",parc->inter[j]);
            }
        printf("\n");
        parc=parc->suivant;
        i++;
    }
    free(parc);
}

int main()
{
    int b[1] = {1};
    int c[2] = {1,2};

    //pTask t1 = creerVide();

    pTask t1 = firstTask(2);
    pTask t2,t3;
    t2 = ajouteTask(t1,3);
    t3 = ajouteTask(t2,4);
    ajouteInter(t2,2,1,b);
    ajouteInter(t3,3,2,c);
    affichmat2(t1);



    return 0;
}

I have made some changes thanks to your advice:

the function ajoutetask which called now addtask: ,,,

pTask addTask(pTask t, int d){
    pTask new;
    if(vide(t)){
        new = firstTask(d);
        new->suivant = t;
        
    }else
    {
        new = (strTache*)(malloc(sizeof(strTache)));
        pTask parc = t;
        while(parc->suivant != NULL){parc=parc->suivant;}
            new->num = parc->num+1;
            new->label = parc->label+1;
            new->suivant = parc->suivant;
            parc->suivant = new;
            new->dure=d;
           
    }
    return new;

}

,,,

the function ajouteinter which called now add antecedent:

void addAntecedent(pTask p,int num, int size, int b[]){

        pTask search = p;
        while(search->num !=num){
            search = search->suivant;
        }
        search->inter = (int*)malloc(sizeof(int)*size);
        search->n = size;
        for(int i = 0;i<size;i++){
            search->inter[i] = b[i];
        }
    }

the changes to the test file :

int main (){
      int b[1] = {1};
    int c[2] = {1,2};

    pTask t1 = firstTask(2);
    pTask t2,t3;
    t2 = addTask(t1,3);
    t3 = addTask(t1,4);
    addAntecedent(t1,2,1,b);
    addAntecedent(t1,3,2,c);
    affichmat2(t1);
    return 0;
}

the code works properly now

the result :

la tache A a 0 antécédents : la tache B a 1 antécédents : 1 la tache C a 2 antécédents : 1 2

For starters use English words for identifiers.

Your code does not make a sense.

Consider for example the function ajouteTask

pTask ajouteTask(pTask t, int d){
    pTask new = (strTache*)(malloc(sizeof(strTache)));
    
    if(vide(t)){
        new = firstTask(d);
        t->suivant = new;
        //...

If the pointer t is equal to NULL (that is when the function vide returns 1 ) then 1) the function invokes undefined behavior due to this statement

        t->suivant = new;

and 2) it produces a memory leak because at first a memory was allocated and its address was assigned to the pointer new

    pTask new = (strTache*)(malloc(sizeof(strTache)));

and then the pointer was reassigned

        new = firstTask(d);

Or within the function ajouteInter you have an infinite while loop if move->num is equal to numero

void ajouteInter(pTask p,int numero, int taille, int tab[]){
    int i,j;
    pTask move = p;

    while(move->suivant!=NULL){
        if(move->num != numero){
            move=move->suivant;
        }//fin if
        else 
        {
            move->inter = (int*) malloc(taille*sizeof(int));
            move->n = taille;
        }//fin else
    }//fin while
    //...

So what you need is to rework your code and if there will be problems ask a new question.

Click here

https://uniblogsp.blogspot.com/2022/05/link-list-implementation-in-c-insertion.html

#include <stdio.h>
#include <stdlib.h>
typedef struct intnode
{
    int data;
    struct intnode *next;
} intnode;
intnode *create(int n)
{
    intnode *head, *q, *newnode;

    head = (intnode *)malloc(sizeof(intnode));
    scanf("%d", &head->data);
    head->next = NULL;
    q = head;
    for (int i = 0; i < n - 1; i++)
    {
        newnode = (intnode *)malloc(sizeof(intnode));
        scanf("%d", &newnode->data);
        newnode->next = NULL;
        q->next = newnode;
        q = newnode;
    }
    return head;
}
void printList(intnode *head)
{
    intnode *temp;
    temp = head;
    while (head != NULL)
    {
        printf("%d", head->data);
        head = head->next;
        printf("\n");
    }

    head = temp;
}
void insertList(intnode *head, int i, intnode *t)
{
    intnode *r, *x;
    if (i == 0)
    {
        t->next = head;
        head = t;
        return;
    }
    r = head;
    for (int j = 1; (j < i - 1) && (r != NULL); j++)
    {
        r = r->next;
    }
    if ((r == NULL) && (i > 0))
    {
        return;
    }

    x = r;
    t->next = x->next;
    x->next = t;
    return;
}
void deleteList(intnode *head, int i)
{
    intnode *y, *x;
    y = head;
    if (i == 0)
    {
        head = head->next;
        free(y);
        return;
    }

    x = y->next;
    for (int j = 1; (j < i) && (x != NULL); i++)
    {
        y = x;
        x = x->next;
    }
    if ((x == NULL) && (i > 0))
    {
        return;
    }
    y->next = x->next;
    x->next = NULL;
    free(x);
    return;
}
int main()
{
    int n, l;
    printf("Enter the no of nodes: \n");
    scanf("%d", &n);
    printf("Enter the elements of nodes: \n");
    intnode *p = create(n);
    printf("Nodes are: \n");
    printList(p);
    printf("If you want to insert node then press 1 and in case of delete node press 0 : \n");
    int ans;
    scanf("%d", &ans);
    if (ans == 1)
    {
        printf("In what positiomn you want to insert the node ??\n");
        scanf("%d", &l);
        intnode *a;
        a = (intnode *)malloc(sizeof(intnode));
        printf("Enter the node data :\n");
        scanf("%d", &a->data);
        a->next = NULL;
        insertList(p, l, a);
        printf("After insertion the list is : \n");
        printList(p);
    }
    else
    {
        printf("In what positiomn you want to delete the node ??\n");
        scanf("%d", &l);
        deleteList(p, l);
        printf("After deletion the list is : \n");
        printList(p);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM