Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Stack<T> CopyStack<T>(Stack<T> s)
- {
- Stack<T> To = new Stack<T>();
- Stack<T> temp = new Stack<T>();
- while (s.Count != 0)
- {
- temp.Push(s.Pop());
- }
- while (temp.Count!= 0)
- {
- T val = temp.Pop();
- To.Push(val);
- s.Push(val);
- }
- return To;
- }
- public static Queue<T> CopyQueue<T>(Queue<T> q)
- {
- Queue<T> ToReturn = new Queue<T>();
- Queue<T> temp = new Queue<T>();
- while (q.Count != 0)
- {
- temp.Enqueue(q.Dequeue());
- }
- while (temp.Count!=0)
- {
- T val = temp.Dequeue();
- ToReturn.Enqueue(val);
- q.Enqueue(val);
- }
- return ToReturn;
- }
- public static bool Domot(Stack<int> s1, Stack<int> s2) //targil 8 page 121
- {
- Stack<int> s1Copy = CopyStack(s1);
- Stack<int> s2Copy = CopyStack(s2);
- while (s2Copy.Count != 0 )
- {
- if(!IsExist(s1Copy, s2Copy.Pop()))
- {
- return false;
- }
- }
- return true;
- }
- public static int FindMax (Stack<int> s)
- {
- Stack<int> temp = CopyStack(s);
- int Max = 0;
- while (temp.Count != 0)
- {
- if (Max < temp.Pop())
- {
- Max = temp.Pop();
- }
- }
- return Max;
- } //find max in stack
- public static void PrintStack<T>(Stack<T> S)
- {
- Stack<T> Copy = CopyStack<T>(S);
- while (Copy.Count != 0)
- {
- Console.Write(Copy.Pop());
- }
- Console.WriteLine();
- } //prints a stack
- public static void PrintQueue<T>(Queue<T> q)
- {
- Queue<T> copy = CopyQueue(q);
- while(copy.Count != 0)
- {
- Console.WriteLine(copy.Dequeue());
- }
- Console.WriteLine();
- }
- public static Stack<T>[] SplitStack<T>(Stack<T> s, int k)
- {
- Stack<T> help = new Stack<T>();
- for (int i = 0; i < k && s.Count!=0; i++)
- {
- help.Push(s.Pop());
- }
- Stack<T>[] arr = new Stack<T>[2];
- arr[0] = help;
- arr[1] = s;
- return arr;
- }
- public static Stack<T> ReverseStack<T>(Stack<T> s)
- {
- Stack<T> help = CopyStack(s);
- Stack<T> Return = new Stack<T>();
- while (help.Count!=0)
- {
- Return.Push(help.Pop());
- }
- return Return;
- }
- public static Stack<int> ReverseFirstK(Stack<int> s, int k)
- {
- if (k < 0 || k > s.Count)
- {
- return s;
- }
- Stack<int>[] SplittedStacks = SplitStack(s, k);
- Stack<int> temp = SplittedStacks[0];
- Stack<int> Mekori = SplittedStacks[1];
- temp = ReverseStack(temp);
- int i = k;
- while (i != 0)
- {
- Mekori.Push(temp.Pop());
- i--;
- }
- return Mekori;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement