linklist insert - begin- end -- problem - one element exist with 0

#include <stdio.h> #include <stdlib.h> typedef struct node{ int data; struct node *next; }element; /* ------------ Utility Functions ------------------*/ void display(struct node *head) { while (head!=NULL){ printf(" %d ", head->data); head= head->next; } } void insertelem_start(struct node **ptr, int data){ struct node *temp = (struct node *)malloc(sizeof(struct node)); temp->data= data; temp->next = *ptr; *ptr= temp; } void insertelem_end(struct node**ptr, int data){ struct node *temp = (struct node *)malloc(sizeof(struct node)); struct node *last= *ptr; temp->data= data; temp->next= NULL; if(*ptr == NULL){ *ptr=temp; return; } // traverse the list to the end while (last->next !=NULL) last=last->next; last->next= temp; } /* -------------------- Driver module ----------------*/ int main(){ element *head=NULL; head= (element *)malloc(sizeof(element)); /* check if malloc successed..*/ if (head== NULL){ printf("malloc NOT success.. Exit now !"); exit(-1); } //head->data = 65; printf("MAIN : Head = %p, data = %d\n ", head, head->data); insertelem_end(&head,100); //insertelem_start(&head,10); //insertelem_start(&head,20); /// insertelem_start(&head,30); // insertelem_start(&head,40); //insertelem_start(&head,50); insertelem_end(&head,200); display(head); }

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.