简体   繁体   English

我如何从该列表中删除一个节点然后进行打印?

[英]How can i remove a node from this list and then print it?

The main idea: make a list of food+calories, print it, ask for what entry to remove then print the list with that removed entry. 主要思想是:制作一份食物+卡路里的清单,将其打印出来,询问要删除的条目,然后使用该条目打印清单。 Can't seem to make it work. 似乎无法使其正常工作。

I originally only wanted to print the initial list, but then later on decided to also ask the user for a specific entry to be removed and the list printed again. 我最初只想打印初始列表,但后来决定还要求用户删除要删除的特定条目,然后再次打印列表。 This is where i failed to make it work. 这是我无法使其工作的地方。

The errors given by the compiler are: 编译器给出的错误是:

1.[Error] 'struct info' has no member named 'current'(lines 91 and 99 in function deleteNode) 1. [错误]“结构信息”没有名为“当前”的成员(函数deleteNode中的第91和99行)
2.[Error] dereferencing pointer to incomplete type (in function printList) 2. [错误]取消指向不完整类型的指针(在函数printList中)

This is my code so far: 到目前为止,这是我的代码:

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

struct info {
int calories;
char name[100];
struct info *next;
};

void add_info(struct info *s);
struct info *create(void);
void printList();
void deleteNode ();

int main()
{
struct info *first;
struct info *current;
struct info *new;      
int x, i, y;

printf("\t\tHow many entries do you want? ");
scanf("%d",&x);

first = create();
current = first;

for(i=0;i<x;i++)
{
    if(i==0)
    {

        first = create();
        current = first;
    }
    else
    {
        new = create();
        current->next = new;
        current = new;
    }
    add_info(current);
}
current->next = NULL;

current = first;       
while(current)
{
    printf("\n\nCalories per food:  %d\t Name of the food: %s\n",current->calories,current->name);
    current = current->next;
}
printf("Which entry would you like to remove? ");
scanf("%d", &y);
deleteNode(y);
printf("The list after deletion is: ");
printfList();

return(0);
}


void add_info(struct info *s)
{
printf("Insert number of calories: ");
scanf("%d",&s->calories);
printf("\n Insert name of the food: ");
scanf("%s",&s->name);
s->next = NULL;
}


struct info *create(void)
{
struct info *initial;

initial = (struct info *)malloc(sizeof(struct info));
if( initial == NULL)
{
    printf("Memory error");
    exit(1);
}
return(initial);
}

void deleteNode(struct info **s, int y)
{

struct info* temp = *s, *prev;


if (temp != NULL && temp->current == y)
{
    *s = temp->next;  
    free(temp);              
    return;
}


while (temp != NULL && temp->current != y)
{
    prev = temp;
    temp = temp->next;
}


if (temp == NULL) return;


prev->next = temp->next;

free(temp);
}

void printList(struct list *info)
{
while (info != NULL)
{
    printf(" %d %s ", info->calories, info->name);
    info = info->next;
}
}

1.[Error] 'struct info' has no member named 'current'(lines 91 and 99 in function deleteNode) 1. [错误]“结构信息”没有名为“当前”的成员(函数deleteNode中的第91和99行)

Look at this declaration: 看一下这个声明:

struct info {
    int calories;
    char name[100];
    struct info *next;
};

Then you have a variable declaration like this: 然后,您将获得如下所示的变量声明:

struct info* temp = *s

And you try to use it like this: 您尝试像这样使用它:

temp->current

but there is no name current inside the info struct. 但是info结构内部没有current的名称。 There are three other names instead. 相反,还有其他三个名称。 You need to decide which of those is most appropriate for what you are trying to do. 您需要确定其中哪一项最适合您要执行的操作。

2.[Error] dereferencing pointer to incomplete type (in function printList) 2. [错误]取消指向不完整类型的指针(在函数printList中)

Look at this line of code: 查看以下代码行:

void printList(struct list *info)

You have no declaration struct list and you are declaring a variable named info which is not the same as the struct info which you already declared. 您没有声明struct list并且正在声明一个名为info变量 ,该变量与您已经声明的struct info Instead you need something like this: 相反,您需要这样的东西:

void printList(struct info* list)

This declares a parameter named list which is a pointer to a struct info . 这将声明一个名为list的参数,该参数是指向struct info的指针。 Now everywhere you have info inside this printList() function, you need to say list instead. 现在,在此printList()函数中您拥有info任何地方,您都需要说list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM