簡體   English   中英

日期結構和鏈表打印問題

[英]date structure and linked list printing problem

我正在嘗試用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;
}

感謝您的建議,我做了一些更改:

現在調用 addtask: ,,, 的函數 ajoutetask

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 0 前面的 : la tache B a 1 前面的 : 1 la tache C a 2 前面的 : 1 2

對於初學者來說,使用英語單詞作為標識符。

你的代碼沒有意義。

例如考慮函數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返回1時),則 1) 由於此語句,函數調用未定義的行為

        t->suivant = new;

2)它會產生內存泄漏,因為首先分配了一個內存,並且它的地址被分配給了指針 new

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

然后重新分配指針

        new = firstTask(d);

或者在 ajouteInter 函數中,如果ajouteInter move->num等於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
    //...

因此,您需要重新編寫代碼,如果出現問題,請提出新問題。

點擊這里

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM