GreekMustard
GreekMustard

Reputation: 9

Creating Node Linear Linked List

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.

Upvotes: 1

Views: 41

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

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.

Upvotes: 2

Related Questions