Advertisement
AvengersAssemble

P1

Nov 2nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Unit4;
  5. using Unit4.CollectionsLib;
  6.  
  7. namespace Lab4
  8. {
  9.     class Program
  10.     {
  11.         //Q1
  12.         private static Random rnd = new Random();
  13.         public static Node<int> RandomNode()
  14.         {
  15.             int length, next;
  16.             length = rnd.Next(5, 11);
  17.             next = rnd.Next(10, 100);
  18.             Node<int> n = new Node<int>(next);
  19.             Node<int> n2;
  20.             for (int i = 1; i < length; i++)
  21.             {
  22.                 next = rnd.Next(10, 100);
  23.                 n2 = new Node<int>(next);
  24.                 n2.SetNext(n);
  25.                 n = n2;
  26.             }
  27.             return n;
  28.         }
  29.  
  30.         //Q2
  31.         public static Stack<Node<int>> NodeStack()
  32.         {
  33.             Stack<Node<int>> stk = new Stack<Node<int>>();
  34.             for (int k = 0; k < 10; k++)
  35.             {
  36.                 stk.Push(RandomNode());
  37.             }
  38.             return stk;
  39.         }
  40.         public static void PrintNodeStack(Stack<Node<int>> stk)
  41.         {
  42.             Stack<Node<int>> forreturn = new Stack<Node<int>>();
  43.             Node<int> temp;
  44.             while (stk.IsEmpty() == false)
  45.             {
  46.                 temp = stk.Pop();
  47.                 forreturn.Push(temp);
  48.                 while (temp != null)
  49.                 {
  50.                     if (temp.GetNext() == null) // אם זה האחרון הוא לא מדפיס "," בסוף
  51.                     {
  52.                         Console.Write(temp.ToString());
  53.                     }
  54.                     else
  55.                     {
  56.  
  57.                         Console.Write(temp.ToString() + ",");
  58.                     }
  59.                     temp = temp.GetNext();
  60.                 }
  61.  
  62.                 Console.WriteLine();
  63.             }
  64.  
  65.             while (forreturn.IsEmpty() == false)
  66.             {
  67.                 stk.Push(forreturn.Pop());
  68.             }
  69.         }
  70.  
  71.         //Q3
  72.         public static Node<int> LongestNode(Stack<Node<int>> s)
  73.         {
  74.             int count, count2 = 0;
  75.             Node<int> temp;
  76.             Node<int> longest = null;
  77.             while (!s.IsEmpty())
  78.             {
  79.                 count = 0;
  80.                 temp = s.Pop();
  81.                 if (longest == null)
  82.                     longest = temp;
  83.                 while (temp != null)
  84.                 {
  85.                     count++;
  86.                     temp = temp.GetNext();
  87.                 }
  88.                 if (count > count2)
  89.                 {
  90.                     longest = temp;
  91.                     count2 = count;
  92.                 }
  93.  
  94.             }
  95.             return longest;
  96.         }
  97.  
  98.         //Q5
  99.         public static void DeleteShortest(Stack<Node<int>> s)
  100.         {
  101.             int count, countMin = int.MaxValue;
  102.             Stack<Node<int>> temp = s;
  103.             Node<int> min = new Node<int>(0);
  104.             while (!temp.IsEmpty())
  105.             {
  106.                 Node<int> tempN = temp.Pop();
  107.                 count = 0;
  108.                 while (temp != null)
  109.                 {
  110.                     count++;
  111.                     tempN = tempN.GetNext();
  112.                 }
  113.                 if (count < countMin)
  114.                 {
  115.                     min = tempN;
  116.                     countMin = count;
  117.                 }
  118.             }
  119.  
  120.         }
  121.  
  122.         //Q7
  123.         public static bool IsFound(Stack<Flower> s, string name)
  124.         {
  125.             while (!s.IsEmpty())
  126.                 if (s.Pop().Name == name)
  127.                     return true;
  128.             return false;
  129.         }
  130.  
  131.         static void Main(string[] args)
  132.         {
  133.             Node<int> n = RandomNode();
  134.             Stack<Node<int>> stk = NodeStack();
  135.             PrintNodeStack(stk);
  136.             Console.WriteLine(LongestNode(stk));
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement