简体   繁体   中英

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)
2.[Error] dereferencing pointer to incomplete type (in function 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)

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. 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)

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. 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 . Now everywhere you have info inside this printList() function, you need to say list instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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