Advertisement
eyafi

LinkedList

Oct 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. struct node *head;
  9. void insert(int data,int n)
  10. {
  11. struct node *temp1=(struct node *)malloc(sizeof(struct node));
  12. temp1->data=data;
  13. temp1->next=NULL;
  14. if(n==1)
  15. {
  16. temp1->next=head;
  17. head=temp1;
  18. return;
  19. }
  20. struct node *temp2=head;
  21. int i;
  22. for(i=0;i<n-2;i++)
  23. {
  24. temp2=temp2->next;
  25. }
  26. temp1->next=temp2->next;
  27. temp2->next=temp1;
  28. }
  29. void print()
  30. {
  31. struct node *s=head;
  32. while(s!=NULL)
  33. {
  34. printf("%d\n",s->data);
  35. s=s->next;
  36. }
  37. }
  38.  
  39. void search(int n)
  40. {
  41. struct node *s=head;
  42. int c=0;
  43. while(s!=NULL)
  44. {
  45. if(s->data==n) c++;
  46.  
  47. s=s->next;
  48. }
  49. printf("%d found %d times\n",n,c);
  50. }
  51. int count()
  52. {
  53. struct node *s=head;
  54. int c=0;
  55. while(s!=NULL)
  56. {
  57. c++;
  58. s=s->next;
  59. }
  60. return c;
  61. }
  62. void dlnode(int n)
  63. {
  64. struct node *temp=head;
  65. if(count()<n)
  66. {
  67. printf("No more node than %d",count());
  68. }
  69. if(n==1)
  70. {
  71. head=temp->next;
  72. free(temp);
  73. printf("Node %d deleted\n",n);
  74. return;
  75. }
  76. int i;
  77. for(i=0;i<n-2;i++)
  78. {
  79. temp=temp->next;
  80. }
  81. struct node *temp2;
  82. temp2=temp->next;
  83. temp->next=temp2->next;
  84. free(temp2);
  85. printf("Node %d deleted\n",n);
  86. return;
  87. }
  88. int main()
  89. {
  90. head = NULL;
  91. insert(5,1);
  92. insert(10,2);
  93. insert(15,3);
  94. insert(20,4);
  95. insert(25,5);
  96. print();
  97. int n=count();
  98. printf("%d items\n",n);
  99. int x;
  100. printf("Which node you want to search?\nReplay:");
  101. scanf("%d",&x);
  102. search(x);
  103. int z;
  104. printf("Which node you want to delete?\nReplay:");
  105. scanf("%d",&z);
  106. dlnode(z);
  107. print();
  108. int m=count();
  109. printf("%d items \n",m);
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement