1

I'm using singly linked lists, in C, without the pointer to the node value. So, instead of:

struct _node 
{
  struct _node * next;
  void* value;
} node;

I have

struct _node 
{
   struct _node * next;
} node;

Is there a special name for lists like that?

EDIT:

Just to explain why these lists are useful (I really don't understand why the downvotes):

struct 
{
  node node;
  int i;
} s;

When I do this, I'm forcing struct s to have a node, but without having to set the void* value.

I'm using this scheme to save my tasks information, which will be on a FIFO list.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
rnunes
  • 2,785
  • 7
  • 28
  • 56
  • 2
    Yep, that would be a "Pointless List", as already described. Yay, I have a list of connected... nothings. How useful... – Ed S. Aug 18 '11 at 21:34
  • 4
    Their main strength is that the complexity for inserting nothing in any position is O(1). On the other hand, zero-length arrays are still better, since they require zero time for any insertion of nothing. – Matteo Italia Aug 18 '11 at 21:35
  • Do you mean where _node actually holds other data as well instead of having a pointer to that data? – Mooing Duck Aug 18 '11 at 21:39
  • @Matteo: not true, it's not O(1) if you have to search for the node. if you don't have to search, it's O(1) for normal linked lists too. BTW actually nodes have identity, so for example for any node there is a implicit property: how many nodes are till the tail. – Karoly Horvath Aug 18 '11 at 21:41
  • @Ed: A pointless list would obviously have only one node: `struct node { void *value; } node;` As is, it's clearly a valueless list, not a pointless one. – Jerry Coffin Aug 18 '11 at 21:43
  • @yi_H: correct, but obviously I was joking :) As for the asymptotic notation, my fault for omitting the search time (O(n), but as you specified a search is not always needed). – Matteo Italia Aug 18 '11 at 21:44
  • Seconding @Jerry, this is most certainly not a pointerless list... – James Greenhalgh Aug 18 '11 at 22:20
  • @James: @Ed didn't say "pointerless" - he said "pointless". An address pointing to another address that points to another address that points to another address that points to NULL is pretty pointless to me as well. :) It's about as useful (IMO) as `int level; level = 1; doSomething(); level = 2; doSomethingElse();` without using the value of `level` anywhere else or even testing the value. – Ken White Aug 18 '11 at 22:43
  • 1
    The point of a data structure like this is that the actual data is stored at a fixed offset from the node structure (e.g. as part of the same block obtained by `malloc`), rather than pointed to by the node. Linux uses this principle all over the place in kernelspace and even has nice macros for working with such lists. – R.. GitHub STOP HELPING ICE Aug 19 '11 at 01:24
  • @Ken, apologies, I forgot to add the tags around my comment. Sharth's answer below should tell you why this isn't pointless. – James Greenhalgh Aug 19 '11 at 08:04

4 Answers4

4

I'm not sure that this has a name. Still it's a useful way to build a linked list, because you can put the link with the data but still keep the two separated.

typedef struct _payload
{
    int x;
    double y;
    char * z;
} payload;

struct _node * pNode;
payload * pData;
pNode = malloc(sizeof(struct _node)+sizeof(payload));
pData = (payload *)(((char*)p)+sizeof(struct _node));
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    I'm not sure that the OP really asked for this thing, which is actually a "normal" linked list in disguise... Still, +1 for finding a sensible use for such a thing. – Matteo Italia Aug 18 '11 at 21:49
  • 1
    This technique is used in the [Windows interlocked singly-linked list API](http://msdn.microsoft.com/en-us/library/ms684121.aspx), where the struct is called an [`SLIST_ENTRY`](http://msdn.microsoft.com/en-us/library/ms686309.aspx). – Rob Kennedy Aug 18 '11 at 21:55
  • @Matteo, sometimes you have to read between the lines. I really hope this is what they were asking for. – Mark Ransom Aug 18 '11 at 21:55
  • @Mark: that's why I gave you my +1 :) – Matteo Italia Aug 18 '11 at 21:58
  • @Mark: I would be cautious about using that code because you don't make any checks about alignment of the payload. I would much rather see something like `struct payload { int x; double y; struct _node node; };` and then use some `CONTAINER_OF` macro. – Bill Lynch Aug 18 '11 at 22:09
  • @sharth, since the `node` struct only contains a single pointer it wouldn't mess up alignment in most cases. The only exception that comes to mind is arrays used by SIMD instructions which might have more strict alignment requirements. – Mark Ransom Aug 18 '11 at 22:20
  • @Mark On a 32 bit machine, it would break the alignment of a `uint64_t`. – Bill Lynch Aug 19 '11 at 00:57
4

This is how the linux kernel implements linked lists for example. Check out this link or this link or many others describing their linked list implementation

As to your actual question, I don't believe that there is an actual name to that pattern. It's simply a way of doing a generic linked list in C.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

Are you talking about moving the "list mechanics" to a separate structure:

struct Single_Link;
struct Single_Link
{
    Single_Link * next;
};

From the above structure, you can derive:

struct Double_Link_Node
: public Single_Link_Node
{
    Single_Link * previous;
};

Using the above structure, you can define a generic node:

template <class Value_Type>
struct Node_Contains_Value
: public Single_Link
{
    Value_Type value;
};

Or you could use a generic pointer to a value:

struct Node_Contains_Pointer
: public Single_Link
{
    void * pointer_to_value;
};

The Linked List would maintain a pointer to the first node:

template <class Value_Type>
struct Linked_List
{
    Node_Contains_Value * ptr_to_head_node;
    Node_Contains_Value * ptr_to_last_node;
};

If this is not homework, I highly recommend using the std::list as it has been thorougly tested.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

I think C programmers just refer to this as a "linked list" and consider it the "normal" form of a linked list. It's only in languages that encourage more bloated idioms that having a pointer to your data in list nodes, rather than just storing the node and the data together, is the norm.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711