删除链表中的第一个节点

时间:2015-08-13 18:02:24

标签: c linked-list

我的作业有问题,处理编辑链表。我的代码似乎工作正常,除了它不删除学生ID = 1(或链表中的第一个节点)的数据项。 当我尝试删除链表上的第一个节点(或删除main()中的(1))时,它返回“找不到学生”消息。 有人可以告诉我出了什么问题吗?感谢。

/****************************************************************************  
    Date        : 12 August 2015  
    Assignment  : Use linked list to build a database of student records, which supports find(), insert() and delete()  
    ****************************************************************************/

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

/* Define structure studentNode that contains ID, name, GPA and pointer to next studentNode*/
typedef struct student {
    int ID;
    char *name;
    float GPA;
    struct student *nextStudentNode;
} studentNode;

/* Initialize header */
studentNode *header;

/* Function prototypes*/
void insert(int ID, char *name, float GPA);
void printList();
void delete(int ID);

/* Main function which runs through while loop until sentinel value of q is typed by user. */
int main() {
    insert(3, "John", 3.5);
    insert(12, "Smith", 2.8);
    insert(1, "Mary", 1.8);

    printList();

    delete(1);    /***** there seems to be a problem here*********/
    delete(12);
    delete(3);
    printList();

    return 0;
}

/* Insert new student data */
void insert(int ID, char *name, float GPA) {
    studentNode *newStudent = (studentNode*)malloc(sizeof(struct student));

    newStudent->ID          = ID;
    newStudent->name        = name;
    newStudent->GPA         = GPA;

    newStudent->nextStudentNode = header; 
    header = newStudent;
}

/* Run through linked list one by one to print all data items */
void printList() {
    studentNode *tempNode;
    for (tempNode = header ; tempNode != NULL ; tempNode = tempNode->nextStudentNode) {
        printf( "ID:%d name:%s GPA:%f\n" , tempNode->ID, tempNode->name, tempNode->GPA);
    }
}

/* Delete student data */
void delete(int inputID) {
    studentNode *tempNode;
    studentNode **headerPtr = &header;

    for (tempNode = *headerPtr ;
         tempNode->nextStudentNode->ID != inputID && tempNode->nextStudentNode->nextStudentNode != NULL;
         tempNode = tempNode->nextStudentNode);

    studentNode *nodeToDelete = tempNode->nextStudentNode;

    if (nodeToDelete->ID != inputID) {
        printf("Student not found.\n");
    } else {
        printf("ID:%d name:%s GPA:%f is deleted.\n" , nodeToDelete->ID, nodeToDelete->name,
               nodeToDelete->GPA);

        tempNode->nextStudentNode = nodeToDelete->nextStudentNode;
        free(nodeToDelete);
    }
}

2 个答案:

答案 0 :(得分:1)

问题在于:

for( tempNode = *headerPtr ; tempNode->nextStudentNode->ID != inputID && tempNode->nextStudentNode->nextStudentNode != NULL ; tempNode = tempNode->nextStudentNode );

你开始检查第二个学生,而不是第一个学生。因此,您将在未找到正确ID的情况下到达列表的末尾。

更新:我的意思是列表顺序。

答案 1 :(得分:0)

在函数的第一个循环中的这个条件

tempNode->nextStudentNode->ID != inputID && tempNode->nextStudentNode->nextStudentNode != NULL ;

错误,因为它在第一个节点中跳过ID。

该功能可以写得更简单

/* Delete student data */
void delete( int inputID )
{
    studentNode *prev = NULL;
    studentNode *tempNode = head;

    while ( tempNode && tempNode->ID != inputID )
    {
        prev = tempNode;
        tempNode = tempNpde->nextStudentNode;
    }

    if ( tempNode )
    {
        if ( !prev ) head = tempNode->nextStudentNode;
        else prev->nextStudentNode = tempNode->nextStudentNode;

        free( tempNode );
    }
}