OneCompiler

Single Linked list program

189

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

struct node {
int data;
struct node *link;
};
int main()
{
struct node head = (struct node)malloc(sizeof(struct node));
head->data =45;
head->link =NULL;

struct node*current = (struct node*)malloc(sizeof(struct node));
current->data=98;
current->link = NULL;

head->link = current;// first and second Node are together connected

current = (struct node*)malloc(sizeof(struct node));
current->data =3;
current->link =NULL;

head->link->link =current;// second and third node are together connected

// printing the or traversing the linked list
int count =0;
if(head==NULL)
{
  printf("linked List is empty");
}
struct node *ptr =head;
while(ptr!=NULL)
    {
        count++;
        printf("%d-->",ptr->data);
        ptr=ptr->link;
    }
printf("NULL\n");
printf("Total Node is:%d",count);
return 0;

}