-3

Is it possible to insert an unsigned long into a linked list ordered from smallest to largest without dynamic memory, using malloc or free?

1 Answers1

4

You can pre-allocate a bunch of list entries in the form of an array, then pick entries from that array when you do an insert. This is of course only possible if the array is either global, or at otherwise kept in scope for the duration of the insertion operation.

Something like:

struct integerNode {
  int                value;
  struct integerNode *next;
};

struct integerNode nodes[100]; /* adjust for your number of integers */

The next step would be to use a trivial for loop to link the items in nodes together, forming a linked list of free items. Then write a function that de-links a node from the free list and links it into another list, while inserting the number.

unwind
  • 391,730
  • 64
  • 469
  • 606