Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
628 B
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

// code to add a node in the beginning of a singly linked list
struct node {
    int data;
    struct node *next;
};

struct node *head = NULL;

void addNode(int data) {
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = head;
    head = newNode;
} 

void printList() {
    struct node *temp = head;
    while(temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}

int main() {
    addNode(1);
    addNode(2);
    addNode(3);
    addNode(4);
    addNode(5);
    printList();
    return 0;
}