Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct c{
  6.  
  7.  
  8. int d;
  9.  
  10. struct c *p;
  11. };
  12. struct c *top = NULL;
  13.  
  14. void push(int a)
  15.  
  16. {
  17.  
  18. struct c *n;
  19.  
  20. n=(c*) malloc (sizeof(c));
  21.  
  22. n->d=a;
  23.  
  24. n->p =top;
  25.  
  26. top=n;
  27.  
  28. //printf("%d\n",n->d);
  29. }
  30.  
  31. void display () {
  32.  
  33. struct c *temp;
  34.  
  35. temp=top;
  36.  
  37. if(top==0){
  38.  
  39. printf("Stack is full\n");
  40.  
  41. }
  42. else {
  43.  
  44. while(temp!=0){
  45.  
  46. printf("%d\n",temp->d);
  47.  
  48. temp=temp->p;
  49.  
  50. }
  51. }
  52. }
  53. void peek()
  54.  
  55. {
  56.  
  57. if(top==0)
  58.  
  59. {
  60.  
  61. printf("Stack is full\n");
  62.  
  63. }
  64. else{
  65.  
  66. printf("\nfirst data is %d\n",top->d);
  67.  
  68. }
  69. }
  70. void pop(){
  71.  
  72. struct c *t;
  73.  
  74. t=top;
  75.  
  76. if(top==0)
  77.  
  78. {
  79. printf("Stack is FUll\n");
  80. }
  81. else{
  82. //printf("deleted number %d\n",top->d);
  83.  
  84. top=top->p;
  85.  
  86. free(t);
  87. }
  88. }
  89.  
  90.  
  91.  
  92. int main()
  93. {
  94. //push(3);
  95. //push(5);
  96. // push(10);
  97. printf("How Many number you want: ");
  98.  
  99. int n,i;
  100.  
  101. scanf("%d",&n);
  102.  
  103. for(i=1;i<=n;i++)
  104.  
  105. {
  106.  
  107. push(i);
  108.  
  109. }
  110.  
  111. display();
  112.  
  113. peek();
  114.  
  115. int k;
  116.  
  117. printf("Enter how many number you want to delete : ");
  118.  
  119. scanf("%d",&k);
  120.  
  121. for(i=1;i<=k;i++)
  122.  
  123. {
  124.  
  125. pop();
  126.  
  127. }
  128.  
  129. peek();
  130.  
  131. display();
  132.  
  133. return 0;
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement