gpt4 book ai didi

c - 使通用代码适应 C 中不同大小的链表

转载 作者:行者123 更新时间:2023-11-30 17:29:36 26 4
gpt4 key购买 nike

在一项编程作业中,我们被要求从文本文件中读取一些数据并用这些数据填充链接列表。这是我们得到的示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define MAX_INPUT 20
#define EXTRA_CHARS 2

typedef struct listNode
{
int data;
struct listNode * next;
} ListNode;

typedef ListNode * ListNodePtr;

int main()
{
ListNodePtr head, new, current, previous, next;
unsigned listSize;

int i, anInt;
char str[MAX_INPUT];

listSize = 0;
head = NULL;

while (fgets(str, MAX_INPUT+EXTRA_CHARS, stdin) != NULL)
{
/* Parsing the string to int */
if(sscanf(str,"%d",&anInt) != 1)
{
sprintf(str, "Invalid input entered \n");
exit(EXIT_FAILURE);
}

/* Creating the node using malloc(...) */

if ( (new=malloc(sizeof(ListNode))) == NULL)
{
fprintf(stderr,"\nMemory Allocation for ListInsert failed!\n");
fprintf(stderr,"Aborting data entry!\n");
break;
}

current = head;
previous = NULL;
/* Search to find where in insert new list node */
while (current != NULL && current->data < anInt)
{
previous = current;
current = current->next;
}

new->data = anInt;
new->next = current;
listSize++;

if (previous == NULL)
{
head = new;
}
else
{
previous->next = new;
}
}/*End of input loop */

/* Display integers in linked list */
current = head;
while (current != NULL)
{
printf("%d\n", current->data);
current = current->next;
}

/* Deallocate memory used by list nodes */
current = head;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}

return EXIT_SUCCESS;
}

这是我遇到的问题。到目前为止,我在网上或书中看到的每个链表示例中,链表的定义都是作为一个结构体给出的,该结构体仅包含一项数据和一个指向列表中下一个节点的指针。问题是我们已经获得了以下结构定义来填充数据:

typedef struct price
{
unsigned dollars;
unsigned cents;
} PriceType;

typedef struct item
{
char itemID[ID_LEN + 1];
char itemName[MAX_NAME_LEN + 1];
PriceType prices[NUM_PRICES];
char itemDescription[MAX_DESC_LEN + 1];
ItemTypePtr nextItem;
} ItemType;

typedef struct category
{
char categoryID[ID_LEN + 1];
char categoryName[MAX_NAME_LEN + 1];
char drinkType; /* (H)ot or (C)old. */
char categoryDescription[MAX_DESC_LEN + 1];
CategoryTypePtr nextCategory;
ItemTypePtr headItem;
unsigned numItems;
} CategoryType;

typedef struct bcs
{
CategoryTypePtr headCategory; /* Pointer to the next node */
unsigned numCategories;
} BCSType;

这与我见过的所有示例都不相符。因此,在上面的“通用”代码中,我是否必须执行上述所有操作,但将“new->data”部分替换为例如“category->categoryID”和“category->categoryName”等结构体的成员以便用数据填充整个链表?

最佳答案

你所需要的数据结构方面的一切都已经给你了。有一个顶级项目,BCSType,它通向其他所有内容。它有一个类别链接列表,其头链接位于 headCategory 中。每个 CategoryType 都有一个下一个指针 (nextCategory) 和指向 ItemTypes 链接列表的头链接 (headItem) >,每个都有一个下一个指针,nextItem。您不需要也不应该向这些结构添加任何内容(如果这样做,您的评分将会降低)。现在您需要编写从文件中读取数据并创建这些数据结构的实例的代码,使用示例代码作为处理链表的指南......但是您必须思考并发挥创造力来应用它到您所获得的三层结构。

这里最重要的是通过实践来学习。尝试编写这样的代码并且不要放弃,但是如果您真的陷入困境,您可以作为最后的手段,在这里提出另一个问题......但是在这样做时要非常具体,并包括您的代码和错误消息以及所有有关您预期发生的情况和实际发生的情况的其他相关信息。在发布这样的问题之前,请务必学习如何使用调试器并使用它。

关于c - 使通用代码适应 C 中不同大小的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25578117/

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