linked list c code example

Example 1: how to make a linked list in c

typedef struct node{
    int value; //this is the value the node stores
    struct node *next; //this is the node the current node points to. this is how the nodes link
}node;

node *createNode(int val){
    node *newNode = malloc(sizeof(node));
    newNode->value = val;
    newNode->next = NULL;
    return newNode;
}

Example 2: c linked list

// https://github.com/davidemesso/LinkedListC for the full
// implementation of every basic function

typedef struct Node 
{
  // Void pointer content of this node (to provide multityping).
  void* data;

  // Points to the next Node in the list.   
  struct Node* next;  
} Node;

Node *llNewList(void* data, Node* next)
{
    Node* result = 
        (Node*)malloc(sizeof(Node));

    result->data          = data;
    result->next          = next;
    
    return result;
}

Tags:

Cpp Example