Advertisement
mahamudulcsedu

singly.linked.list

Jan 22nd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct mylist
  4. {
  5. int data;
  6. struct mylist*next;
  7. };
  8.  
  9. struct mylist*head=NULL;
  10.  
  11. void display()
  12. {
  13. struct mylist*temp=head;
  14. while(temp!=NULL)
  15. {
  16. printf("%d->",temp->data);
  17. temp=temp->next;
  18.  
  19. }
  20. printf("\n");
  21. }
  22.  
  23. void first_insert(int data)
  24. {
  25. struct mylist*temp=(struct mylist*)malloc(sizeof(struct mylist));
  26. temp->data=data;
  27. temp->next=head;
  28. head=temp;
  29. }
  30. void insert_last(int data)
  31. {
  32. struct mylist*temp=head;
  33. struct mylist*new_node=(struct mylist*)malloc(sizeof(struct mylist));
  34. new_node->data=data;
  35. new_node->next=NULL;
  36. if(temp==NULL)
  37. {
  38. head=new_node;
  39. return;
  40. }
  41. while(temp->next!=NULL)
  42. {
  43. temp=temp->next;
  44. }
  45. temp->next=new_node;
  46. return;
  47. }
  48. void middle_insert(int n, int n1)
  49. {
  50. struct mylist*new_node=(struct mylist*) malloc(sizeof(struct mylist*));
  51. struct mylist*temp=head;
  52. new_node->data=n1;
  53. new_node->next=NULL;
  54. int i;
  55. for(i=0;i<(n-2);i++)
  56. {
  57. temp=temp->next;
  58. }
  59. struct mylist*temp1;
  60. temp1=temp->next;
  61. temp->next=new_node;
  62. new_node->next=temp1;
  63. return;
  64. }
  65. void delete_node(int n)
  66. {
  67. struct mylist*temp=head;
  68. if(n==1)
  69. {
  70. head=temp->next;
  71. free(temp);
  72. return;
  73. }
  74. int i;
  75. for(i=0;i<(n-2);i++)
  76. {
  77. temp=temp->next;
  78. }
  79. struct mylist*temp1;
  80. temp1=temp->next;
  81. temp->next=temp1->next;
  82. return;
  83. }
  84. int count_node()
  85. {
  86. struct mylist*temp;
  87. int count=0;
  88. temp=head;
  89. while(temp->next!=NULL)
  90. {
  91. count++;
  92. temp=temp->next;
  93. }
  94. printf("%d node\n",count+1);
  95. }
  96. void create(int data)
  97. {
  98. insert_last(data);
  99. }
  100. void search(int data)
  101. { int count=0;
  102. struct mylist*temp=head;
  103. while(temp!=NULL)
  104. {
  105.     if (temp->data==data)
  106.         count++;
  107. temp=temp->next;
  108. }
  109. printf("%d",count);
  110. }
  111. int main()
  112. {
  113. struct mylist *head;
  114. insert_last(8);
  115. insert_last(5);
  116. first_insert(2);
  117. first_insert(6);
  118. first_insert(3);
  119. first_insert(1);
  120. middle_insert(6,7);
  121. display();
  122. count_node();
  123. delete_node(2);
  124. display();
  125. count_node();
  126. delete_node(4);
  127. display();
  128. count_node();
  129. search(5);
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement