Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using Unit4;
- using Unit4.CollectionsLib;
- namespace Lab4
- {
- class Program
- {
- //Q1
- private static Random rnd = new Random();
- public static Node<int> RandomNode()
- {
- int length, next;
- length = rnd.Next(5, 11);
- next = rnd.Next(10, 100);
- Node<int> n = new Node<int>(next);
- Node<int> n2;
- for (int i = 1; i < length; i++)
- {
- next = rnd.Next(10, 100);
- n2 = new Node<int>(next);
- n2.SetNext(n);
- n = n2;
- }
- return n;
- }
- //Q2
- public static Stack<Node<int>> NodeStack()
- {
- Stack<Node<int>> stk = new Stack<Node<int>>();
- for (int k = 0; k < 10; k++)
- {
- stk.Push(RandomNode());
- }
- return stk;
- }
- public static void PrintNodeStack(Stack<Node<int>> stk)
- {
- Stack<Node<int>> forreturn = new Stack<Node<int>>();
- Node<int> temp;
- while (stk.IsEmpty() == false)
- {
- temp = stk.Pop();
- forreturn.Push(temp);
- while (temp != null)
- {
- if (temp.GetNext() == null) // אם זה האחרון הוא לא מדפיס "," בסוף
- {
- Console.Write(temp.ToString());
- }
- else
- {
- Console.Write(temp.ToString() + ",");
- }
- temp = temp.GetNext();
- }
- Console.WriteLine();
- }
- while (forreturn.IsEmpty() == false)
- {
- stk.Push(forreturn.Pop());
- }
- }
- //Q3
- public static Node<int> LongestNode(Stack<Node<int>> s)
- {
- int count, count2 = 0;
- Node<int> temp;
- Node<int> longest = null;
- while (!s.IsEmpty())
- {
- count = 0;
- temp = s.Pop();
- if (longest == null)
- longest = temp;
- while (temp != null)
- {
- count++;
- temp = temp.GetNext();
- }
- if (count > count2)
- {
- longest = temp;
- count2 = count;
- }
- }
- return longest;
- }
- //Q5
- public static void DeleteShortest(Stack<Node<int>> s)
- {
- int count, countMin = int.MaxValue;
- Stack<Node<int>> temp = s;
- Node<int> min = new Node<int>(0);
- while (!temp.IsEmpty())
- {
- Node<int> tempN = temp.Pop();
- count = 0;
- while (temp != null)
- {
- count++;
- tempN = tempN.GetNext();
- }
- if (count < countMin)
- {
- min = tempN;
- countMin = count;
- }
- }
- }
- //Q7
- public static bool IsFound(Stack<Flower> s, string name)
- {
- while (!s.IsEmpty())
- if (s.Pop().Name == name)
- return true;
- return false;
- }
- static void Main(string[] args)
- {
- Node<int> n = RandomNode();
- Stack<Node<int>> stk = NodeStack();
- PrintNodeStack(stk);
- Console.WriteLine(LongestNode(stk));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement