gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:06:20 24 4
gpt4 key购买 nike

我正在构建一个学生管理系统,用户可以在其中动态添加学生详细信息。当我第一次在列表开头添加 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();


}

最佳答案

你的问题是你的 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++;
}
}
}

关于c - 在链表中的第 n 个位置插入节点不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34802722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com