Advertisement
saira12tabassum19

Untitled

Nov 11th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct stack{
  5.  
  6. int x;
  7. struct stack *next;
  8. }*top=NULL;
  9.  
  10. void push(int n){
  11.  
  12. struct stack *temp;
  13. temp=(struct stack*)malloc(sizeof(struct stack));
  14. temp->x=n;
  15. if(top==NULL)
  16. {
  17. temp->next=NULL;
  18. top=temp;
  19. }
  20. else{
  21.  
  22. temp->next=top;
  23. top=temp;
  24. }
  25.  
  26. }
  27.  
  28. void pop()
  29. {
  30.  
  31. if(top==NULL)
  32. {
  33. printf("The stack is underflow!!\n");
  34. }
  35. else{
  36.  
  37. top=top->next;
  38.  
  39. }
  40. }
  41. void display()
  42. {
  43. struct stack *c=top;
  44. while(c!=NULL)
  45. {
  46. printf("%d\n",c->x);
  47. c=c->next;
  48. }
  49. printf("\n");
  50.  
  51.  
  52. }
  53. int main()
  54. {
  55. push(11);
  56. display();
  57. push(7);
  58. display();
  59. push(5);
  60. display();
  61. pop();
  62. display();
  63. push(12);
  64. push(9);
  65. display();
  66. pop();
  67. display();
  68. pop();
  69. display();
  70. pop();
  71. display();
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement