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;
- Stack<Node<int>> forreturn = new Stack<Node<int>>();
- Node<int> temp, temp2;
- Node<int> longest = null;
- while (!s.IsEmpty())
- {
- count = 0;
- temp = s.Pop();
- forreturn.Push(temp);
- temp2 = temp;
- if (longest == null)
- longest = temp;
- while (temp != null)
- {
- count++;
- temp = temp.GetNext();
- }
- if (count > count2)
- {
- longest = temp2;
- count2 = count;
- }
- }
- while (!forreturn.IsEmpty())
- s.Push(forreturn.Pop());
- return longest;
- }
- // Q4
- public static Stack<int> BiggestInNode(Stack<Node<int>> stk)
- {
- Console.WriteLine("Biggest Numbers:");
- Stack<int> NumStack = new Stack<int>();
- Stack<int> tempStack = new Stack<int>();
- Stack<Node<int>> forreturn = new Stack<Node<int>>();
- Node<int> temp;
- while (stk.IsEmpty() == false)
- {
- temp = stk.Pop();
- forreturn.Push(temp);
- int max = temp.GetInfo();
- while (temp != null)
- {
- if (temp.GetInfo() > max)
- {
- max = temp.GetInfo();
- }
- temp = temp.GetNext();
- }
- tempStack.Push(max);
- }
- while (!tempStack.IsEmpty())
- NumStack.Push(tempStack.Pop());
- while (!forreturn.IsEmpty())
- stk.Push(forreturn.Pop());
- return NumStack;
- }
- //Q5
- public static void DeleteShortest(Stack<Node<int>> s)
- {
- int count, count2 = int.MaxValue;
- Stack<Node<int>> forreturn = new Stack<Node<int>>();
- Node<int> temp, temp2;
- Node<int> shortest = null;
- while (!s.IsEmpty())
- {
- count = 0;
- temp = s.Pop();
- forreturn.Push(temp);
- temp2 = temp;
- if (shortest == null)
- shortest = temp;
- while (temp != null)
- {
- count++;
- temp = temp.GetNext();
- }
- if (count < count2)
- {
- shortest = temp2;
- count2 = count;
- }
- }
- while (!forreturn.IsEmpty())
- {
- temp = forreturn.Pop();
- if (temp != shortest)
- s.Push(temp);
- }
- }
- //Q7
- public static bool IsFound(Stack<Flower> s, string name)
- {
- while (!s.IsEmpty())
- if (s.Pop().Name == name)
- return true;
- return false;
- }
- //Q8
- public static double TotalPrice(Node<Flower> flwrs)
- {
- double sum = 0;
- while (flwrs != null)
- {
- sum = sum + flwrs.GetInfo().Price;
- flwrs = flwrs.GetNext();
- }
- return sum;
- }
- //Q9
- public static Flower HighestFlower(Node<Stack<Flower>> flwrs)
- {
- double max = 0;
- Stack<Flower> temp;
- double prc = 0;
- Flower big = new Flower("temp", "temp", 0);
- Flower tempflower;
- while (flwrs != null)
- {
- temp = flwrs.GetInfo();
- max = temp.Top().Price;
- while (temp.IsEmpty() == false)
- {
- tempflower = temp.Pop();
- prc = tempflower.Price;
- if (prc > max)
- {
- max = prc;
- big = tempflower;
- }
- }
- flwrs = flwrs.GetNext();
- }
- return big;
- }
- static void Main(string[] args)
- {
- Node<int> n = RandomNode();
- Stack<Node<int>> stk = NodeStack();
- Console.WriteLine("\nStack of nodes:");
- PrintNodeStack(stk);
- Console.WriteLine("\nLongest node first value");
- Console.WriteLine(LongestNode(stk));
- Console.WriteLine();
- Console.WriteLine(BiggestInNode(stk));
- DeleteShortest(stk);
- Console.WriteLine("\nShortest list removed:");
- PrintNodeStack(stk);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement