TEXT   44
slstack
Guest on 23rd November 2022 06:22:23 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.  
  23. if(sp==(struct node *)0)
  24. {
  25. *status=0;
  26. printf("empty stack");
  27. }
  28. else
  29. {
  30. item=sp->data;
  31. t=sp;
  32. sp=sp->next;
  33. free(t);
  34. *status=1;
  35. return item;
  36. }
  37. }
  38. int search(int item)
  39. {
  40. struct node *t=sp;
  41. int count=0;
  42. while(t!=(struct node *)0)
  43. {
  44. ++count;
  45. if(t->data==item)
  46. return count;
  47. t=t->next;
  48. }
  49. return 0;
  50. }
  51. void main()
  52. {
  53. int item,opt,ans,status;
  54. do
  55.  
  56. {
  57. printf("\n 1.push:\n");
  58. printf("\n 2.pop:\n");
  59. printf("\n 3.search:\n");
  60. printf("\n 4.exit:\n");
  61. printf("\n Enter the option:");
  62. scanf("%d",&opt);
  63. switch(opt)
  64. {
  65. case 1:printf(" Enter the data:");
  66. scanf("%d",&item);
  67. push(item);
  68. break;
  69. case 2:item=pop(&status);
  70. if(status!=0)
  71. printf("Deleted=%d",item);
  72. break;
  73. case 3:printf("Enter the number to search:");
  74. scanf("%d",&item);
  75. ans=search(item);
  76. if(ans==0)
  77. printf(" Item not found");
  78. else
  79.  printf("found at %d Position",ans);
  80. break;
  81. case 4:exit(0);
  82. }
  83. }
  84. while(1);
  85. }

Raw Paste

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