Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Queues
- {
- class Utils
- {
- public static Queue<T> Copy<T>(Queue<T> q)
- {
- Queue<T> copy = new Queue<T>();
- Queue<T> temp = new Queue<T>();
- while (!q.IsEmpty())
- {
- T val = q.Remove();
- copy.Insert(val);
- temp.Insert(val);
- }
- while (!temp.IsEmpty())
- {
- q.Insert(temp.Remove());
- }
- return copy;
- }
- public static void PushQueue<T>(Queue<T> from, Queue<T> to)
- {
- while (!from.IsEmpty())
- to.Insert(from.Remove());
- }
- public static Node<T> QueueToNode<T>(Queue<T> q)
- {
- if (q.IsEmpty())
- return null;
- Queue<T> clone = Copy(q);
- Node<T> head = null,
- previous = null;
- while (!clone.IsEmpty())
- {
- Node<T> node = new Node<T>(clone.Remove());
- if (head == null)
- {
- head = node;
- previous = head;
- }
- else
- {
- previous.SetNext(node);
- previous = previous.GetNext();
- }
- }
- return head;
- }
- public static bool IsContained<T>(Queue<T> q1, Queue<T> q2)
- {
- Queue<T> cloneQ2 = Copy(q2);
- Node<T> nodeQ1 = QueueToNode(q1);
- Node<T> current = nodeQ1;
- while (!cloneQ2.IsEmpty() && current != null)
- {
- // if current element is the same
- if (cloneQ2.Remove().Equals(current))
- // move to check next element
- current = current.GetNext();
- // if current element doesn't match anticipated element, start over
- else
- current = nodeQ1;
- }
- return current == null;
- }
- public static int QueueSize<T>(Queue<T> q)
- {
- int size = 0;
- Queue<T> clone = Copy(q);
- while (!clone.IsEmpty())
- {
- size++;
- clone.Remove();
- }
- return size;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement