gpt4 book ai didi

c - C编程中的链表-无法运行程序

转载 作者:行者123 更新时间:2023-11-30 21:01:55 25 4
gpt4 key购买 nike

我有一个关于 C 编程中的链表的问题。我编写了这段代码,但在尝试运行该代码时出现运行时错误。我是 C 编程新手,请帮忙。

  • Project1.exe 中的 0x00161A66 处抛出异常:0xC0000005:写入位置 0xCCCCCCEC 时出现访问冲突。
  • Project1.exe 中 0x00161A66 处出现未处理的异常:0xC0000005:写入位置 0xCCCCCCEC 时出现访问冲突。插入 Thompson 时发生错误

下面是我正在尝试解决的问题。我无法按字母顺序打印它们。

我正在做的问题是

a) Create a pointer to the start of the list called startPtr. The list is empty.
b) Create a new node of type GradeNode that’s pointed to by pointer newPtr of type GradeNodePtr.
Assign the string "Jones" to member lastName and the value 91.5 to member
grade (use strcpy). Provide any necessary declarations and statements.
c) Assume that the list pointed to by startPtr currently consists of 2 nodes—one containing
"Jones" and one containing "Smith". The nodes are in alphabetical order. Provide
the statements necessary to insert in order nodes containing the following data for
lastName and grade:
"Adams" 85.0
"Thompson" 73.5
"Pritchard" 66.5

这是代码:

  #include <stdio.h>
#include <string.h>
int main(){
struct gradeNode{
char lastName [20];
double grade;
struct gradeNode *nextPtr;
};

typedef struct gradeNode GradeNode;
typedef GradeNode *GradeNodePtr;
GradeNodePtr startPtr = NULL;
GradeNodePtr currentPtr = NULL;
GradeNodePtr previousPtr = NULL;

GradeNodePtr newPtr;
if (newPtr != NULL){
newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr -> lastName,"Jones");
newPtr -> grade = 91.5;
newPtr -> nextPtr = NULL;

}

//Insert "Adams"
//previousPtr is NULL, and currentPtr points to the first node in the list.
newPtr -> nextPtr = currentPtr;
startPtr = newPtr;


newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr -> lastName,"Smith");
newPtr -> grade = 40.5;
newPtr -> nextPtr = NULL;

newPtr -> nextPtr = currentPtr;
startPtr = newPtr;

newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr -> lastName,"Adams");
newPtr -> grade = 85.0;


//Insert "Thompson"
//previousPtr points to the last node in the list(containing Smith") and currentPtr is NULL
newPtr -> nextPtr = currentPtr; //or newPtr -> nextPtr = NULL
previousPtr -> nextPtr = newPtr;

newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr -> lastName,"Thompson");
newPtr -> grade = 73.5;

//Insert "Pritchard"
//previousPtr points to the node containing "Jones" and currentPtr points to the node contaiing "Smith"
newPtr -> nextPtr = currentPtr;
previousPtr -> nextPtr = newPtr;

newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr -> lastName,"Pritchard");
newPtr -> grade = 66.5;

currentPtr = startPtr;
while(currentPtr!=NULL){
printf("Lastname = %s\nGrade = %.1f\n\n",currentPtr->lastName,currentPtr->grade);
currentPtr = currentPtr->nextPtr;
}






}

最佳答案

首先,你应该弄清楚你的逻辑。
这里有一些你可以使用的技巧。

  1. 您没有包含 stdlib.h,其中包含 malloc 的定义。在这种情况下,编译器将发出一条警告消息
  2. 创建 newPtr 后,您将检查其是否为 NULL。它最初将为 NULL 或一些垃圾值。仅当内存不为 NULL 时才分配内存,这是不正确的
  3. 您没有正确链接节点。首先在论文中阐述您的逻辑,然后尝试对其进行编码。

关于c - C编程中的链表-无法运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951724/

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