-3
我有一個關於C編程鏈接列表的問題。 我寫出了這段代碼,但我在運行代碼時遇到了運行時錯誤。 我是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;
}
}
什麼錯誤?運行時還是實際編譯時間?從什麼行(谷歌如何使用調試器,如果它是運行時錯誤,如默認)。 – hyde
請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – GingerPlusPlus
@hyde我編輯了這個問題 – stack