single link list store and display

#include <stdio.h> #include <stdlib.h> /*------------------- Struct Declaration -------- */ typedef struct node{ int data; struct node *next; }element; /* ------------ Utility Functions ------------------*/ /*void printadd(struct node *head){ printf("Head address = %p, data = %d\n", head, head->data); }*/ struct node * insertelem(struct node *ptr, int data ){ ptr->data= data; ptr->next=(struct node *)malloc(sizeof(struct node)); // printadd(ptr); return ptr->next; } void display(struct node *head){ while (head->next!=NULL) { printf(" %d ",head->data); head=head->next; } } /* -------------------- Driver module ----------------*/ int main(){ struct node *head=NULL; struct node *consthead=NULL; head= (struct node *)malloc(sizeof(struct node)); //Check for malloc fail if(head==NULL){ printf("malloc failed.. Exit now .. !!"); exit(-1); } //hold the address of first node permanently. Dont loose it consthead=head; head=insertelem(head,10); head=insertelem(head,20); head=insertelem(head,30); head=insertelem(head,40); head=insertelem(head,50); display(consthead); }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.