Advertisement
Ruven

Untitled

May 16th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1.  static int sumOf(Node<int> l, int k)
  2.         {
  3.             Node<int> s = l;
  4.             int sum = 0;
  5.             while (k > 0)
  6.             {
  7.                 sum = sum + s.GetInfo();
  8.                 s = s.GetNext();
  9.                 k--;
  10.             }
  11.             return sum;
  12.         }
  13.         static bool haveNext(Node<int> l,int k){
  14.             bool booli = true;
  15.             Node<int> s = l;
  16.             while (k > 0)
  17.             {
  18.                 l = l.GetNext();
  19.                 if (l == null)
  20.                 {
  21.                     booli = false;
  22.                 }
  23.                 k--;
  24.             }
  25.             return booli;
  26.            
  27.         }
  28.         static int lenList(Node<int> l)
  29.         {
  30.             Node<int> temp = l;
  31.             int count = 0;
  32.             while (temp != null)
  33.             {
  34.                 count++;
  35.                 temp = temp.GetNext();
  36.             }
  37.             return count;
  38.         }
  39.         static bool isCyclic(Node<int> l)
  40.         {
  41.             int m = lenList(l);
  42.             Node<int> p = l;
  43.             bool booli = false;
  44.             while (m > 0)
  45.             {
  46.                 if (p.GetInfo() == 1)
  47.                 {
  48.                     booli = true;
  49.                 }
  50.                 p = pos(l, p.GetInfo());
  51.                 Console.WriteLine(p);
  52.                 m--;
  53.             }
  54.             return booli;
  55.         }
  56.         static void nodeToString(Node<int> l)
  57.         {
  58.             Node<int> temp = l;
  59.             while (temp != null)
  60.             {
  61.                 Console.Write(temp.GetInfo() + "->");
  62.                 temp = temp.GetNext();
  63.             }
  64.         }
  65.         static Node<int> pos(Node<int> l, int p)
  66.         {
  67.             Node<int> temp = l;
  68.             int i = 0;
  69.             Node<int> s = l;
  70.             while (temp != null)
  71.             {
  72.                 i++;
  73.                 if (p == i)
  74.                 {
  75.                     s = temp;
  76.                 }
  77.                 temp = temp.GetNext();
  78.                
  79.             }
  80.             return s;
  81.         }
  82.         static int queueLength(Queue<int> q)
  83.         {
  84.             Queue<int> temp = cloneQueue(q);
  85.             int i = 0;
  86.             while (!temp.IsEmpty())
  87.             {
  88.                 i++;
  89.                 temp.Remove();
  90.             }
  91.             return i;
  92.         }
  93.         static bool maagal(Queue<int> q, Queue<int> q1)
  94.         {
  95.             Queue<int> temp = cloneQueue(q);
  96.             int num = queueLength(temp);
  97.             bool booli = false;
  98.             while (num > 0)
  99.             {
  100.                 temp.Insert(temp.Remove());
  101.                 if (isEqual(temp, q1))
  102.                 {
  103.                     booli = true;
  104.                 }
  105.                 num--;
  106.             }
  107.             return booli;
  108.  
  109.         }
  110.         static bool isEqual(Queue<int> q, Queue<int> q1)
  111.         {
  112.             Queue<int> temp = cloneQueue(q);
  113.             Queue<int> temp1 = cloneQueue(q1);
  114.             if (queueLength(temp) != queueLength(temp1))
  115.             {
  116.                 return false;
  117.             }
  118.             int i = queueLength(temp);
  119.             bool booli = true;
  120.             while (!temp.IsEmpty())
  121.             {
  122.                 if(!temp.Head().ToString().Equals(temp1.Head().ToString())){
  123.                     booli = false;
  124.                 }
  125.                 temp.Remove();
  126.                 temp1.Remove();
  127.             }
  128.             return booli;
  129.  
  130.         }
  131.         static Queue<int> cloneQueue(Queue<int> q)
  132.         {
  133.             Queue<int> temp1 = new Queue<int>();
  134.             Queue<int> temp2 = new Queue<int>();
  135.             while (!q.IsEmpty())
  136.             {
  137.                 temp1.Insert(q.Head());
  138.                 temp2.Insert(q.Remove());
  139.             }
  140.             while (!temp2.IsEmpty()) // החזרת התור למצבו המקורי
  141.             {
  142.                 q.Insert(temp2.Remove());
  143.             }
  144.             return temp1;
  145.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement