Thursday, 15 November 2018

Write down a C Program to insert in beginning & end, delete a node in beginning & end, Traverse for the singly linked list and also modify a specified node for the Singly Linked List using Global start variable & call by reference & by using the Switch Case

Write down a C Program to insert in beginning & end, delete a node in beginning & end, Traverse for the Singly Linked List and also modify a specified node for the Singly Linked List using Global start variable & call by reference & by using the Switch Case.

  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=NULL;
  10. void create_nodebegining(int);
  11. void create_nodeend(int);
  12. void delete_nodebegining();
  13. void delete_nodeend();
  14. void modify(int,int);
  15. void traverse();
  16. int main()
  17. {
  18. int data,i,n,ch,data1;
  19. printf("\n Enter The Value of data to be Entered:\n");
  20. scanf("%d",&n);
  21. for(i=0;i<n;i++)
  22. {
  23. printf("\n Enter The Value Should be Enter In Data of Node:\n");
  24. scanf("%d",&data);
  25. create_nodeend(data);
  26. }
  27. printf("\n Please Enter Your Choise \n ");
  28. printf("\n Press 1 for Insert in Beginning \n Press 2 for Insert in end \n Press 3 Delete in Beginning \n Press 4 for delete in end \n Press 5 for modify \n press 6 for traverse \n");
  29. scanf("%d",&ch);
  30. switch(ch)
  31. {
  32. case 1:
  33. printf("\n Enter The Value For Node Data:\n");
  34. scanf("%d",&data);
  35. create_nodebegining(data);
  36. break;
  37. case 2:
  38. printf("\n Enter The Value For Node Data:\n");
  39. scanf("%d",&data);
  40. create_nodeend(data);
  41. break;
  42. case 3:
  43. delete_nodebegining();
  44. break;
  45. case 4:
  46. delete_nodeend();
  47. break;
  48. case 5:
  49. printf("\n Enter The Data To be Modified:\n");
  50. scanf("%d",&data1);
  51. printf("\n Enter The Data Modified:\n");
  52. scanf("%d",&data);
  53. modify(data1,data);
  54. break;
  55. case 6:
  56. traverse();
  57. break;
  58. }
  59. traverse();
  60. }
  61. void create_nodeend(int x)
  62. {
  63. struct node *t,*t1;
  64. t=(struct node*)malloc(sizeof(struct node));
  65. t->data=x;
  66. t->next=NULL;
  67. if(start==NULL)
  68. {
  69. start=t;
  70. }
  71. else
  72. {
  73. t1=start;
  74. while(t1->next!=NULL)
  75. {
  76. t1=t1->next;
  77. }
  78. t1->next=t;
  79. }
  80. }
  81. void create_nodebegining(int x)
  82. {
  83. struct node *t,*t1;
  84. t=(struct node*)malloc(sizeof(struct node));
  85. t1=start;
  86. t->data=x;
  87. t->next=t1;
  88. start=t;
  89. traverse();
  90. }
  91. void delete_nodeend()
  92. {
  93. int d;
  94. struct node *t,*t1;
  95. t=start;
  96. while(t->next!=NULL)
  97. {
  98. t1=t;
  99. t=t->next;
  100. }
  101. d=t->data;
  102. free(t);
  103. t1->next=NULL;
  104. traverse();
  105. }
  106. void delete_nodebegining()
  107. {
  108. int d;
  109. struct node *t,*t1;
  110. t=start->next;
  111. d=start->data;
  112. free(start);
  113. start=t;
  114. traverse();
  115. }
  116. void modify(int x,int y)
  117. {
  118. struct node *t;
  119. t=start;
  120. while(t->data!=x)
  121. {
  122. t=t->next;
  123. }
  124. t->data=y;
  125. traverse();
  126. }
  127. void traverse()
  128. {
  129. struct node *t;
  130. t=start;
  131. while(t->next!=NULL)
  132. {
  133. printf("\n %d",t->data);
  134. t=t->next;
  135. }
  136. printf("\n %d\n\n",t->data);
  137. }



No comments:

Post a Comment