繁体   English   中英

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

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

主要思想是:制作一份食物+卡路里的清单,将其打印出来,询问要删除的条目,然后使用该条目打印清单。 似乎无法使其正常工作。

我最初只想打印初始列表,但后来决定还要求用户删除要删除的特定条目,然后再次打印列表。 这是我无法使其工作的地方。

编译器给出的错误是:

1. [错误]“结构信息”没有名为“当前”的成员(函数deleteNode中的第91和99行)
2. [错误]取消指向不完整类型的指针(在函数printList中)

到目前为止,这是我的代码:

#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. [错误]“结构信息”没有名为“当前”的成员(函数deleteNode中的第91和99行)

看一下这个声明:

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

然后,您将获得如下所示的变量声明:

struct info* temp = *s

您尝试像这样使用它:

temp->current

但是info结构内部没有current的名称。 相反,还有其他三个名称。 您需要确定其中哪一项最适合您要执行的操作。

2. [错误]取消指向不完整类型的指针(在函数printList中)

查看以下代码行:

void printList(struct list *info)

您没有声明struct list并且正在声明一个名为info变量 ,该变量与您已经声明的struct info 相反,您需要这样的东西:

void printList(struct info* list)

这将声明一个名为list的参数,该参数是指向struct info的指针。 现在,在此printList()函数中您拥有info任何地方,您都需要说list

暂无
暂无

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

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