Thursday, 11 October 2018

C Program to Insert Node at The End of Single Linked List

Single Linked List: - Simply a list is a sequence of data, and the linked list is a sequence of data linked with each other. 

The formal definition of a single linked list is as follows.

The single linked list is a sequence of elements in which every element has link to its next element in the sequence.

In any single linked list, the individual element is called as "Node". Every "Node" contains two fields, data and next. The data field is used to store the actual value of that node and the next field is used to store the address of the next node in the sequence.



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. struct node
  5. {
  6.  int data;
  7.  struct node *next;
  8. };
  9. struct node *start;
  10. void creat_node(int);
  11. void creat_nodee(int);
  12. void print();
  13. int main()
  14. {
  15. int data,i,n,neww;
  16. printf("Plese Enter the Length of the Linked List:\n");
  17. scanf("%d",&n);
  18. for(i=0;i<n;i++)
  19. {
  20. printf("Please Enter A Value:\n");
  21. scanf("%d",&data);
  22. creat_node(data);
  23. }
  24. print();
  25. printf("Please Enter A Value to be Insert at the End of the Linked LIST:\n");
  26. scanf("%d",&neww);
  27. creat_nodee(neww);
  28. print();
  29. }
  30. void creat_node(int x)
  31. {
  32. struct node *t,*t1;
  33. t=(struct node*)malloc(sizeof(struct node));
  34. t->data=x;
  35. t->next=NULL;
  36. if(start==NULL)
  37. {
  38. start=t;
  39. }
  40. else
  41. {
  42. t1=start;
  43. while(t1->next!=NULL)
  44. {
  45. t1=t1->next;
  46. }
  47. t1->next=t;
  48. }
  49. }
  50. void creat_nodee(int x)
  51. {
  52. struct node *t,*t1;
  53. t=(struct node*)malloc(sizeof(struct node));
  54. t->data=x;
  55. t->next=NULL;
  56. t1=start;
  57. while(t1->next!=NULL)
  58. {
  59. t1=t1->next;
  60. }
  61. t1->next=t;
  62. }
  63. void print()
  64. {
  65. struct node *t;
  66. t=start;
  67. while(t->next!=NULL)
  68. {
  69. printf("Your Entered DATA was: %d \n",t->data);
  70. t=t->next;
  71. }
  72. printf("Your Entered DATA was (END): %d \n",t->data);
  73. }

No comments:

Post a Comment