Create and traverse linked list in c

 Create and traverse linked list in c


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

struct Node

{
    int data;
    struct Node *next; /* data */
};

void printlist(struct Node *n)
{

    while (n != NULL)
    {
        printf("%d\n", n->data);
        n = n->next;
    }
}

int main()
{

    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *thrid = NULL;

    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    thrid = (struct Node *)malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = thrid;

    thrid->data = 3;
    thrid->next = NULL;

    printlist(head);
    }







Post a Comment

0 Comments