Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Stack stk = new Stack(100);
  14.  
  15. int rounds = Convert.ToInt32(Console.ReadLine());
  16.  
  17. for (int i = 1; i <= rounds; i++)
  18. {
  19. string[] str = Console.ReadLine().Split(' ');
  20. switch (str[0])
  21. {
  22. case "push":
  23. stk.push(Convert.ToInt32(str[1]));
  24. Console.WriteLine("stack pushed key : " + str[1]);
  25. break;
  26. case "pop":
  27. if (!stk.IsEmpty())
  28. {
  29. Console.WriteLine(stk.pop());
  30. }
  31. else
  32. {
  33. Console.WriteLine("-1");
  34. }
  35.  
  36. break;
  37. case "size":
  38. Console.WriteLine(stk.Size());
  39. break;
  40. case "top":
  41.  
  42. if (!stk.IsEmpty())
  43. {
  44. Console.WriteLine(stk.peek());
  45. }
  46. else
  47. {
  48. Console.WriteLine("-1");
  49. }
  50.  
  51. break;
  52. case "empty":
  53.  
  54. if (!stk.IsEmpty())
  55. {
  56. Console.WriteLine("0");
  57. }
  58. else
  59. {
  60. Console.WriteLine("1");
  61. }
  62.  
  63. break;
  64. }
  65. }
  66. }
  67. }
  68.  
  69. class Stack
  70. {
  71. int top, size;
  72. int[] stack;
  73.  
  74. public Stack(int size)
  75. {
  76. top = 0;
  77. stack = new int[size];
  78. this.size = size;
  79. }
  80.  
  81. public int peek()
  82. {
  83. return stack[top];
  84. }
  85.  
  86. public void push(int value)
  87. {
  88. stack[++top] = value;
  89. }
  90.  
  91. public int pop()
  92. {
  93. return stack[top--];
  94. }
  95.  
  96. public bool IsEmpty()
  97. {
  98. if(top > 0)
  99. {
  100. return false;
  101. }
  102. else
  103. {
  104. return true;
  105. }
  106. }
  107.  
  108. public int Size()
  109. {
  110. return top;
  111. }
  112.  
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement