删除链表节点的函数不会在另一个函数中返回修改后的哨兵

问题描述 投票:0回答:1

这个deleteNode函数之前可以工作,但是minScore函数被设置为返回最大分数而不是最小值。我已经调试了6个小时,似乎当它去计算新的minScore时,sant(即哨兵)具有空值,这意味着节点被删除但sant的链接没有更新?我已经尝试了deleteNode函数的很多变体,以前,在我使用它的循环之后,我只会存储前一个节点,这样我就会有一种更简单的删除函数,只有prev和current。

float minScore(nod *sant, int n)
{
    nod *p=sant->next;
    float minp=p->tp;
    for(int i=0;i<n;i++)
    {
        if(minp > p->tp)
        {
            minp=p->tp;
        }
        p=p->next;
    }
    return minp;

}

nod  *deleteNode(nod **sant, nod *del)
{
        if (*sant == NULL || del == NULL)
        return NULL;

        if ((*sant)->next == del)
        (*sant)->next = del->next;

        nod* temp = (*sant)->next;
        while (temp->next != NULL && temp->next != del) 
        {
            temp = temp->next;
        }
         if (temp->next == NULL) 
        {
            return NULL;
        }
        temp->next = del->next;
        for(int i=0; i<del->np;i++)
        {
            free(del->players[i].nume);
            free(del->players[i].prenume);
        }
        free(del->players);
        free(del->team);
        free(del);
        return temp;

}

void adjust(nod *sant, int *n)
{
    int nmax=1;
    while(nmax*2<=(*n))
        nmax*=2;
    for(nod *p=sant->next; p!=sant;p=p->next)
        {
            p->tp= teamScore(p,p->np);

        }
    nod *x=sant;
    float scrmin;
    while((*n)!=nmax)
    {
        scrmin=minScore(sant,(*n));
        for(nod *p=sant->next; p!=sant&& (*n)!=nmax;p=p->next)
        {

            if(fabs(scrmin - p->tp) < 0.0001)
            {
                
                p=deleteNode(&sant,p);
                
                (*n)--;
            }
            x=p;
        }
    }
}


我也尝试过这种方法,但它输出分段错误:

void deleteNode(nod **sant, nod *del)
{
    nod *c=(*sant);
        while((*sant)!=c)
        {
            if((*sant)==del)
            {
                nod *temp= *sant;
                for(int i=0; i<del->np;i++)
                {
                    free(temp->players[i].nume);
                    free(temp->players[i].prenume);
                }
                free(temp->players);
                free(temp->team);
                free(temp);
            }
            else
            {
                sant=&(*sant)->next;
            }
        
        }

}

void adjust(nod *sant, int *n)
{
    int nmax=1;
    while(nmax*2<=(*n))
        nmax*=2;
    for(nod *p=sant->next; p!=sant;p=p->next)
        {
            p->tp= teamScore(p,p->np);

        }
    nod *x=sant;
    float scrmin;
    while((*n)!=nmax)
    {
        scrmin=minScore(sant,(*n));
        for(nod *p=sant->next; p!=sant&& (*n)!=nmax;p=p->next)
        {

            if(fabs(scrmin - p->tp) < 0.0001)
            {
                
                deleteNode(&sant,p);
                p=x;
                printf("%s\n", p->next->team);
                
                (*n)--;
            }
            x=p;
        }
    }
}

以下是重新创建列表的函数:

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

typedef struct team
{
    char *team;
    int np;
    struct player *players;
    struct team *next;
    float tp;

} nod;


typedef struct player
{
    char *nume;
    char *prenume;
    int scor;

} playerNode;

float teamScore(nod *team, int n)
{
    float s=0;
    for(int i=0;i<team->np;i++)
        {
            s=s+team->players[i].scor;
        }
    s=s/n;
    return s;
}

float minScore(nod *sant, int n)
{
    nod *p=sant->next;
    float minp=p->tp;
    for(int i=0;i<n;i++)
    {
        if(minp > p->tp)
        {
            minp=p->tp;
        }
        p=p->next;
    }
    return minp;

}

nod  *deleteNode(nod **sant, nod *del)
{
        if (*sant == NULL || del == NULL)
        return NULL;

        if ((*sant)->next == del)
        (*sant)->next = del->next;

        nod* temp = (*sant)->next;
        while (temp->next != NULL && temp->next != del) 
        {
            temp = temp->next;
        }
         if (temp->next == NULL) 
        {
            return NULL;
        }
        temp->next = del->next;
        for(int i=0; i<del->np;i++)
        {
            free(del->players[i].nume);
            free(del->players[i].prenume);
        }
        free(del->players);
        free(del->team);
        free(del);
        return temp;

}

void adjust(nod *sant, int *n)
{
    int nmax=1;
    while(nmax*2<=(*n))
        nmax*=2;
    for(nod *p=sant->next; p!=sant;p=p->next)
        {
            p->tp= teamScore(p,p->np);

        }
    nod *x=sant;
    float scrmin;
    while((*n)!=nmax)
    {
        scrmin=minScore(sant,(*n));
        for(nod *p=sant->next; p!=sant&& (*n)!=nmax;p=p->next)
        {

            if(fabs(scrmin - p->tp) < 0.0001)
            {
                
                p=deleteNode(&sant,p);
                
                (*n)--;
            }
            x=p;
        }
    }
}
void displayList(nod *sant, FILE *fisier)
{
    for(nod *p=sant->next; p!=sant;p=p->next)
    {
        fprintf(fisier,"%s\n",p->team);
    }

}
nod *pushFront(nod *sant)
{

    FILE *fisier;
    if ((fisier=fopen("d.in", "r"))==NULL)
    {
        puts("File can't be opened!\n");
        exit(1);
    }
    char line[50], str1[100], str2[100], str3[100];
    int n2, scr;
    int n1;
    fscanf(fisier,"%d",&n1);
    for(int j=0;j<n1;j++)
    {
        nod *newNode=(nod*)malloc(sizeof(nod));
        if(sant->next==sant)
        {
            newNode->next= sant;
            sant->next=newNode;
        }
        else
        {
        nod *c = (nod*)malloc(sizeof(nod));
        c=sant->next;
        sant->next=newNode;
        newNode->next=c;
        }
        fscanf(fisier,"%d ", &n2);
        fscanf(fisier,"%[^\n]",line);
        newNode->np=n2;
        newNode->team = malloc(sizeof(char)*(strlen(line) + 1));
        strcpy(newNode->team, line);
        newNode->players=(playerNode*)malloc(sizeof(playerNode)*newNode->np);

        for(int i=0;i<newNode->np;i++)
        {
            fscanf(fisier, "%s %s %s\n", str1, str1, str3);
            scr=atoi(str3);
            (*newNode).players[i].nume = malloc(sizeof(char)*(strlen(str1) + 1));
            (*newNode).players[i].prenume = malloc(sizeof(char)*(strlen(str2) + 1));
            strcpy(newNode->players[i].nume, str1);
            strcpy(newNode->players[i].prenume, str2);
            newNode->players[i].scor=scr;
        }
    }
    return sant;
}
int main () {
    
    nod *sant=(nod*)malloc(sizeof(nod));
    sant->next=sant;
    sant= pushFront(sant, argv);
    FILE *fisier, *output;
    if ((fisier=fopen("d.in", "r"))==NULL)
    {
        puts("File can't be opened!\n");
        exit(1);
    }
    if ((output=fopen("r.out", "w"))==NULL)
    {
        puts("File can't be opened!\n");
        exit(1);
    }
    int n;
    fscanf(fisier,"%d",&n);
    adjust(sant,&n);
    displayList(sant, output);
    return 0;
}

我使用带有断点的 gdb 调试器,正如我之前提到的,它在 deleteNode 函数中进入无限循环,因此它不会输出任何内容,因为它不会在其他函数之前退出 adjustment(...) 函数调用打印到我的输出文件夹。如果我要在 adjustment(...) 之前使用我的 displayList 函数,它就可以正常工作。但 minScore 只适用于 1 次调用,然后在第二次调用中间中断,当它到达 NULL 节点时(我在 minScore 函数中使用了 put 来在调试时解决这个问题)

这是上面名为 d.in 的输入文件中用于填充列表的数据:

90
5 The Waiver Wire
Popa Ana-Maria 6
Popescu Alexandru 10
Pop Mihaela 6
Radu Andreea 2
Dumitru Elena 1

5 Vandelay Industries
Stan Elena-Claudia 4
Stoica Maria-Roxana 0
Gheorghe Alina-Daniela 6
Matei Andreea-Simona 3
Ciobanu Cristina-Daniela 1

5 The Big Egos
Ionescu Grigore 8
Rusu Cosmin-Alexandru 7
Mihai Ionut-Florin 5
Serban Victoria 3
Constantin Marius-Valentin 7

5 Ibuprofen and Ben Gay
Marin Bogdan-Ioan 4
Stefan Alexandra-Georgiana 9
Lazar Cristina-Gabriela 10
Vasile Andreea-Catalina 2
Munteanu Marius-Mihai 0

5 Chafing the Dream
Ilie Cristian-Mihai 10
Oprea Mihai-Catalin 8
Toma Andra-Maria 5
Florea Ioana-Mihaela 0
Moldovan Alexandru-Iulian 4

5 Chunky Monkees
Tudor Tamas 6
Stanci Erzsebet 0
Sandu Vasile-Adrian 10
Ion Alexandru-Marius 3
Andrei Otilia 10

5 The Abusement Park
Dinu Mihaela-Alina 10
Ungureanu Bogdan-Gabriel 7
Anghel Cosmina 10
Barbu Lorand 3
Cristea Claudia-Elena 7

5 Defending Champs
Ionita Cristian-Florin 9
Neagu Andrei-Daniel 7
Enache Marius-Nicolae 8
Badea Vladimir 2
Dragomir Marius-Bogdan 10

5 Shooting Stars
Vlad Ioan-Adrian 7
Mocanu Amalia 10
Grigore Emilian 4
Voicu Alexandru-Stefan 1
Balan Nicolae-Adrian 0

5 MO si F.O.C.A
Craciun Elena-Diana 10
Ivan Diana-Alexandra 4
Dobre Mihaela-Elena 9
Iordache Ionut-Marian 7
Cojocaru Ioana-Roxana 6

5 The Skyrynners
Muresan Corina-Elena 8
Lupu Adrian-Vasile 6
Tanase Valerica 2
Preda Adriana-Maria 2
Coman Cristina-Ioana 7

5 Konfetti
Petre Roxana-Andreea 6
Lungu Traian 6
Ene George-Catalin 5
Manea Norbert 5
Nagy Andrei-Catalin 7

5 Savage
Nicolae Magdalena 4
Roman Dorel 4
Dumitrescu Dumitru-Daniel 4
Simion Jozsef 3
Iancu Bogdan-Florin 6

5 Pufuletii
Iacob Ionut-Cosmin 9
Nistor Constantin-Adrian 10
Nita Mihai-Gabriel 2
Olteanu Andrei-Mihai 4
Rosu Laura-Elena 1

5 Legalize
Pavel Ioana-Alina 0
Filip Andrei-Bogdan 10
Bucur Alexandru-Adrian 9
Avram Razvan-Alexandru 4
Stoian Loredana-Elena 0

5 Black swans
Rus Ionut-Andrei 10
Dragan Mihai-Bogdan 1
Stancu Florin-Catalin 2
Luca Marioara 6
Calin Petruta 9

5 Elixir Junior
Diaconu Alina-Andreea 7
David Mihaela-Gabriela 1
Szabo Andrada 4
Cretu Cristina-Alexandra 9
Costea Carmen-Elena 3

5 Mistretii Atomici
Ghita Alin-Constantin 9
Marcu Florin-Cristian 2
Rotaru Andreea-Raluca 10
Zaharia Cosmin-Ionut 3
Paun Bogdan-Nicolae 7

5 Winner
Baciu Alexandru-Catalin 6
Neacsu Adrian-Constantin 8
Suciu Adrian-Ioan 9
Dan Ramona-Maria 8
Alexandru Adrian-Mihai 9

5 Cei 3 muschetari
Nedelcu Alina-Florentina 4
Georgescu Cristina-Andreea 5
Muntean Ionut-Alin 9
Costache Ioana-Raluca 5
Marinescu Anca-Ioana 9

5 0% ANGELS 
Bogdan Cristian-Alexandru 8
Vilcu Elena-Simona 3
Ciubotariu Marcela 8
Demian Virgil 7
Mustata Iulia-Maria 5

5 ALIGATORS 
Sanda Madalina-Elena 7
Balog Elena-Georgiana 4
Ciocoiu Elena-Raluca 7
Neculai Raluca-Mihaela 7
Olah Alin-Ionut 8

5 AMERICAN BABES
Baluta Natalia 5
Nicu Livia 1
Papp Denisa 10
Badiu Doina 7
Dina Maria-Alina 8

5 ANACONDAS
Truta Andreea-Madalina 0
Mitrache Lacramioara 6
Bercea Mihai-Daniel 10
Deak Izabella 8
Fazakas Maria-Daniela 9

5 ANTEATERS
Tita Cristiana 4
Frincu Mihaela-Andreea 2
Turcanu Iosif 4
Dragusin Eugenia 3
Amariei Sandor 10

5 ASH KICKERS
Todoran Marius-Andrei 3
Hutanu Emanuela 5
Poenaru Ionut-Gabriel 2
Trofin Ionut-Valentin 10
Vizitiu Emese 8

5 AVALANCHE
Mihut Constantin-Daniel 10
Anghelescu Simona-Maria 5
Obreja Daniel-Ionut 0
Andreescu Delia 5
Botea Florentin 10

5 FALCONS
Cadar Andreea-Daniela 6
Mihu Angelica 10
Comanescu Anca-Mihaela 3
Condrea Alina-Ioana 0
Mincu Ionut-Marius 7

5 FIGHTING IRISH 
Muraru Maria-Andreea 10
Ivanov Cristinel 5
Andreica Bogdan-Mihai 6
Bran Andreea-Gabriela 9
Papuc Emoke 10

5 FIRE ANTS
Mihali Daniela-Elena 4
Eftimie Stelian 9
Stana Elena-Gabriela 5
Macarie Alina-Nicoleta 3
Nastasa George-Daniel 7

5 FIRE STRIKERS
Dragnea George-Adrian 2
Margarit Vlad-Alexandru 8
Pirvan Mioara 7
Tomoiaga Florin-Daniel 4
Craciunescu Florin-Adrian 9

5 FIRE CRACKERS
Negrila Ionica 4
Mate Manuela 5
Cimpean Constantin-Catalin 1
Szakacs Felicia 7
Tiron Marina 4

5 FLAMING TIGERS
Ciornei Oana-Cristina 10
Fekete Alexandra-Cristina 6
Mutu Raluca-Ioana 8
Ferencz Eduard 8
Cimpeanu Florin-Alexandru 0

5 FLAME BOYS
Neag Noemi 7
Butnariu Mihai-Andrei 3
Mihaescu Elena-Ramona 9
Velicu Alina-Gabriela 0
Panaite Razvan-Ionut 1

5 FLASH
Avramescu Andrei-Cristian 5
Denes Tatiana 8
Petrut Gina 4
Oana Adrian-Nicolae 0
Bordea Bogdan-Constantin 6

5 FLYING DRAGONS
Cruceru Mihai-Cristian 4
Rotar Adrian-Florin 2
Mardare Dan-Alexandru 2
Miklos Marius-Constantin 9
Barna Eva 6

5 FUGITIVES
Tudoran Bogdan-Andrei 7
Traistaru Alexandru-Ioan 9
Borza Monika 1
Cernea Ruxandra 3
Vacaru Constantin-Cosmin 3

5 MAD DOGS
Bala Oana-Mihaela 8
Maria George-Cristian 8
Onica Reka 8
Manescu Larisa 9
Pruna Orsolya 4

5 MANARCHS
Gavrilescu Mihai-Adrian 0
Ambrus Ildiko 8
Lixandru Madalin 8
Jipa Cristian-Ionut 8
Lup Alexandru-Bogdan 4

5 MAROON MADNESS
Loghin Aurelian 6
Gliga Beata 4
Valeanu Andreea-Nicoleta 7
Balas Alexandru-Florin 4
Bartha Alexandru-Nicolae 2

5 MEAN GREEN ALIENS
Stoicescu Zsuzsanna 9
Vilceanu Marius-Florin 7
Stuparu Dana 10
Bordeianu Alexandra-Mihaela 2
Iosub Alexandru-Constantin 6

5 MERCENARIES
Goia Roxana-Mihaela 3
Lacatusu Calin 4
Danci Alexandru-Marian 8
Pascal Florica 6
Antonescu Marius-Gabriel 1

5 MIDNIGHT RAIDERS
Buruiana Csilla 1
Jakab Elisabeta 0
Fabian Maria-Mihaela 10
Gherghina Marius-Alexandru 0
Virlan Annamaria 4

5 MIGHTY DUCKS
Bulai Geanina 0
Cristache Maria-Mirabela 8
Lepadatu Adela 10
Ureche Marcel 7
Nicolau Petre 7

5 MIGHTY PANTHERS
Doroftei Teodor 1
Joita Mihaela-Cristina 7
Kadar Sorina 2
Abrudan Elena-Loredana 9
Androne Costin 2

5 MIGHTY STINGRAYS
Gruia Marin 6
Vladescu Catalin-Ionut 2
Stoican Andreea-Roxana 4
Costan Alina-Cristina 1
Gherghel Tunde 5

5 MIND TRIP
Paul Emanuel 7
Urs Oana-Elena 8
Goga Ramona-Elena 10
Tabacaru Tiberiu 4
Zamfirescu Gheorghita 8

5 MONEY
Botez Alina-Georgiana 3
Jitaru Oana-Alexandra 6
Bora Lidia 2
Aron Maria-Madalina 1
Badulescu Raluca-Maria 10

5 MUTINY
Tugui Ionut-Bogdan 3
Nanu George-Alexandru 0
Raduta Simona-Elena 10
Pirvulescu Alexandru-Gabriel 0
Olar Ioan-Alexandru 1

5 SABRES
Ioana Oana-Andreea 1
Vancea Kinga 1
Minca Anisoara 7
Benedek Marius-Daniel 0
Sasu Andreea-Alexandra 8

5 SCORPIONS
Eremia Alexandru-Daniel 3
Ursachi Szabolcs 1
Chereches Ecaterina 2
Condurache Andrei-Ionut 4
Curca Adelina 8

5 SECURITY SMURFS
Coca Andreea-Georgiana 4
Dunca Adrian-Ionut 10
Ababei Elena-Mihaela 9
Mezei Lenuta 8
Lita Marilena 9

5 SHARKS
Voda Mihaita 3
Mogos Elena-Madalina 2
Cioara Marius-Catalin 6
Stinga Cristina-Mihaela 0
Corbu Ioana-Andreea 5

5 SHOOTING COMETS
Nichita Diana-Elena 2
Pascalau Ionut-Adrian 4
Voiculescu Ionut-Cristian 0
Gutu Marius-Adrian 4
Ancuta Andrei-Alexandru 3

5 SILVER BUTTERFLIES
Muscalu Georgian 8
Carstea Elena-Roxana 5
Hanganu Dorina 3
Dita Marius-Cristian 7
Buzea Elena-Alexandra 5

5 SILVER PANTHERS
Safta Andreea-Ioana 2
Purice Erika 9
Taranu Dorin 4
Chelariu Angela 10
Nicolaescu Emil 7

5 SILVER STRIKE
Covrig Petrica 1
Iftime Alexandru-Cristian 0
Tudorica Laszlo 9
Burtea Ioana-Cristina 1
Marchis Alexandru-Mihai 4

5 SILVER X- TREME
Danaila Raluca-Elena 4
Juganaru Ionut-Catalin 5
Fieraru Melinda 4
Bordei Mihail 2
Ciorba Anca-Elena 1

5 SLICK CHICS
Hategan Ionut-Daniel 0
Porumb Nicusor 3
Surugiu Florian 1
Csiki Eniko 7
Amza Diana-Maria 4

5 SMASHING PUMPKINS
Vicol Alexandra-Ioana 6
Prisacariu Petronela 9
Capraru Marius-Ionut 6
Zidaru Paula 4
Vornicu Katalin 0

5 SNOW DEVILS
Cismaru Alexandra-Elena 9
Laza Aurel 1
Iovan Bogdan-Alexandru 5
Creanga Andra 1
Dinica Bianca 7

5 SOCCER BEES
Balasoiu Bogdan-Ionut 8
Agache Elena-Daniela 2
Din Stefania 3
Chelu Sebastian 2
Mot Aurelia 1

5 SOCCER DIVAS
Balaceanu Tudor 10
Mihaly Andrea 1
Enciu Roxana-Maria 1
Gorgan Csaba 6
Blanaru Danut 1

5 SOCCER STARS
Stratulat Lavinia 6
Tinca Elena-Andreea 0
Ceausu Ilie 4
Bursuc Octavian 8
Dragne Cornel 0

5 SPACE BABES
Suteu Luminita 5
Varzaru Cornelia 8
Dumitras Maria-Magdalena 2
Vesa Maria-Alexandra 5
Calugaru Levente 2

5 SPARK PLUGS
Furtuna Istvan 4
Postelnicu Andreea-Mihaela 9
Burciu Teodora 9
Mathe Elena-Alina 10
Nutu Eugen 3

5 SPEEDY TURTLES
Stef Ioana-Maria 9
Stroie Andreea-Cristina 8
Lung Emilia 2
Cuc Timea 1
Giurca Mihai-Alexandru 7

5 SPLASH
Manaila Alexandru-Ionut 7
Nacu Viorica 8
Cocos Sergiu 9
Gaman Ionut-Alexandru 10
Manu Zsolt 8

5 STAR SHOOTERS
Benea Roxana-Elena 8
Lazea Zoltan 8
Graur Alexandra-Maria 7
Badita Ioana-Alexandra 10
Stanculescu Ancuta 3

5 STATIC X
Oprisan Marinela 8
Rata Cristina-Maria 3
Leca Rodica 1
Trasca Anca-Maria 2
Brasoveanu Violeta 0

5 STINGRAYS
Ionel Victor 2
Borcan Alina-Mihaela 5
Mihoc Ileana 6
Manciu Silvia 4
Bruma Veronica 10

5 STRIKERS
Ticu Attila 7
Murgu Liviu 8
Miclaus Petru 9
Tanko Cristina-Elena 5
Brinza Vasilica 5

5 SUGER BEES
Grigoriu Alina-Maria 10
Tutuianu Dragos 2
Kerekes Silviu 2
Santa Andreea-Maria 10
Kocsis Adina 1

5 SUNFLOWER GIRLS
Salagean Mircea 7
Voinescu Laurentiu 7
Butoi Georgeta 9
Leahu Maria-Cristina 6
Pauna Paul 4

5 SUPER POWER GIRLS
Dobrica Andreea-Elena 6
Gurau Dan 1
Bot Costel 1
Balla Alina-Elena 1
Maciuca Elena-Cristina 0

5 SUPER STARS
Bejenaru Carmen 5
Zota Claudiu 9
Balta Alin 1
Vieriu Oana-Maria 6
Ciortan Camelia 0

5 SWEETHEARTS
Irimescu Iulia 10
Velea Ovidiu 6
Pricope Florina 6
Coste Robert 6
Nitulescu Catalina 8

5 TARANTULAS
Vrinceanu Razvan 3
Mitrofan Radu 2
Jurj Liliana 3
Vladut Valentina 10
Ciocirlan Viorel 5

5 TEAL TITANS
Tuca Ana 8
Chirea Ciprian 9
Balaci Corina 7
Dediu Laura 8
Floarea Vlad 7

5 TEAM ROCKETS
Dulgheru Loredana 7
Platon Claudia 5
Plesca Monica 3
Bucsa Ramona 5
Borcea Dumitru 9

5 TERMINATORS
Podaru Raluca 5
Pascariu Ionel 10
Glavan Lucian 10
Ana Cosmin 7
Scutaru Sorin 0

5 TIDAL WAVES
Belu Irina 9
Angheluta Diana 10
Catrina Mirela 9
Ivanescu Iuliana 10
Badila Madalina 3

5 TIMBERWOLVES
Prisecaru Anamaria 8
Moroianu Simona 10
Geana Iulian 10
Furdui Roxana 1
Chirca Oana 3

5 TOO HOT
Motoc Catalin 7
Datcu Stefan 2
Burghelea Ion 3
Leu Florentina 0
Galan Anca 2

5 TOUCANS
Mladin George 8
Pascaru Ioan 6
Lupsa Valentin 0
Luta Adriana 3
Vass Ionela 4

5 TUFF-E-NUFF
Oniga Vasile 7
Mates Gabriel 10
Deliu Gabriela 9
Angelescu Nicolae 8
Hagiu Gheorghe 2

5 TWISTING TORNADOES
Codrea Constantin 5
Fechete Nicoleta 3
Dedu Georgiana 1
Druga Mariana 4
Florian Bogdan 8

5 UN MELLOW YELLOW
Ungurean Cristian 10
Mitrica Daniela 0
Iftimie Alina 7
Biris Maria 7
Vranceanu Ioana 0

5 VALLEY GIRLS
Cirjan Cristina 10
Buzdugan Florin 4
Cobzaru Daniel 8
Manda Marian 7
Vincze Marius 3

5 VIPERS
Bulgaru Adrian 0
Grigorie Andrei 2
Placinta Alexandra 5
Antonie Mihai 3
Ciuperca Ionut 0

c memory-management memory-leaks linked-list sentinel
1个回答
0
投票

如果我们把你提到的哨兵东西放在一边,我认为你应该稍微重新安排一下你的deleteNode函数(仅目视编译):

首先,循环查找节点并记住前一个节点

nod* prev = NULL;
for (nod* p = *sant; p != NULL; p = p->next)
{
  // check if it is the one
  if (p == del)
  {
     // now there is first the normal case, the node is not the first
     // node.
     if (prev != NULL)
     {
        prev->next = del->next; // detach node from list
     }
     else // the first item in the list needs to be deleted
     {
        *sant = p->next;
     }
     // now free the contents of del (do a func for this) 
     // and return list 
     freeNode(del);
     return *sant; 
  }
  prev = p;
}
© www.soinside.com 2019 - 2024. All rights reserved.