TEXT   63
ls
Guest on 24th November 2022 07:06:54 AM


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. struct node *sp=(struct node *)0;
  9. void push(int item)
  10. {
  11. struct node *t;
  12. t=(struct node *)malloc(sizeof (struct node));
  13. t->data=item;
  14. t->next=sp;
  15. sp=t;
  16. return;
  17. }
  18. int pop(int *status)
  19. {
  20. struct node *t;
  21. int item;
  22. if(sp==(struct node *)0)
  23. {
  24. printf("empty stack");
  25. *status=0;
  26. }
  27. else
  28. {
  29. item=sp->data;
  30. t=sp;
  31. sp=sp->next;
  32. free(t);
  33. *status=1;
  34. return item;
  35. }
  36. }
  37. int search(int item)
  38. {
  39. struct node *t=sp;
  40. int count=0;
  41. while(t!=(struct node *)0)
  42. {
  43. ++count;
  44. if(t->data==item)
  45. return count;
  46. else
  47. t=t->next;
  48. }
  49. return 0;
  50. }
  51. int main()
  52. {
  53. int item,opt,ans,status;
  54. do
  55. {
  56. printf("\n1.push");
  57. printf("\n2.pop");
  58. printf("\n3.search");
  59. printf("\n4.exit");
  60. printf("\nenter your option:");
  61. scanf("%d",&opt);
  62. switch(opt)
  63. {
  64. case 1:printf("\ndata:");
  65. scanf("%d",&item);
  66. push(item);
  67. printf("\ninterted %d",item);
  68. break;
  69. case 2:item=pop(&status);
  70. if(status==1)
  71. printf("\ndeleted %d",item);
  72. break;
  73. case 3:printf("\ndata to search:");
  74. scanf("%d",&item);
  75. ans=search(item);
  76. if(ans==0)
  77. printf("\ndata is not found");
  78. else
  79. printf("\ndata is found at %d node",ans);
  80. break;
  81. case 4:exit(0);
  82. break;
  83. }
  84. }
  85. while(1);
  86. }

Raw Paste

Login or Register to edit or fork this paste. It's free.