gpt4 book ai didi

c - 谁能帮我一个C语言的键列表?

转载 作者:行者123 更新时间:2023-11-30 21:49:10 27 4
gpt4 key购买 nike

这是我的应用程序代码:

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

struct node {
int data;
struct node *next;
};

void push(struct node** head_ref, int new_data) {
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void deleteKey(struct node **head_ref, int key) {
struct node* temp = *head_ref, *prev;
while (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
temp = *head_ref; // Change Temp
}

while (temp != NULL) {
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp); // Free memory
temp = prev->next;
}
}

void printList(struct node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}

int main() {
struct node* head = NULL;

push(&head, 7);
push(&head, 2);
push(&head, 3);
push(&head, 2);
push(&head, 8);
push(&head, 1);
push(&head, 2);
push(&head, 2);

int key = 2; // key to delete

puts("Created Linked List: ");
printList(head);

deleteKey(&head, key);
puts("\nLinked List after Deletion of 1: ");

printList(head);
return 0;
}

要求:

#ifndef LNKDLST_H_
#define LNKDLST_H_
/*
* Function : Initialises the list
* Purpose : Ensures that pointers are set to point to the right thing
If the lists exists already it is emptied before
initialising a new.
* Return : 1 indicates success, 0 error

int initialise ();
/*
* Function : put
* Purpose : Add data mapped with key to the end of the list
* Return : 1 indicates success, 0 error

int put (int key, void *data);
/*
* Function : get
* Purpose : Get the first item in the list and return the data
* Return : A pointer to the data, NULL on error

void *get ();
/*
* Function : getKeyd
* Purpose : Get the item with key and return the data
* Return : A pointer to the data, NULL on error

void *getKeyd (int key);
/*
* Function : isEmpty
* Purpose : Checks if the list is empty
* Return : 1 to indicate an empty list, 0 a non empty list

int isEmpty();
/*
* Function : exists
* Purpose : Checks if the item with key exists in the list
* Return : 1 if key exists, 0 if not

int exists (int key);
#endif

我尝试重新设计程序来满足要求,但我发现很难遵循。我需要制作另一个程序还是可以重新设计这个程序?

这是一项家庭作业,但我陷入困境,无法弄清楚如何正确完成它。

最佳答案

如果我没理解错的话,你想通过使用上面编写的程序来达到要求。
可以使用您自己的代码重新设计它。

例如:对于 get()

struct node *get(int key, void *data)
{
if(*head_ref != NULL)
{
key = *head_ref->data;
return *head_ref;
}
else
{
printf("Error in retrieving data from head of list\n");
}
}

通过这种方式,您可以使用程序中的代码并稍微修改它来实现所要求的。如果列表确实存在,则您访问的第一个节点是列表的头部。系统会要求您返回指向列表头部的指针,并将该节点中的数据复制到变量“key”中。如果您仍然遇到问题,我建议您再次了解链表概念,以确保您不会遇到任何问题。您可以引用 Youtube 查找有关这些概念的视频。

关于c - 谁能帮我一个C语言的键列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532250/

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