Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Stack.cs
  8. {
  9.     class Program
  10.     {
  11.         //simple stack functions. mehr ist kalt!
  12.         public static Stack<int> BuildAStack()
  13.         {
  14.             Stack<int> st = new Stack<int>();
  15.             Console.WriteLine("Enter a number that you wish to enter the stack, at the end, enter 999");
  16.             int num = 0;
  17.             while (num != 999)
  18.             {
  19.                 num = int.Parse(Console.ReadLine());
  20.                 st.Push(num);
  21.             }
  22.             return st;
  23.         }
  24.  
  25.         public static void ShowTopToBottom(Stack<int> st)
  26.         {
  27.             Stack<int> temp = new Stack<int>();
  28.             int value;
  29.             while (!st.IsEmpty())
  30.             {
  31.                 value = st.Pop();
  32.                 temp.Push(value);
  33.                 Console.WriteLine(value);
  34.             }
  35.         }
  36.  
  37.         public static void ShowBottomToTop(Stack<int> st)
  38.         {
  39.             Stack<int> temp = new Stack<int>();
  40.             int value;
  41.             while (!st.IsEmpty())
  42.             {
  43.                 value = st.Pop();
  44.                 temp.Push(value);
  45.             }
  46.             value = 0;
  47.             while (!temp.IsEmpty())
  48.             {
  49.                 value = st.Pop();
  50.                 Console.WriteLine(value);
  51.             }
  52.         }
  53.  
  54.         public static bool Search(Stack<int> st, int value)
  55.         {
  56.             int popped = 0;
  57.             while (!st.IsEmpty())
  58.             {
  59.                 popped = st.Pop();
  60.                 if (value == popped)
  61.                 {
  62.                     return true;
  63.                 }
  64.                 else
  65.                     return false;
  66.             }
  67.             return false;
  68.         }
  69.  
  70.         public static int Place(Stack<int> st, int value)
  71.         {
  72.             int count = 0;
  73.             int popped = 0;
  74.             while (!st.IsEmpty())
  75.             {
  76.                 popped = st.Pop();
  77.                 while (popped != value)
  78.                 {
  79.                     count++;
  80.                 }
  81.                 if (popped == value)
  82.                 {
  83.                     return count;
  84.                 }
  85.                                
  86.             }
  87.             return -1;
  88.         }
  89.  
  90.         public static int Max(Stack<int> st)
  91.         {
  92.             int max = 0;
  93.             while (!st.IsEmpty())
  94.             {
  95.                 max = st.Pop();
  96.                 if (st.Top()>max)
  97.                 {
  98.                     max = st.Top();
  99.                 }
  100.             }
  101.             return max;
  102.         }
  103.  
  104.         public static int AmountOfMax(Stack<int> st)
  105.         {
  106.             int max = Max(st);
  107.             int count = 0;
  108.             int popped = 0;
  109.             while (!st.IsEmpty())
  110.             {
  111.                 popped = st.Pop();
  112.                 if (popped == max)
  113.                 {
  114.                     count++;
  115.                 }
  116.                 else
  117.                     st.Push(popped);
  118.             }
  119.             return count;
  120.         }
  121.  
  122.         public static void Extractor(Stack<int> st, int value)
  123.         {
  124.             Stack<int> temp = new Stack<int>();
  125.             int popped = 0;
  126.             while (!st.IsEmpty())
  127.             {
  128.                 popped = st.Pop();
  129.                 if (popped != value)
  130.                 {
  131.                     temp.Push(popped);
  132.                 }
  133.             }
  134.             popped = 0;
  135.             while (!temp.IsEmpty())
  136.             {
  137.                 popped = temp.Pop();
  138.                 st.Push(popped);
  139.             }
  140.         }
  141.  
  142.         public static bool TopEqualToBottom(Stack<int> st)
  143.         {
  144.             int top = st.Top();
  145.             int popped = 0;
  146.             Stack<int> temp = new Stack<int>();
  147.             while (!st.IsEmpty())
  148.             {
  149.                 popped = st.Pop();
  150.                 temp.Push(popped);
  151.             }
  152.             if (temp.Top() == top)
  153.             {
  154.                 return true;
  155.             }
  156.             return false;
  157.         }
  158.  
  159.         public static bool SortedByLargest(Stack<int> st)
  160.         {
  161.             int popped = 0;
  162.             Stack<int> temp = new Stack<int>();
  163.             while (!st.IsEmpty())
  164.             {
  165.                 popped = temp.Pop();
  166.                 temp.Push(popped);
  167.             }
  168.             int max = temp.Top();
  169.             popped = 0;
  170.             while (!temp.IsEmpty())
  171.             {
  172.                 popped = temp.Pop();
  173.                 max = temp.Top();
  174.                 if (popped>max)
  175.                 {
  176.                     return true;
  177.                 }
  178.                 return false;
  179.             }
  180.             return false;
  181.         }
  182.  
  183.         public static void FillBySeries( int a1, int d, int n,Stack<int> st)
  184.         {
  185.             st = new Stack<int>();
  186.             st.Push(a1);
  187.             for (int i = 0; i < n; i++)
  188.             {
  189.                 a1 +=(n-1)*d;
  190.                 st.Push(a1);
  191.             }
  192.         }
  193.  
  194.         public static void Fibonachi(Stack<int> st, int length)
  195.         {
  196.             int pop1 = 0;
  197.             int pop2 = 0;
  198.             int count = 0;
  199.             int sum = 0;
  200.             while (count<length&&!st.IsEmpty())
  201.             {
  202.                  pop1 = st.Pop();
  203.                 pop2 = st.Pop();
  204.                 sum = pop1 + pop2;
  205.                 st.Push(pop2);
  206.                 st.Push(pop1);
  207.                 st.Push(sum);
  208.             }
  209.  
  210.         }
  211.  
  212.         public static void EnterWithoutFuckingUp(Stack<int> st, int num)
  213.         {
  214.             int popped = 0;
  215.             Stack<int> si = new Stack<int>();
  216.             while (!st.IsEmpty())
  217.             {
  218.                
  219.                 popped = st.Pop();
  220.                 bool answer = popped < num;
  221.                 if (answer)
  222.                 {
  223.                     si.Push(num);
  224.                     si.Push(popped);
  225.                 }
  226.                 else if (!answer)
  227.                 {
  228.                     si.Push(popped);
  229.                     si.Push(num);
  230.                 }
  231.                
  232.             }
  233.             while (!si.IsEmpty())
  234.             {
  235.                 st.Push(si.Pop());
  236.             }
  237.         }
  238.  
  239.         public static Stack<int> MergeSortedStacks(Stack<int> s1, Stack<int> s2)
  240.         {
  241.             int popped1 = 0;
  242.             int popped2 = 0;
  243.             bool answer;
  244.             Stack<int> sR = new Stack<int>();
  245.             while (!s1.IsEmpty() && !s2.IsEmpty())
  246.             {
  247.                
  248.                 popped1 = s1.Pop();
  249.                 popped2 = s2.Pop();
  250.                 answer = popped1 > popped2;
  251.                 if (answer)
  252.                 {
  253.                     sR.Push(popped1);
  254.                     sR.Push(popped2);
  255.                 }
  256.                 else if (!answer)
  257.                 {
  258.                     sR.Push(popped2);
  259.                     sR.Push(popped1);
  260.                 }  
  261.             }
  262.             return sR;
  263.         }
  264.  
  265.         public static void ReplaceSequenceWithSum(Stack<int> st)
  266.         {
  267.             Stack<int> si = new Stack<int>();
  268.             int popped = 0;
  269.             int count = 0;
  270.             int sum = 0;
  271.             popped = st.Pop();
  272.             si.Push(popped);
  273.             while (!st.IsEmpty())
  274.             {
  275.                 popped = st.Pop();
  276.                 if (popped == si.Top())
  277.                 {
  278.                     count++;
  279.                     sum = si.Top() * count;
  280.                     popped = st.Pop();
  281.                     if (popped != si.Top())
  282.                     {
  283.                         si.Push(sum);
  284.                     }
  285.                 }
  286.                 else
  287.                     si.Push(popped);
  288.  
  289.             }
  290.         }
  291.  
  292.         public static bool IsSymmetrical(Stack<int> st)
  293.         {
  294.             Stack<int> temp = new Stack<int>();
  295.             Stack<int> reversed = new Stack<int>();
  296.             bool isSymmetric;
  297.             while (!st.IsEmpty())
  298.             {
  299.                 reversed.Push(st.Pop());
  300.                 temp.Push(reversed.Top());
  301.             }
  302.             while (!temp.IsEmpty())
  303.             {
  304.                 if (temp.Top() == reversed.Pop())
  305.                 {
  306.                     isSymmetric = true;
  307.                 }
  308.                 st.Push(temp.Pop());
  309.             }
  310.             return false;
  311.         }
  312.  
  313.         public static Stack<int> SameToBothStacks(Stack<int> si1, Stack<int> si2)
  314.         {
  315.             int popped1 = 0;
  316.             int popped2 = 0;
  317.             Stack<int> sR = new Stack<int>();
  318.             Stack<int> s2 = new Stack<int>();
  319.             Stack<int> s1 = new Stack<int>();
  320.             while (!si1.IsEmpty())
  321.             {
  322.                 popped1 = si1.Pop();
  323.                 while (!si2.IsEmpty())
  324.                 {
  325.                     popped2 = si2.Pop();
  326.                     if (popped1 == popped2)
  327.                     {
  328.                         sR.Push(popped1);
  329.                         s2.Push(popped2);
  330.                         s1.Push(popped1);
  331.                     }
  332.                     else
  333.                     {
  334.                         s2.Push(popped2);
  335.                         s1.Push(popped1);
  336.                     }
  337.  
  338.                 }
  339.             }
  340.             return sR;
  341.         }
  342.  
  343.         public static void Split(Stack<int> si, int value)
  344.         {
  345.             Stack<int> smaller = new Stack<int>();
  346.             Stack<int> largerOrEqual = new Stack<int>();
  347.             Stack<int> temp = new Stack<int>();
  348.             int popped = 0;
  349.             while (!si.IsEmpty())
  350.             {
  351.                 popped = si.Pop();
  352.                 temp.Push(popped);
  353.             }
  354.             while (!temp.IsEmpty())
  355.             {
  356.                 popped = temp.Pop();
  357.                 if (popped<value)
  358.                 {
  359.                     smaller.Push(popped);
  360.                 }
  361.                 else if (popped>value || popped==value)
  362.                 {
  363.                     largerOrEqual.Push(popped);
  364.                 }
  365.  
  366.             }
  367.         }
  368.  
  369.         public static Stack<int> Multi(Stack<int> si, int value)
  370.         {
  371.             int popped = 0;
  372.             Stack<int> temp = new Stack<int>();
  373.             while (!si.IsEmpty())
  374.             {
  375.                 popped = si.Pop();
  376.                 for (int i = 0; i < value; i++)
  377.                 {
  378.                     temp.Push(popped);
  379.                 }
  380.             }
  381.             while (!temp.IsEmpty())
  382.             {
  383.                 si.Push(temp.Pop());
  384.             }
  385.             return si;
  386.         }
  387.  
  388.         //public static int MaximalDifference(Stack<int> s)
  389.         //{
  390.  
  391.         //}
  392.  
  393.  
  394.         public static bool IsBalanced(Stack<int> s)
  395.         {
  396.             int sum = 0;
  397.             Stack<int> st = new Stack<int>();
  398.             Stack<int> temp = new Stack<int>();
  399.             while (!s.IsEmpty())
  400.             {
  401.                 sum += s.Top();
  402.                 temp.Push(s.Pop());
  403.             }
  404.             while (!temp.IsEmpty())
  405.             {
  406.                 st.Push(temp.Pop());
  407.                 s.Push(temp.Pop());
  408.             }
  409.             bool checks = false;
  410.             while (!st.IsEmpty())
  411.             {
  412.                 int value = st.Pop();
  413.                 int num = sum - value;
  414.                 if (value == num)
  415.                 {
  416.                     checks = true;
  417.                 }
  418.             }
  419.             return checks;
  420.         }
  421.            
  422.         static void Main(string[] args)
  423.         {
  424.             Stack<int> s = new Stack<int>();
  425.             Console.WriteLine("enter a number, if you desire to stop, enter -1");
  426.             int num = 0;
  427.             while (num != -1)
  428.             {
  429.                num = int.Parse(Console.ReadLine());
  430.                 s.Push(num);
  431.             }
  432.            
  433.             Console.WriteLine("for printing the stack, press p");
  434.             string print = Console.ReadLine();
  435.             if (print == "p")
  436.             {
  437.                 while (s.IsEmpty())
  438.                 {
  439.                     Console.WriteLine(s.ToString());
  440.                 }
  441.             }
  442.         }
  443.         //stacks with types and classes
  444.         public static Stack<Student> BuildStackOfStudents()
  445.         {
  446.             Stack<Student> ss = new Stack<Student>();//an empty stack
  447.             string id;
  448.             Student student;
  449.             Console.WriteLine("ahi! enter the id of the student please!");
  450.             id = Console.ReadLine();
  451.             while (!id.Equals("End"))
  452.             {
  453.                 student = new Student();
  454.                 student.Id = id;
  455.                 Console.WriteLine("What is the name of the student?");
  456.                 student.Name = Console.ReadLine();
  457.                 Console.WriteLine("what is the age of the student?");
  458.                 student.Age = int.Parse(Console.ReadLine());
  459.                 Console.WriteLine("what is the gender of the student?");
  460.                 student.Gender = char.Parse(Console.ReadLine());
  461.                 ss.Push(student);
  462.                 Console.WriteLine("enter the id again please! ;-;");
  463.             }
  464.             return ss;
  465.         }
  466.  
  467.         public static void AgesAboveFifteen(Stack<Student> st)
  468.         {
  469.             Student student;
  470.             int popped = 0;
  471.             Stack<Student> auxst = new Stack<Student>();
  472.             while (!st.IsEmpty())
  473.             {
  474.                 student = st.Pop();
  475.                 auxst.Push(student);
  476.                 popped = student.Age;
  477.                 if (popped>15 )
  478.                 {
  479.                     Console.WriteLine(student.Name + " " + student.Id + "  " + student.Gender + "  " + student.Age);
  480.                 }
  481.             }
  482.             while (!auxst.IsEmpty())
  483.             {
  484.                 st.Push(auxst.Pop());
  485.             }
  486.         }
  487.  
  488.         public static int BoysMoreThanGirls(Stack<Student> ss)
  489.         {
  490.             int countGirls = 0;
  491.             int countBoys = 0;
  492.             Student popped;
  493.             Stack<Student> tripleS = new Stack<Student>();
  494.             while (!ss.IsEmpty())
  495.             {
  496.                 popped = ss.Pop();
  497.                 if (popped.Gender == 'M')
  498.                 {
  499.                     countBoys++;
  500.                 }
  501.                 else if (popped.Gender == 'F')
  502.                 {
  503.                     countGirls++;
  504.                 }
  505.                 tripleS.Push(popped);
  506.             }
  507.             while (!tripleS.IsEmpty())
  508.             {
  509.                 ss.Push(tripleS.Pop());
  510.             }
  511.             if (countGirls>countBoys)
  512.             {
  513.                 return 2;
  514.             }
  515.             else if (countBoys>countGirls)
  516.             {
  517.                 return 1;
  518.             }
  519.             else if (countGirls==countBoys)
  520.             {
  521.                 return 0;
  522.             }
  523.             return -1;
  524.         }
  525.  
  526.         public static Student DesiredStudent(Stack<Student> ss, string id)
  527.         {
  528.             Student popped;
  529.             Stack<Student> tripleS = new Stack<Student>();
  530.             while (!ss.IsEmpty())
  531.             {
  532.                 popped = ss.Pop();
  533.                 if (popped.Id == id)
  534.                 {
  535.                     return popped;
  536.                 }
  537.                 tripleS.Push(popped);
  538.             }
  539.             while (!tripleS.IsEmpty())
  540.             {
  541.                 ss.Push(tripleS.Pop());
  542.             }
  543.             return null;
  544.         }
  545.  
  546.         public static void AddToSorted(Stack<Student> ss, Student st)
  547.         {
  548.             Student popped;
  549.             Stack<Student> tripleS = new Stack<Student>();
  550.             while (!ss.IsEmpty())
  551.             {
  552.                 popped = ss.Pop();
  553.                 if (popped.Id[0]<st.Id[0])
  554.                 {
  555.                     tripleS.Push(st);
  556.                     tripleS.Push(popped);
  557.                 }
  558.                 else
  559.                 {
  560.                     tripleS.Push(popped);
  561.                     tripleS.Push(st);
  562.                 }
  563.             }
  564.             while (!tripleS.IsEmpty())
  565.             {
  566.                 ss.Push(tripleS.Pop());
  567.             }
  568.         }
  569.         //other assignments
  570.         public static void CreateArrayOfStacks(Stack<int> [] arrst)
  571.         {
  572.             Console.WriteLine("you will now be entering numbers to a stack that is a part of an array of stacks;\ndo you wish to continue? (y/n)");
  573.             string reply = Console.ReadLine();
  574.             if (reply == "n")
  575.             {
  576.                 return;
  577.             }
  578.             else if (reply == "y")
  579.             {
  580.                 Console.WriteLine("enter numbers. if you wish to stop, enter -1");
  581.                 for (int i = 0; i < arrst.Length; i++)
  582.                 {
  583.                     int num = 0;
  584.                     while (num != 999)
  585.                     {
  586.                         num = int.Parse(Console.ReadLine());
  587.                         arrst[i].Push(num);
  588.                     }
  589.                 }
  590.             }
  591.         }
  592.  
  593.         public static void PresentLargestInStacks(Stack<int> [] arrst)
  594.         {
  595.             for (int i = 0; i < arrst.Length; i++)
  596.             {
  597.                 Console.WriteLine(Max(arrst[i]));
  598.             }
  599.         }
  600.  
  601.         public static int CountInArrays(Stack<int> [] arrst, int num)
  602.         {
  603.             int count = 0;
  604.             for (int i = 0; i < arrst.Length; i++)
  605.             {
  606.                 Search(arrst[i], num);
  607.                 count++;
  608.             }
  609.             return count;
  610.         }
  611.  
  612.         public static int IsTopBottomArr(Stack<int>[] arrst)
  613.         {
  614.             int count = 0;
  615.             for (int i = 0; i < arrst.Length; i++)
  616.             {
  617.                 TopEqualToBottom(arrst[i]);
  618.                 count++;
  619.             }
  620.             return count;
  621.         }
  622.  
  623.         //public static void AddToSortedArr(Stack<int>[] arrst, int num)
  624.         //{
  625.         //    for (int i = 0; i < arrst.Length; i++)
  626.         //    {
  627.         //        AddToSorted(arrst[i],num);
  628.         //    }
  629.         //}
  630.  
  631.     }
  632. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement