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

时间:2016-01-15 01:06:34

标签: c linked-list

我正在建立一个学生管理系统,用户可以动态地添加学生详细信息。当我第一次在列表开头添加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("%s\n%d\n%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: %d\n", ptrToHead->age);
    //printf("Name: %s\n", ptrToHead->name);
    //printf("Degree: %s\n", 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!\n\n");
    char* mainmenu[] = { "Display All Students", "Add A Student" };

    for (int i = 0; i < 2; i++){
        printf("%d:  %s\n", i + 1, mainmenu[i]);
    }
    printf("\n\nEnter 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("\n\nHow many students do you want to add? Enter Choice: ");
    scanf_s(" %d", &NumberOfStudents);

    printf("\n\n");

    for (int i = 0; i < NumberOfStudents; i++){
        printf("\n\n");


        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("\n\nWhere Do You Want To Add This Student?\n\n1: At The Beginning\n\n2: At A Position N\n\n3: 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("\n\n");

    }
    MainMenu();


}

1 个答案:

答案 0 :(得分:0)

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

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

你只需要一个指向ptrToHead的temp和另外一个temp(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++;
            }
        }
}