Advertisement
sanpai

Linked List /insertion and deletion to be done

Sep 4th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct node * addelement(struct node * ,int);
  5. void display(struct node *);
  6.  
  7.  
  8. struct node
  9.  
  10. {
  11.  
  12. int data;
  13. struct node *ptr;
  14. };
  15.  
  16. struct head
  17. {
  18. struct node *head_ptr;
  19. int num;
  20. };
  21.  
  22. int main()
  23. {
  24. struct head a;
  25. int i,data;
  26. a.head_ptr=NULL;
  27. printf("\n Enter the number of elements to be entered to the list :");
  28. scanf("%d",&a.num);
  29. for(i=1;i<=a.num;i++)
  30. {
  31. printf("\n Enter the elements onto the list ");
  32. scanf("%d",&data);
  33. a.head_ptr=addelement(a.head_ptr,data);
  34. }
  35. display(a.head_ptr);
  36. }
  37.  
  38. struct node * addelement(struct node *head,int info)
  39. {
  40.  
  41. struct node *newnode;
  42. newnode=(struct node *)malloc(sizeof(struct node));
  43. newnode->data=info;
  44. if(head==NULL)
  45. {
  46.  
  47.  
  48. newnode->ptr=NULL;
  49. return newnode;
  50. }
  51. else
  52.  
  53. {
  54. newnode->ptr=head;
  55. return newnode;
  56. }
  57. }
  58. display(struct node *head)
  59. {
  60.  
  61. while(head!=NULL)
  62. {
  63. printf("%d\t",head->data);
  64. head=head->ptr;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement