Answers for "linked list in c;"

C
31

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;
}
Posted by: Guest on June-21-2020
1

link list c

#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
    void* data;
    struct Node *next;
} Node;
//push to the front of the list
struct Node* push(struct Node* head, void* data) {
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = data;
    new_node->next = head;
    return new_node;
}
int main(){
    Node* list = NULL;
    //remember to set the list after push
    list = push(list, "!");
    list = push(list, "World ");
    list = push(list, "Hello ");
    //print out the elements
    Node* crnt = list;
    while(crnt != NULL){
        printf("%s", crnt->data);
        crnt = crnt->next;
    }
    return 0;
}
Posted by: Guest on January-27-2022

Code answers related to "C"

Browse Popular Code Answers by Language