1

I'm trying to create a linear linked list.

Seemed pretty simple but even though the code looks fine it won't compile.

Here's the header file and the main. Could you tell me what the problem is?

#include <malloc.h>
typedef int TYP;

typedef struct
{
    TYP info;
    node_linear_linked_list *next;
} node_linear_linked_list;

void init_linear_linked_list(node_linear_linked_list **manager)
{
    *manager = NULL;
}

void push_linear_linked_list(node_linear_linked_list **manager, TYP info)
{
    node_linear_linked_list *ptr =
    (node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));

    ptr->info = info;
    ptr->next = *manager;
    *manager = ptr;
}

void insert_after_linear_linked_list(node_linear_linked_list *before, TYP info)
{
    node_linear_linked_list *ptr =
    (node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));

    ptr->info = info;
    ptr->next = before->next;
    before->next = ptr;
}

void pop_linear_linked_list(node_linear_linked_list **manager)
{
    node_linear_linked_list *temp_ptr = *manager;
    *manager = temp_ptr->next;
    free(temp_ptr);
}

void delete_after_linear_linked_list(node_linear_linked_list *before)
{
    node_linear_linked_list *temp_ptr = before;
    before->next = before->next->next;
    free(temp_ptr);
}

here's the main:

#include <malloc.h>
#include "node_linear_linked_list.h"
void main(void)
{
    node_linear_linked_list *manager =
    (node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));

    init_node_linear_linked_list(&manager);
    getch();
}

Would appreciate some help. Thanks.

3
  • typedef struct { TYP info; node_linear_linked_list *next; --> typedef struct node { TYP info; struct node *next; Commented Nov 25, 2016 at 18:25
  • 2
    @Maya It is the compiler that reports errors. Read its diagnostic messages. Commented Nov 25, 2016 at 18:25
  • 1
    And don't put functions in header files, this is not how header files are used. Commented Nov 25, 2016 at 18:32

1 Answer 1

2

According to the C Standard

5 Tw o declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types. Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type.

In this declaration

typedef struct
{
    TYP info;
    node_linear_linked_list *next;
} node_linear_linked_list;

there is declared an unnamed structure. In this data member declaration

    node_linear_linked_list *next;

the name node_linear_linked_list is undefined.

You have to write for example

typedef struct node_linear_linked_list
{
    TYP info;
    struct node_linear_linked_list *next;
} node_linear_linked_list;

This function

void delete_after_linear_linked_list(node_linear_linked_list *before)
{
    node_linear_linked_list *temp_ptr = before;
    before->next = before->next->next;
    free(temp_ptr);
}

has a bug. I think you mean

void delete_after_linear_linked_list(node_linear_linked_list *before)
{
    if ( before && before->next )
    {
        node_linear_linked_list *temp_ptr = before->next;
        before->next = before->next->next;
        free(temp_ptr);
    }
}

This statement in main

node_linear_linked_list *manager =
(node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));

results in a memory leak because in the following statement

init_node_linear_linked_list(&manager);

the pointer is reassigned.

Also I advice to do a check in the functions whether a pointer passed as the argument is equal to NULL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.