C - 在链表中的第 n 个位置插入节点不起作用



我正在构建一个学生管理系统,用户可以在其中动态添加学生详细信息。当我第一次在列表开头添加 2 名学生时,学生添加正确。但是,当我在示例 1 的位置添加另一个学生时(这意味着我试图再次将其作为头节点),程序崩溃。我尝试找出错误但没有成功。程序只是崩溃

    #pragma warning(disable: 4996)
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <Windows.h>


struct students{
    char *name;
    int age;
    char *degree;
    struct students* next;
};
int TotalStudents = 0;
struct students* ptrToHead;
void insertAtBeginning(char name[], int age, char degree[]){
    struct students * temp = (students *)malloc(sizeof(struct students));
    temp->name= strdup(name);
    temp->age = age;
    temp->degree=strdup(degree);
    temp->next = NULL;
    if (ptrToHead != NULL)
    {
        temp->next = ptrToHead;
    }
    ptrToHead = temp;
    //printf("%sn%dn%s", temp->name, temp->age, temp->degree);
}
void insertAtAnyPosition(int position, char name[], int age, char degree[]){
    int i = 1;
    struct students * temp = (students *)malloc(sizeof(struct students));
    struct students * temp2 =  ptrToHead, *temp3;
    temp = ptrToHead;
    if (position <= TotalStudents || position <= TotalStudents + 1){
        while (i < position){
            if (i == position - 1)
                temp3 = temp2;

            temp2 = temp2->next;
            i++;
        }
    }
    temp->name = strdup(name);
    temp->age = age;
    temp->degree = strdup(degree);
    temp2->next = temp;
    temp--;
    temp = temp2;
}
void MainMenu();
void addStudent();

void print(){
    struct students* temp = ptrToHead;
    printf("List of Students: ");
    while (temp != NULL){
        printf("nStudent's Name: %s", temp->name);
        printf("nStudent's Age: %d", temp->age);
        printf("nStudent's Degree: %s", temp->degree);
        printf("nEND - OF - STUDENT");
        temp = temp->next;
    }
    printf("n");
    Sleep(7000);
    system("cls");
    MainMenu();
}

int main(){
    MainMenu();
    //students * temp= (students *)malloc(sizeof(students));
    //temp->age = 22;
    //temp->degree = "Software Engineering";
    //temp->name = "Fahad Bin Saleem";
    //temp->next = NULL;
    //ptrToHead = temp;
    //
    //printf("Age: %dn", ptrToHead->age);
    //printf("Name: %sn", ptrToHead->name);
    //printf("Degree: %sn", ptrToHead->degree);

    //temp = (students *)malloc(sizeof(students));
    //temp->age = 19;
    //temp->degree = "Electrical Engineering";
    //temp->name = "Rafay Hayat Ali";
    //temp->next = NULL;

    //students * temp1 = ptrToHead;
    //while (temp1->next != NULL){
    //  temp1 = temp1->next;

    //}
    //temp1->next = temp;
    //




    _getch();
    return 0;
}
void MainMenu(){
    int choice;
    printf("Welcome to Student Information Center!nn");
    char* mainmenu[] = { "Display All Students", "Add A Student" };
    for (int i = 0; i < 2; i++){
        printf("%d:  %sn", i + 1, mainmenu[i]);
    }
    printf("nnEnter Your Choice: ");
    scanf_s(" %d", &choice);
    if (choice == 2){
        addStudent();
    }
    if (choice == 1){
        print();
    }

}
void addStudent(){
    int NumberOfStudents;
    int choiceOfAdding;
    char tempName[40];
    char tempDegree[40];
    int tempAge;
    system("cls");

    ptrToHead = NULL;
    for (int i = 0; i < 15; i++){
        printf("  ");
    }
    printf("**ADD A STUDENT**");
    printf("nnHow many students do you want to add? Enter Choice: ");
    scanf_s(" %d", &NumberOfStudents);
    printf("nn");
    for (int i = 0; i < NumberOfStudents; i++){
        printf("nn");

        printf("Enter Student's Name:  ");
        fflush(stdin);
        gets_s(tempName, 40);
        printf("Enter Student's Age:  ");
        scanf_s(" %d", &tempAge);
        printf("Enter Student's Degree:  ");
        fflush(stdin);
        gets_s(tempDegree, 40);
        //insert(tempName, tempAge, tempAgeDegree);

        printf("nnWhere Do You Want To Add This Student?nn1: At The Beginningnn2: At A Position Nnn3: At The End");
        scanf_s(" %d", &choiceOfAdding);
        fflush(stdin);
        if (choiceOfAdding == 1){
            insertAtBeginning(tempName, tempAge, tempDegree);
        }
        else if (choiceOfAdding == 2){
            int position;
            printf("nEnter the position you want to insert: ");
            scanf_s(" %d", &position);
            insertAtAnyPosition(position, tempName, tempAge, tempDegree);
        }
        TotalStudents++;

        printf("nn");
    }
    MainMenu();

}

你的问题是你的 temp2 和 temp 等于 ptrHead:

struct students * temp = (students *)malloc(sizeof(struct students));
struct students * temp2 =  ptrToHead, *temp3;
temp = ptrToHead;

您只需要一个指向 ptrToHead 的临时和额外的温度 (temp2) 即可进行交换。在您的代码中,您不需要 temp3。

我不是专家,但试一试:

void insertAtAnyPosition(int position, char name[], int age, char degree[]){
    int i = 1;
    struct students * temp = (students *)malloc(sizeof(struct students));
    struct students *temp2;
    temp->name = strdup(name);
    temp->age = age;
    temp->degree = strdup(degree);
    temp2 = &ptrToHead;
    if (position <= TotalStudents + 1){
        while (i < position){
            if (i == position - 1)
                temp->next = temp2->next;
                temp2->next = temp;
                temp2 = temp2->next;
                i++;
            }
        }
}

相关内容

  • 没有找到相关文章

最新更新