0

In a program I'm writing I need a linked list, so it's a pretty specific implementation. It needs:

  1. the ability to add a node to the end
  2. the ability to remove a node whose data matches a specified value

The data is a cstring, no more than 20 characters in length. I'm not very experienced with C and am getting errors with the following signature void addToEnd(llist root, char entery[51]). I tried replacing llist with node but then the error is "unknown type name node". How can I get rid of this?

Here's the code

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

typedef struct node
{
  char entery[51];
  struct node* next;
} llist;

/*may be losing root address permanently*/
void addToEnd(llist root, char entery[51])
{
    while(root->next != NULL)
        root = root->next;
    node last = malloc(sizeof(struct node));
    root->next = last;
    strcpy(last, entery);
}

int main()
{
    struct node *root = malloc(sizeof(struct node));
    root->next = NULL;
    strcpy(root->entery, "Hello");

    struct node *conductor = root;//points to a node while traversing the list

    if(conductor != 0)
        while(conductor->next != 0)
            conductor = conductor->next;

    /* Creates a node at the end of the list */
    conductor->next = malloc(sizeof(struct node));

    conductor = conductor->next;

    if (conductor == NULL)
    {
        printf( "Out of memory" );
        return EXIT_SUCCESS;
    }
    /* initialize the new memory */
    conductor->next = NULL;
    strcpy(conductor->entery, " world\n");

    addToEnd(root, " at the");
    addToEnd(root, " end");

    /*print everything in list*/
    conductor = root;
    if(conductor != NULL)
    {
        while(conductor->next != NULL)
        {
            printf("%s", conductor->entery);
            conductor = conductor->next;
        }
        printf("%s", conductor->entery);
    }

    return EXIT_SUCCESS;
}

One thing I'm unclear about, is in all the examples I've seen is they typedef the struct. Why? Let me elaborate: how do you know if you want to be passing just node or struct node. Also I don't really see the point, struct node isn't that much longer than a single typedef'd name.

Community
  • 1
  • 1
Celeritas
  • 14,489
  • 36
  • 113
  • 194

2 Answers2

2

Problems:

  1. line 12: void addToEnd(llist root, char entery[51]) shall be void addToEnd(llist *root, char entery[51]). Here root must be a pointer type or you actually can not modify its value inside the function and make it visible outside the function.

  2. line 16: node last = malloc(sizeof(struct node)); shall be struct node *last = malloc(sizeof(struct node));. Since in C you must reference a type name with the keyword struct, and also it shall be a pointer or it cannot be initialized with malloc.

As for your typedef question, I believe it is optional and people use it only for convenience. Personally I don't use typedef on a struct very often.

EDITED:

Also your code comes with bugs. Sorry I was only focusing on the syntax before.

Please notice that malloc in C don't assure you that the allocated memory is zeored, it's actually could be anything inside. So you need to fill it manually: to add a line last->next = NULL; at the end of addToEnd.

starrify
  • 14,307
  • 5
  • 33
  • 50
1

To refer to your struct of the linked list, use struct node, after the typedef, you can also use llist. You can also ues, as the linked question uses.

typedef struct node
{
  char entery[51];
  struct node* next;
} node;

In this style, you can use node the same as struct node.

The syntax error you are facing is, you misused the arrow operator ->, it's used with pointers of struct. For struct, use the dot operator .

So for the function

void addToEnd(llist root, char entery[51])
{
    while(root->next != NULL)
        root = root->next;

You should pass in a pointer:

void addToEnd(llist* root, char entery[51])
Yu Hao
  • 119,891
  • 44
  • 235
  • 294