Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Stack.cs
- {
- class Program
- {
- //simple stack functions. mehr ist kalt!
- public static Stack<int> BuildAStack()
- {
- Stack<int> st = new Stack<int>();
- Console.WriteLine("Enter a number that you wish to enter the stack, at the end, enter 999");
- int num = 0;
- while (num != 999)
- {
- num = int.Parse(Console.ReadLine());
- st.Push(num);
- }
- return st;
- }
- public static void ShowTopToBottom(Stack<int> st)
- {
- Stack<int> temp = new Stack<int>();
- int value;
- while (!st.IsEmpty())
- {
- value = st.Pop();
- temp.Push(value);
- Console.WriteLine(value);
- }
- }
- public static void ShowBottomToTop(Stack<int> st)
- {
- Stack<int> temp = new Stack<int>();
- int value;
- while (!st.IsEmpty())
- {
- value = st.Pop();
- temp.Push(value);
- }
- value = 0;
- while (!temp.IsEmpty())
- {
- value = st.Pop();
- Console.WriteLine(value);
- }
- }
- public static bool Search(Stack<int> st, int value)
- {
- int popped = 0;
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- if (value == popped)
- {
- return true;
- }
- else
- return false;
- }
- return false;
- }
- public static int Place(Stack<int> st, int value)
- {
- int count = 0;
- int popped = 0;
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- while (popped != value)
- {
- count++;
- }
- if (popped == value)
- {
- return count;
- }
- }
- return -1;
- }
- public static int Max(Stack<int> st)
- {
- int max = 0;
- while (!st.IsEmpty())
- {
- max = st.Pop();
- if (st.Top()>max)
- {
- max = st.Top();
- }
- }
- return max;
- }
- public static int AmountOfMax(Stack<int> st)
- {
- int max = Max(st);
- int count = 0;
- int popped = 0;
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- if (popped == max)
- {
- count++;
- }
- else
- st.Push(popped);
- }
- return count;
- }
- public static void Extractor(Stack<int> st, int value)
- {
- Stack<int> temp = new Stack<int>();
- int popped = 0;
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- if (popped != value)
- {
- temp.Push(popped);
- }
- }
- popped = 0;
- while (!temp.IsEmpty())
- {
- popped = temp.Pop();
- st.Push(popped);
- }
- }
- public static bool TopEqualToBottom(Stack<int> st)
- {
- int top = st.Top();
- int popped = 0;
- Stack<int> temp = new Stack<int>();
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- temp.Push(popped);
- }
- if (temp.Top() == top)
- {
- return true;
- }
- return false;
- }
- public static bool SortedByLargest(Stack<int> st)
- {
- int popped = 0;
- Stack<int> temp = new Stack<int>();
- while (!st.IsEmpty())
- {
- popped = temp.Pop();
- temp.Push(popped);
- }
- int max = temp.Top();
- popped = 0;
- while (!temp.IsEmpty())
- {
- popped = temp.Pop();
- max = temp.Top();
- if (popped>max)
- {
- return true;
- }
- return false;
- }
- return false;
- }
- public static void FillBySeries( int a1, int d, int n,Stack<int> st)
- {
- st = new Stack<int>();
- st.Push(a1);
- for (int i = 0; i < n; i++)
- {
- a1 +=(n-1)*d;
- st.Push(a1);
- }
- }
- public static void Fibonachi(Stack<int> st, int length)
- {
- int pop1 = 0;
- int pop2 = 0;
- int count = 0;
- int sum = 0;
- while (count<length&&!st.IsEmpty())
- {
- pop1 = st.Pop();
- pop2 = st.Pop();
- sum = pop1 + pop2;
- st.Push(pop2);
- st.Push(pop1);
- st.Push(sum);
- }
- }
- public static void EnterWithoutFuckingUp(Stack<int> st, int num)
- {
- int popped = 0;
- Stack<int> si = new Stack<int>();
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- bool answer = popped < num;
- if (answer)
- {
- si.Push(num);
- si.Push(popped);
- }
- else if (!answer)
- {
- si.Push(popped);
- si.Push(num);
- }
- }
- while (!si.IsEmpty())
- {
- st.Push(si.Pop());
- }
- }
- public static Stack<int> MergeSortedStacks(Stack<int> s1, Stack<int> s2)
- {
- int popped1 = 0;
- int popped2 = 0;
- bool answer;
- Stack<int> sR = new Stack<int>();
- while (!s1.IsEmpty() && !s2.IsEmpty())
- {
- popped1 = s1.Pop();
- popped2 = s2.Pop();
- answer = popped1 > popped2;
- if (answer)
- {
- sR.Push(popped1);
- sR.Push(popped2);
- }
- else if (!answer)
- {
- sR.Push(popped2);
- sR.Push(popped1);
- }
- }
- return sR;
- }
- public static void ReplaceSequenceWithSum(Stack<int> st)
- {
- Stack<int> si = new Stack<int>();
- int popped = 0;
- int count = 0;
- int sum = 0;
- popped = st.Pop();
- si.Push(popped);
- while (!st.IsEmpty())
- {
- popped = st.Pop();
- if (popped == si.Top())
- {
- count++;
- sum = si.Top() * count;
- popped = st.Pop();
- if (popped != si.Top())
- {
- si.Push(sum);
- }
- }
- else
- si.Push(popped);
- }
- }
- public static bool IsSymmetrical(Stack<int> st)
- {
- Stack<int> temp = new Stack<int>();
- Stack<int> reversed = new Stack<int>();
- bool isSymmetric;
- while (!st.IsEmpty())
- {
- reversed.Push(st.Pop());
- temp.Push(reversed.Top());
- }
- while (!temp.IsEmpty())
- {
- if (temp.Top() == reversed.Pop())
- {
- isSymmetric = true;
- }
- st.Push(temp.Pop());
- }
- return false;
- }
- public static Stack<int> SameToBothStacks(Stack<int> si1, Stack<int> si2)
- {
- int popped1 = 0;
- int popped2 = 0;
- Stack<int> sR = new Stack<int>();
- Stack<int> s2 = new Stack<int>();
- Stack<int> s1 = new Stack<int>();
- while (!si1.IsEmpty())
- {
- popped1 = si1.Pop();
- while (!si2.IsEmpty())
- {
- popped2 = si2.Pop();
- if (popped1 == popped2)
- {
- sR.Push(popped1);
- s2.Push(popped2);
- s1.Push(popped1);
- }
- else
- {
- s2.Push(popped2);
- s1.Push(popped1);
- }
- }
- }
- return sR;
- }
- public static void Split(Stack<int> si, int value)
- {
- Stack<int> smaller = new Stack<int>();
- Stack<int> largerOrEqual = new Stack<int>();
- Stack<int> temp = new Stack<int>();
- int popped = 0;
- while (!si.IsEmpty())
- {
- popped = si.Pop();
- temp.Push(popped);
- }
- while (!temp.IsEmpty())
- {
- popped = temp.Pop();
- if (popped<value)
- {
- smaller.Push(popped);
- }
- else if (popped>value || popped==value)
- {
- largerOrEqual.Push(popped);
- }
- }
- }
- public static Stack<int> Multi(Stack<int> si, int value)
- {
- int popped = 0;
- Stack<int> temp = new Stack<int>();
- while (!si.IsEmpty())
- {
- popped = si.Pop();
- for (int i = 0; i < value; i++)
- {
- temp.Push(popped);
- }
- }
- while (!temp.IsEmpty())
- {
- si.Push(temp.Pop());
- }
- return si;
- }
- //public static int MaximalDifference(Stack<int> s)
- //{
- //}
- public static bool IsBalanced(Stack<int> s)
- {
- int sum = 0;
- Stack<int> st = new Stack<int>();
- Stack<int> temp = new Stack<int>();
- while (!s.IsEmpty())
- {
- sum += s.Top();
- temp.Push(s.Pop());
- }
- while (!temp.IsEmpty())
- {
- st.Push(temp.Pop());
- s.Push(temp.Pop());
- }
- bool checks = false;
- while (!st.IsEmpty())
- {
- int value = st.Pop();
- int num = sum - value;
- if (value == num)
- {
- checks = true;
- }
- }
- return checks;
- }
- static void Main(string[] args)
- {
- Stack<int> s = new Stack<int>();
- Console.WriteLine("enter a number, if you desire to stop, enter -1");
- int num = 0;
- while (num != -1)
- {
- num = int.Parse(Console.ReadLine());
- s.Push(num);
- }
- Console.WriteLine("for printing the stack, press p");
- string print = Console.ReadLine();
- if (print == "p")
- {
- while (s.IsEmpty())
- {
- Console.WriteLine(s.ToString());
- }
- }
- }
- //stacks with types and classes
- public static Stack<Student> BuildStackOfStudents()
- {
- Stack<Student> ss = new Stack<Student>();//an empty stack
- string id;
- Student student;
- Console.WriteLine("ahi! enter the id of the student please!");
- id = Console.ReadLine();
- while (!id.Equals("End"))
- {
- student = new Student();
- student.Id = id;
- Console.WriteLine("What is the name of the student?");
- student.Name = Console.ReadLine();
- Console.WriteLine("what is the age of the student?");
- student.Age = int.Parse(Console.ReadLine());
- Console.WriteLine("what is the gender of the student?");
- student.Gender = char.Parse(Console.ReadLine());
- ss.Push(student);
- Console.WriteLine("enter the id again please! ;-;");
- }
- return ss;
- }
- public static void AgesAboveFifteen(Stack<Student> st)
- {
- Student student;
- int popped = 0;
- Stack<Student> auxst = new Stack<Student>();
- while (!st.IsEmpty())
- {
- student = st.Pop();
- auxst.Push(student);
- popped = student.Age;
- if (popped>15 )
- {
- Console.WriteLine(student.Name + " " + student.Id + " " + student.Gender + " " + student.Age);
- }
- }
- while (!auxst.IsEmpty())
- {
- st.Push(auxst.Pop());
- }
- }
- public static int BoysMoreThanGirls(Stack<Student> ss)
- {
- int countGirls = 0;
- int countBoys = 0;
- Student popped;
- Stack<Student> tripleS = new Stack<Student>();
- while (!ss.IsEmpty())
- {
- popped = ss.Pop();
- if (popped.Gender == 'M')
- {
- countBoys++;
- }
- else if (popped.Gender == 'F')
- {
- countGirls++;
- }
- tripleS.Push(popped);
- }
- while (!tripleS.IsEmpty())
- {
- ss.Push(tripleS.Pop());
- }
- if (countGirls>countBoys)
- {
- return 2;
- }
- else if (countBoys>countGirls)
- {
- return 1;
- }
- else if (countGirls==countBoys)
- {
- return 0;
- }
- return -1;
- }
- public static Student DesiredStudent(Stack<Student> ss, string id)
- {
- Student popped;
- Stack<Student> tripleS = new Stack<Student>();
- while (!ss.IsEmpty())
- {
- popped = ss.Pop();
- if (popped.Id == id)
- {
- return popped;
- }
- tripleS.Push(popped);
- }
- while (!tripleS.IsEmpty())
- {
- ss.Push(tripleS.Pop());
- }
- return null;
- }
- public static void AddToSorted(Stack<Student> ss, Student st)
- {
- Student popped;
- Stack<Student> tripleS = new Stack<Student>();
- while (!ss.IsEmpty())
- {
- popped = ss.Pop();
- if (popped.Id[0]<st.Id[0])
- {
- tripleS.Push(st);
- tripleS.Push(popped);
- }
- else
- {
- tripleS.Push(popped);
- tripleS.Push(st);
- }
- }
- while (!tripleS.IsEmpty())
- {
- ss.Push(tripleS.Pop());
- }
- }
- //other assignments
- public static void CreateArrayOfStacks(Stack<int> [] arrst)
- {
- 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)");
- string reply = Console.ReadLine();
- if (reply == "n")
- {
- return;
- }
- else if (reply == "y")
- {
- Console.WriteLine("enter numbers. if you wish to stop, enter -1");
- for (int i = 0; i < arrst.Length; i++)
- {
- int num = 0;
- while (num != 999)
- {
- num = int.Parse(Console.ReadLine());
- arrst[i].Push(num);
- }
- }
- }
- }
- public static void PresentLargestInStacks(Stack<int> [] arrst)
- {
- for (int i = 0; i < arrst.Length; i++)
- {
- Console.WriteLine(Max(arrst[i]));
- }
- }
- public static int CountInArrays(Stack<int> [] arrst, int num)
- {
- int count = 0;
- for (int i = 0; i < arrst.Length; i++)
- {
- Search(arrst[i], num);
- count++;
- }
- return count;
- }
- public static int IsTopBottomArr(Stack<int>[] arrst)
- {
- int count = 0;
- for (int i = 0; i < arrst.Length; i++)
- {
- TopEqualToBottom(arrst[i]);
- count++;
- }
- return count;
- }
- //public static void AddToSortedArr(Stack<int>[] arrst, int num)
- //{
- // for (int i = 0; i < arrst.Length; i++)
- // {
- // AddToSorted(arrst[i],num);
- // }
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement