链表头删除后的垃圾打印

时间:2016-06-24 17:59:28

标签: c linked-list

每个教室学生(aluno)代表一个链表节点。

按学生姓名删除后,我的打印功能会在删除的学生前“位置”上打印垃圾。

[编辑]添加完整代码。

完整代码:

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

struct student{
    char *name;
    int number;
    struct student *next;
};

struct student *constructor(char *name, int number){
    struct student *newNode = (struct student *)malloc(sizeof(struct student));
    newNode->name = name;
    newNode->number = number;
    newNode->next = NULL;

    return newNode;
}

struct student *newStudent(struct student *node, char *name, int number){
    if (node == NULL)
        constructor(name, number);

    struct student *p = node;

    while(p->next != NULL) p = p->next;
    p->next = constructor(name,number);

    return node;
}

struct student *removeAluno(struct student *node, char *name){
    struct student *p = node;

    if (strcmp(p->name,name) == 0){
        struct student *devolver = p->next;
        free(p);
        return devolver;
    } //Removes head

    while(p->next != NULL){

        if (strcmp(p->next->name,name) == 0){

            if (p->next->next == NULL){ //Removes last element
                struct student *remover = p->next;
                p->next = NULL;
                free(remover);
            } 
            else{ //Removes any element between head and last
                struct student *remover = p->next; 
                p->next = p->next->next;
                free(remover);
            }

        }   
        else 
            p = p->next;
    }

    return p;
}

void printClass(struct student *students){
    struct student *p = students;

    while(p->next != NULL){
        printf("\nNome: %s, Numero: %d",p->name,p->number);
        p = p->next;
    }
    printf("\nNome: %s, Numero: %d",p->name,p->number);
    printf("\n");
}


int main(){
    struct student *a = constructor("Michael",15);
    newStudent(a,"John",14);
    newStudent(a,"Jack",13);
    printClass(a);
    removeAluno(a,"Michael");
    printClass(a);
    removeAluno(a,"John");
    printClass(a);
    removeAluno(a,"Jack");
    printClass(a);
    return 0;
}

我的输出:

//list after inserting Michael, John, Jack (in this order)
Nome: Michael, Numero: 15
Nome: John, Numero: 14
Nome: Jack, Numero: 13
//list after removing Michael
o, Numero: 7283408 //junk
Nome: John, Numero: 14
Nome: Jack, Numero: 13
//list after removing John
o, Numero: 7283408 //junk
Nome: Jack, Numero: 13
//list after removing Jack
Nome: ╚6o, Numero: 7283408 //junk

2 个答案:

答案 0 :(得分:0)

您的函数return结构指针,但在main中您不使用它们的返回。您需要做的是main -

a=newStudent(a,"John",14);

其他功能相同。

答案 1 :(得分:0)

您可以使用指针指针:

例如,你可以将你的删除功能改为:

struct student *removeAluno(struct student **node, char *name){
    struct student *p = *node;

    if (strcmp(p->name,name) == 0){
        struct student *devolver = p->next;
        free(p);

        *node = devolver;

        return devolver;
    } //Removes head

    while(p->next != NULL){

        if (strcmp(p->next->name,name) == 0){

            if (p->next->next == NULL){ //Removes last element
                struct student *remover = p->next;
                p->next = NULL;
                free(remover);

                *node = NULL;
            } 
            else{ //Removes any element between head and last
                struct student *remover = p->next; 
                p->next = p->next->next;
                free(remover);
            }

        }   
        else{ 
            p = p->next;

        }
    }

    return p;
}

为了避免列表为空时出现问题,您的打印功能可以更改为:

void printClass(struct student *students){
    if(students==NULL)
        return;

    struct student *p = students;

    while(p->next != NULL){
        printf("\nNome: %s, Numero: %d",p->name,p->number);
        p = p->next;
    }
    printf("\nNome: %s, Numero: %d",p->name,p->number);
    printf("\n");
}

我试过了,得到了这个输出:

Nome: Michael, Numero: 15
Nome: John, Numero: 14
Nome: Jack, Numero: 13

Nome: John, Numero: 14
Nome: Jack, Numero: 13

Nome: Jack, Numero: 13