sách gpt4 ăn đã đi

创建列表的列表(结构)

In lại Tác giả: Walker 123 更新时间:2023-11-30 15:27:33 32 4
mua khóa gpt4 giày nike

我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够

我创建了一个结构,如下

struct node
{
float coef;
int expo;
struct node *link;
};

由此,我可以创建指向每个具有不同名称的结构的指针,但是我遇到的问题是我希望用户输入来确定我的结构数量。我还希望能够添加和创建不同的结构。

câu trả lời hay nhất

您需要创建一个头指针来跟踪您的列表。

这可能会有所帮助:

struct list
{
struct node *head;
int count;
};

并且您需要为每个新节点分配内存并将头移动到新创建的节点。

struct node *add_node(struct list *pList, float coef, int expo)
{
if (pList == NULL)
{
return NULL;
}

struct *node pNode = (struct node*)malloc(sizeof(struct node));

if (node == NULL)
{
return NULL;
}

pNode->coef = coef;
pNode->expo = expo;
pNode->link = pList->head;

pList->head = pNode;
pList->count++;

return pNode;
}

要删除一个元素,您需要向前移动头部,然后释放它以前的内存。

void *delete_node(struct list *pList, float coef, int expo)
{
if (pList == NULL)
{
return NULL;
}


struct node *tmp = pList->head;

pList->head = pList->head->link;

free(tmp);

pList->count--;
}

Để ý:

这不是最终版本。我只是指出应该做什么。

关于创建列表的列表(结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26936670/

32 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress