hi everyone I'm working currently with linked list , and there's a problem with my code , the link is writing the same place every time (it replaces the previous values with the new ones without inserting a new one ) can anyone help ? Thanks in advance

#include<stdio.h>
#include<unistd.h>

struct mem_chunk 
{
unsigned   *mem_break ;    
int size ;          
int  required_size; 
struct mem_chunk * next;

};

struct mem_chunk  *used_chunks , *unused_chunks;

void insert( struct mem_chunk new_node , int flag)
{

    if ( flag ==0 )
    {

        if (used_chunks == NULL )
        {

            used_chunks = &new_node; 

        }

        else {

            struct mem_chunk *current ; 
            current = used_chunks;
            current=current->next;
            current=&new_node;

             }
        }   
}

The first parameter to that function needs to be a pointer so that it can be added to the list. What you are doing now is just setting a pointer to a local variable, which goes out of scope as soon as that function returns. You should ALWAYS pass structures by reference (pointers), never by value.
That function might look like this:

void insert( struct mem_chunkI* new_node , int flag)
{
    if( used_chucks == NULL)
       used_chuncks = new_node;
}

In the else statement, if you want to add the new node to the head of the linked list

new_node->next = used_chunck;
used_chunck = new_node;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.