日期结构和链接列表打印问题

发布于 2025-01-29 15:18:18 字数 3974 浏览 4 评论 0原文

我正在尝试以C语言创建一个链接列表, 该代码没有错误编译,但是当我运行它时,它不会运行。

数据结构是一个任务,其中包含标签的数字,持续时间,链接任务的数量,该任务将放在数组中,并指向下一个任务的指针。

这是我的代码和测试代码,

#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;
}

我对您的建议进行了一些更改:

函数ajoutetask现在称为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;

}

测试文件的

现在调用的函数ajouteinter现在添加前述:

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];
        }
    }

更改:

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;
}

代码现在正常工作的

结果:

la tache a a a 0antécédents: La Tache B A 1Antécétents:1 La Tache C A 2Antécédents:1 2

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2025-02-05 15:18:18

首先,使用英语单词作为标识符。

您的代码没有意义。

例如,请考虑函数ajoutetask

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

如果指针t等于null(那时,函数vide code> vide vide返回1)然后1)函数调用由于此语句而引起的不确定行为

        t->suivant = new;

泄漏

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

,2)它会产生内存 重新分配

        new = firstTask(d);

或在函数中ajouteinter您有一个无限的循环,如果move> move-&gt; num等于numero 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
    //...

那么您需要的是重新工作您的代码,如果会有问题,请提出一个新问题。

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.

水水月牙 2025-02-05 15:18:18

单击此处

a href =“ https://uniblogsp.blogspot.com/2022/05/link-list-implementation-in-c--insertion.html” rel =“ nofollow noreferrer”> https://uniiblogsp.blogspot.blogspot.com/2022222 /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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文