Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public static Stack<T> CopyStack<T>(Stack<T> s)
  2. {
  3. Stack<T> To = new Stack<T>();
  4. Stack<T> temp = new Stack<T>();
  5. while (s.Count != 0)
  6. {
  7. temp.Push(s.Pop());
  8. }
  9. while (temp.Count!= 0)
  10. {
  11. T val = temp.Pop();
  12. To.Push(val);
  13. s.Push(val);
  14. }
  15. return To;
  16. }
  17.  
  18. public static Queue<T> CopyQueue<T>(Queue<T> q)
  19. {
  20. Queue<T> ToReturn = new Queue<T>();
  21. Queue<T> temp = new Queue<T>();
  22. while (q.Count != 0)
  23. {
  24. temp.Enqueue(q.Dequeue());
  25. }
  26. while (temp.Count!=0)
  27. {
  28. T val = temp.Dequeue();
  29. ToReturn.Enqueue(val);
  30. q.Enqueue(val);
  31. }
  32. return ToReturn;
  33. }
  34.  
  35. public static bool Domot(Stack<int> s1, Stack<int> s2) //targil 8 page 121
  36. {
  37. Stack<int> s1Copy = CopyStack(s1);
  38. Stack<int> s2Copy = CopyStack(s2);
  39.  
  40. while (s2Copy.Count != 0 )
  41. {
  42. if(!IsExist(s1Copy, s2Copy.Pop()))
  43. {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49.  
  50. public static int FindMax (Stack<int> s)
  51. {
  52. Stack<int> temp = CopyStack(s);
  53. int Max = 0;
  54.  
  55. while (temp.Count != 0)
  56. {
  57. if (Max < temp.Pop())
  58. {
  59. Max = temp.Pop();
  60. }
  61. }
  62. return Max;
  63. } //find max in stack
  64.  
  65.  
  66. public static void PrintStack<T>(Stack<T> S)
  67. {
  68. Stack<T> Copy = CopyStack<T>(S);
  69. while (Copy.Count != 0)
  70. {
  71. Console.Write(Copy.Pop());
  72. }
  73. Console.WriteLine();
  74.  
  75. } //prints a stack
  76.  
  77. public static void PrintQueue<T>(Queue<T> q)
  78. {
  79. Queue<T> copy = CopyQueue(q);
  80. while(copy.Count != 0)
  81. {
  82. Console.WriteLine(copy.Dequeue());
  83. }
  84. Console.WriteLine();
  85. }
  86.  
  87. public static Stack<T>[] SplitStack<T>(Stack<T> s, int k)
  88. {
  89. Stack<T> help = new Stack<T>();
  90. for (int i = 0; i < k && s.Count!=0; i++)
  91. {
  92. help.Push(s.Pop());
  93. }
  94.  
  95. Stack<T>[] arr = new Stack<T>[2];
  96. arr[0] = help;
  97. arr[1] = s;
  98.  
  99. return arr;
  100. }
  101.  
  102. public static Stack<T> ReverseStack<T>(Stack<T> s)
  103. {
  104. Stack<T> help = CopyStack(s);
  105. Stack<T> Return = new Stack<T>();
  106. while (help.Count!=0)
  107. {
  108. Return.Push(help.Pop());
  109. }
  110. return Return;
  111. }
  112.  
  113. public static Stack<int> ReverseFirstK(Stack<int> s, int k)
  114. {
  115. if (k < 0 || k > s.Count)
  116. {
  117. return s;
  118. }
  119.  
  120. Stack<int>[] SplittedStacks = SplitStack(s, k);
  121. Stack<int> temp = SplittedStacks[0];
  122. Stack<int> Mekori = SplittedStacks[1];
  123. temp = ReverseStack(temp);
  124.  
  125. int i = k;
  126.  
  127.  
  128.  
  129. while (i != 0)
  130. {
  131. Mekori.Push(temp.Pop());
  132. i--;
  133. }
  134.  
  135.  
  136. return Mekori;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement