Linked List in C

Discussion in 'C' started by priyankpandya3, May 8, 2015.

  1. priyankpandya3

    priyankpandya3 New Member

    Joined:
    May 8, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Is it possible to insert an unsigned long into a linked list ordered from smallest to largest Without using malloc or free.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The use of malloc and free is dependent on how you implement your linked list and not on the operations like insert / delete.
     
  3. priyankpandya3

    priyankpandya3 New Member

    Joined:
    May 8, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I have used malloc and free in my code. I want to know if I could avoid using them in my inserting function
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    No you cannot because if you use malloc for each node, you will need to use malloc before you can assign data to it.

    If you would have done such that you malloc few nodes in one go, you can avoid malloc and free before each operation.
     
  5. todd12345

    todd12345 New Member

    Joined:
    Sep 30, 2015
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. Specifically, the programmer writes a struct definition that contains variables holding information about something and that has a pointer to a struct of its same type (it has to be a pointer--otherwise, every time an element was created, it would create a new element, infinitely). Each of these individual structs or classes in the list is commonly known as a node or element of the list.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The only way to avoid using malloc during insertion of a new element in the array is to use an entry that was preallocated in some way. However that only means the memory was allocated at some other time. When inserting into a linked list you usually create a new entry, which means allocating memory. So to insert without using malloc is a contradiction of terms.

    If you want to avoid memory fragmentation caused by repeatedly lengthening and shortening the list then you could only allocate new nodes and never free them, and mark each node as either used or unused (and further, to allocate a batch of new nodes at a time). With at least one unused node in the list you could then insert without allocating.

    Could you explain what problem you are trying to solve? Why do you want to insert without calling malloc?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice