我对编码很新,并且正在学校修读C语言课程。我们正在学习C中的数据结构,并且已经给出了一个赋值,它要求我们创建一个包含从1到10的奇数整数的链表,并打印该列表。但是,我写的代码似乎有一些问题。这是:
#include <stdio.h>
#include <stdlib.h>
struct listNode {
int value;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
void createList(ListNodePtr *sPtr);
void printList(ListNodePtr currentPtr);
int main(void) {
ListNodePtr list1Ptr;
list1Ptr = NULL;
list1Ptr->value = 1;
createList(&list1Ptr);
printf("List 1 is: ");
printList(list1Ptr);
printf("End of run.\n");
return 0;
}
void createList(ListNodePtr *sPtr) {
ListNodePtr previousPtr;
ListNodePtr newPtr;
newPtr = malloc(sizeof(ListNodePtr));
if (newPtr != NULL) {
newPtr = *sPtr;
previousPtr = NULL;
while (((*sPtr)->value) <= 10) {
previousPtr = *sPtr;
*sPtr = (*sPtr)->nextPtr;
(*sPtr)->value = previousPtr->value + 2;
}
}
else {
printf("No memory available.\n");
}
}
int isEmpty(ListNodePtr sPtr) {
return sPtr == NULL;
}
void printList(ListNodePtr currentPtr) {
if (currentPtr == NULL) {
printf("List is empty.\n\n");
}
else {
while (currentPtr != NULL) {
printf("%d ", currentPtr->value);
currentPtr = currentPtr->nextPtr;
}
printf("*\n");
}
}
所需的输出:[列表1是:1 3 5 7 9 *] 我使用的环境一直告诉我,有&#34;未处理的异常&#34; (我不太确定这意味着什么)或者只是中途停止运行。关于我如何能够达到预期结果的任何想法?
非常感谢任何其他提示和建议。谢谢!
答案 0 :(得分:1)
逻辑上的错误以及pointers
的使用都很少。可能是以下代码有用。评论是否需要澄清:
#include <stdio.h>
#include <stdlib.h>
#define START_COUNT 1
#define INCREMENT_COUNT 2
#define END_COUNT 10
struct listNode {
int value;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode* ListNodePtr;
ListNodePtr createList(ListNodePtr sPtr, int val);
void printList(ListNodePtr currentPtr);
int main(void) {
ListNodePtr list1Ptr;
int counter = END_COUNT;
list1Ptr = NULL;
do
{
list1Ptr = createList(list1Ptr, START_COUNT);
counter -= INCREMENT_COUNT;
}while(counter>0);
printf("List 1 is: ");
printList(list1Ptr);
printf("End of run.\n");
return 0;
}
ListNodePtr createList(ListNodePtr sPtr, int val) {
ListNodePtr previousPtr;
ListNodePtr newPtr;
newPtr = malloc(sizeof(ListNode));
if (newPtr != NULL) {
//Make 'newPtr' very first node
newPtr->nextPtr =sPtr;
newPtr->value = val;
sPtr = newPtr;
//(sPtr != NULL) is to check for [1] and (sPtr->nextPtr != NULL) for [2]
while ((sPtr != NULL) && (sPtr->nextPtr != NULL) && (sPtr->value <= END_COUNT)) {
previousPtr = sPtr; //[1]
sPtr = sPtr->nextPtr; //[2]
sPtr->value = previousPtr->value + INCREMENT_COUNT; //[1] and [2]
}
}
else {
printf("No memory available.\n");
}
//Set to the very first Node
return newPtr;
}
int isEmpty(ListNodePtr sPtr) {
return sPtr == NULL;
}
void printList(ListNodePtr currentPtr) {
if (currentPtr == NULL) {
printf("List is empty.\n\n");
}
else {
while (currentPtr != NULL) {
printf("%d ", currentPtr->value);
currentPtr = currentPtr->nextPtr;
}
printf("*\n");
}
}
答案 1 :(得分:0)
while (((*sPtr)->value) <= 10) { <<<<<
previousPtr = *sPtr;
*sPtr = (*sPtr)->nextPtr;
(*sPtr)->value = previousPtr->value + 2; <<<<<
}
在尝试解除引用之前,您必须先检查(*sPtr)
NULL
。
答案 2 :(得分:0)
恕我直言,这种构造函数函数最直观的界面是
struct listNode *listNode_generate(int start, int stop, int step)
{
struct listNode *result, **pp;
if (!step) return NULL;
result = NULL, pp = &result;
for ( ; start <= stop; start += step) {
*pp = malloc (sizeof **pp);
if (!*pp) break;
(*pp)->value = start;
pp = &(*pp)->next;
}
*pp = NULL;
return result;
}
在main()
中调用,如:
list1Ptr = listNode_generate(1,10,2);