public static void Main()
        {
            var testStack = new MyStack <int>();

            for (int i = 0; i < 10; i++)
            {
                testStack.Push((i + 1) * 5);
                Console.WriteLine("At position {0}. is inserted {1}", i, testStack.Peek());
            }

            var lastElement = testStack.Pop();

            Console.WriteLine("{0} was removed from the stack", lastElement);

            Console.WriteLine("The number of elemnents in the satck is {0} and last one is {1}", testStack.Count, testStack.Peek());
        }
        static void Main(string[] args)
        {
            MyStack <int> numbers = new MyStack <int>();


            for (int i = 0; i < 10; i++)
            {
                numbers.Push(i);
            }

            for (int i = 0; i < 10; i++)
            {
                Console.Write(numbers.Pop() + " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            MyStack <int> myStack = new MyStack <int>();

            while (true)
            {
                string inputComand = Console.ReadLine();

                if (inputComand == "END")
                {
                    break;
                }

                if (inputComand.StartsWith("Push"))
                {
                    var itemsToPush = inputComand
                                      .Substring(inputComand.IndexOf(" ") + 1)
                                      .Split(", ");

                    foreach (var item in itemsToPush)
                    {
                        myStack.Push(int.Parse(item));
                    }
                }

                else if (inputComand.StartsWith("Pop"))
                {
                    if (!myStack.Any())
                    {
                        Console.WriteLine("No elements");
                        continue;
                    }

                    myStack.Pop();
                }
            }

            for (int i = 0; i < 2; i++)
            {
                foreach (var item in myStack)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            MyStack <int> stack = new MyStack <int>();

            for (int i = 0; i < 10; i++)
            {
                stack.Push(i);
            }

            Console.WriteLine(stack.Count);

            Console.WriteLine(stack.Peek());

            while (stack.Count > 0)
            {
                Console.WriteLine(stack.Pop());
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            MyStack <int> s1 = new MyStack <int>();

            s1.Push(3);
            s1.Push(4);
            s1.Push(1);
            PrintStack(s1);
            s1.Push(-2);
            s1.Push(341);
            s1.Pop();
            //PrintStack(s1);
            foreach (int item in s1)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
Beispiel #6
0
Datei: F.cs Projekt: nAglTI/pr1
        public F(int n)
        {
            var stack = new MyStack <int>(n);
            int num   = n;

            while (stack.Count() != n)
            {
                if (num > 0)
                {
                    stack.Push(num--);
                    Console.WriteLine(stack.Peek());
                }
            }
            while (!stack.IsEmpty())
            {
                Console.WriteLine(stack.Pop().ToString());
            }
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            var myStack = new MyStack <int>();

            var command = Console.ReadLine();

            while (command != "END")
            {
                var input  = command.Split();
                var action = input[0];

                if (action == "Push")
                {
                    var data    = command.Substring(5);
                    var numbers = data.Split(", ").Select(int.Parse).ToList();

                    for (int i = 0; i < numbers.Count; i++)
                    {
                        myStack.Push(numbers[i]);
                    }
                }
                else if (action == "Pop")
                {
                    try
                    {
                        myStack.Pop();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                command = Console.ReadLine();
            }

            for (int i = 0; i < 2; i++)
            {
                foreach (var item in myStack)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            MyStack <int> myStack = new MyStack <int>();

            string line;

            while ((line = Console.ReadLine()) != "END")
            {
                try          // опитай да изпълниш този код
                {
                    string[] input   = line.Split(new string[] { ", ", " " }, StringSplitOptions.RemoveEmptyEntries);
                    string   command = input[0];

                    switch (command)
                    {
                    case "Push":
                        List <int> elements = input.Skip(1).Select(int.Parse).ToList();
                        foreach (var item in elements)
                        {
                            myStack.Push(item);
                        }
                        break;

                    case "Pop":                 // вместо Try-Catch може да сложим проверка преди да изпълним Pop()
                        myStack.Pop();
                        break;
                    }
                }
                catch (InvalidOperationException e) // catch (Exception e) - ще прихваща и отпечатва всички грешка
                                                    // catch (InvalidOperationException e) - можем да конкретизираме типа на грешката, който да прихваща
                {
                    Console.WriteLine(e.Message);   // отпечатва съобщението на грешката
                }
            }
            foreach (var item in myStack)
            {
                Console.WriteLine(item);
            }

            foreach (var item in myStack)
            {
                Console.WriteLine(item);
            }
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            MyStack <int> stack = new MyStack <int>();

            string commandInput = Console.ReadLine();

            while (commandInput != "END")
            {
                string[] tokens = commandInput.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries);

                string command = tokens[0];

                if (command == "Push")
                {
                    foreach (var item in tokens.Skip(1))
                    {
                        stack.Push(int.Parse(item));
                    }
                }
                else if (command == "Pop")
                {
                    if (stack.Count == 0)
                    {
                        Console.WriteLine("No elements");
                    }
                    else
                    {
                        stack.Pop();
                    }
                }

                commandInput = Console.ReadLine();
            }
            foreach (int i in stack)
            {
                Console.WriteLine(i);
            }
            foreach (int i in stack)
            {
                Console.WriteLine(i);
            }
        }
        static void Main(string[] args)
        {
            MyStack <int> mystack = new MyStack <int>();

            string comandInput = Console.ReadLine();

            while (comandInput != "END")
            {
                string[] comandData = comandInput.Split(new string[] { ", ", " " }, StringSplitOptions.RemoveEmptyEntries);
                string   comand     = comandData[0];

                if (comand == "Push")
                {
                    for (int i = 1; i < comandData.Length; i++)
                    {
                        int item = int.Parse(comandData[i]);
                        mystack.Push(item);
                    }
                }
                else if (comand == "Pop")
                {
                    try
                    {
                        mystack.Pop();
                    }
                    catch (InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }


                comandInput = Console.ReadLine();
            }

            foreach (var item in mystack)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine(string.Join(Environment.NewLine, mystack));
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            MyStack stack = new MyStack(5);

            stack.Push(5);
            stack.Push(4);
            stack.Push(3);
            stack.Push(2);
            stack.Push(1);

            Console.WriteLine("1. Size of stack after push operations: " + stack.Size);
            Console.WriteLine("2. Pop elements from stack : ");

            while (!stack.IsEmpty())
            {
                Console.WriteLine(stack.Pop());
            }

            Console.WriteLine("\n3. Size of stack after pop operations : " + stack.Size);
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            MyStack <int> stack = new MyStack <int>();

            string input = Console.ReadLine();

            while (input != "END")
            {
                string[] commands = input.Split(new string[] { " ", ", " }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                string command = commands[0];

                if (command == "Push")
                {
                    for (int i = 1; i < commands.Length; i++)
                    {
                        stack.Push(int.Parse(commands[i]));
                    }
                }
                else if (command == "Pop")
                {
                    try
                    {
                        stack.Pop();
                    }
                    catch (InvalidOperationException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                input = Console.ReadLine();
            }

            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine(string.Join(Environment.NewLine, stack));
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            MyStack <string> myStack = new MyStack <string>();

            string input = Console.ReadLine();

            while (input != "END")
            {
                string[] tokens  = input.Split(new string[] { " ", ", " }, StringSplitOptions.RemoveEmptyEntries);
                string   command = tokens[0];
                if (command == "Push")
                {
                    foreach (var item in tokens.Skip(1))
                    {
                        myStack.Push(item);
                    }
                }
                if (command == "Pop")
                {
                    if (myStack.Count != 0)
                    {
                        myStack.Pop();
                    }
                    else
                    {
                        Console.WriteLine("No elements");
                    }
                }

                input = Console.ReadLine();
            }
            foreach (var item in myStack)
            {
                Console.WriteLine(item);
            }
            foreach (var item in myStack)
            {
                Console.WriteLine(item);
            }
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            MyStack <int> stack1 = new MyStack <int>(3);

            Console.WriteLine(stack1.stack.Length);
            Console.WriteLine(stack1.IsEmpty());
            stack1.PrintStack();
            stack1.Push(8);
            stack1.PrintStack();
            Console.WriteLine(stack1.Count);
            Console.WriteLine(stack1.Size);
            stack1.Push(6);

            stack1.PrintStack();
            stack1.Pop();
            Console.WriteLine(stack1.Count);

            Console.WriteLine(stack1.IsEmpty());
            stack1.PrintStack();

            Console.WriteLine(stack1.Count);
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            MyStack <String> stack = new MyStack <string>();

            stack.Push("Prvi string");
            stack.Push("Drugi string");
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine();

            stack.Push("Novi Prvi string");
            stack.Push("Novi Drugi string");
            stack.Push("Novi Treci string");
            stack.Push("Novi Cetvrti string");

            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());

            Console.ReadKey();
        }
Beispiel #16
0
        static void Main(string[] args)
        {
            MyStack <int> myStack = new MyStack <int>();

            string[] command = Console.ReadLine().Split(new string[] { " ", ", " }, StringSplitOptions.RemoveEmptyEntries);

            while (command[0] != "END")
            {
                if (command[0] == "Push")
                {
                    for (int i = 1; i < command.Length; i++)
                    {
                        myStack.Push(int.Parse(command[i].Substring(0, command[i].Length)));
                    }
                }
                else if (command[0] == "Pop")
                {
                    try
                    {
                        myStack.Pop();
                    }
                    catch (InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                command = Console.ReadLine().Split();
            }

            for (int i = 0; i < 2; i++)
            {
                foreach (int item in myStack)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #17
0
        static void Main(string[] args)
        {
            var    myStack = new MyStack <int>();
            string input;

            while ((input = Console.ReadLine()) != "END")
            {
                var cmdArg = input.Split(' ', 2);
                if (cmdArg[0] == "Push")
                {
                    var elements = cmdArg[1].Split(", ").Select(int.Parse).ToArray();
                    myStack.Push(elements);
                }
                else if (cmdArg[0] == "Pop")
                {
                    myStack.Pop();
                }
            }
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine(string.Join(Environment.NewLine, myStack));
            }
        }
        public static void Main()
        {
            MyStack <int> myStack = new MyStack <int>();
            string        input;

            while ((input = Console.ReadLine()) != "END")
            {
                var cmdArgs = input.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                var cmd     = cmdArgs[0];
                var args    = cmdArgs.Skip(1).Select(int.Parse).ToArray();
                switch (cmd)
                {
                case "Push":
                    myStack.Push(args);
                    break;

                case "Pop":
                    try
                    {
                        myStack.Pop();
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    break;

                case "END":
                    return;
                }
            }
            foreach (var stack in myStack)
            {
                Console.WriteLine(stack);
            }
        }
Beispiel #19
0
        static void Main(string[] args)
        {
            MyStack stack = new MyStack();

            stack.Push(1);
            stack.Push(5);
            stack.Push(8);
            Console.WriteLine($"Count={stack.Count}");
            stack.Pop();
            stack.Push(5);
            stack.Push(8);
            stack.Clear();
            stack.Push(14);
            stack.Push(5);
            stack.Push(8);
            stack.Push(5);
            stack.Push(")");
            stack.Push(")");
            stack.Push(8);
            stack.Pop();
            stack.Pop();
            Console.WriteLine($"Top={stack.Top()}");
            stack.Pop();
        }
Beispiel #20
0
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch watch, watch1, watch2;
            long elapsedMs;
            int  N = 1000000;

            var mystack = new MyStack <int>(N);

            var stack = new Stack <int>();

            watch  = System.Diagnostics.Stopwatch.StartNew();
            watch1 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i != N; i++)
            {
                stack.Push(i);
            }
            watch1.Stop();
            watch2 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i != N; i++)
            {
                stack.Pop();
            }
            watch2.Stop();
            watch.Stop();

            elapsedMs = watch1.ElapsedMilliseconds;
            Console.WriteLine("Stack: PushTime - {0} ms", elapsedMs);

            elapsedMs = watch2.ElapsedMilliseconds;
            Console.WriteLine("Poptime - {0} ms", elapsedMs);

            elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Fulltime - {0} ms", elapsedMs);

            Console.WriteLine();
            Console.ReadKey();

            watch  = System.Diagnostics.Stopwatch.StartNew();
            watch1 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i != N; i++)
            {
                mystack.Push(i);
            }
            watch1.Stop();
            watch2 = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i != N; i++)
            {
                mystack.Pop();
            }
            watch2.Stop();
            watch.Stop();

            elapsedMs = watch1.ElapsedMilliseconds;
            Console.WriteLine("MyStack: PushTime - {0} ms;", elapsedMs);

            elapsedMs = watch2.ElapsedMilliseconds;
            Console.WriteLine("Poptime - {0} ms;", elapsedMs);

            elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Fulltime - {0} ms.", elapsedMs);

            var f = new F(3);

            /////////////////// Обратная польская запись

            void ReversePolish(string notOPZstr)
            {
                MyStack <char> myStack2 = new MyStack <char>();
                string         result   = string.Empty;

                for (var i = 0; i < notOPZstr.Length; i++)
                {
                    if (char.IsNumber(notOPZstr[i]))
                    {
                        if (i != 0 && !char.IsNumber(notOPZstr[i - 1]))
                        {
                            result += ' ';
                        }
                        result += notOPZstr[i];
                    }
                    else
                    {
                        switch (notOPZstr[i])
                        {
                        case '(':
                            myStack2.Push(notOPZstr[i]);
                            break;

                        case ')':
                        {
                            while (myStack2.Peek() != '(' && SignPrioritet(myStack2.Peek()) >= SignPrioritet(notOPZstr[i]))
                            {
                                result += $" {myStack2.Peek()}";
                                myStack2.Pop();
                            }
                            myStack2.Pop();
                            break;
                        }

                        default:
                        {
                            while (!myStack2.IsEmpty() && SignPrioritet(myStack2.Peek()) >= SignPrioritet(notOPZstr[i]))
                            {
                                result += $" {myStack2.Peek()}";
                                myStack2.Pop();
                            }
                            myStack2.Push(notOPZstr[i]);
                            break;
                        }
                        }
                    }
                }

                while (!myStack2.IsEmpty())
                {
                    result += $" {myStack2.Peek()}";
                    myStack2.Pop();
                }
                Console.WriteLine(result);
            }

            int SignPrioritet(char sign)
            {
                int priority = 0;

                switch (sign)
                {
                case '^': priority = 3; break;

                case '*': priority = 2; break;

                case '/': priority = 2; break;

                case '+': priority = 1; break;

                case '-': priority = 1; break;
                }
                return(priority);
            }

            Console.ReadKey();
        }
Beispiel #21
0
 public LocalEnumerator(MyStack <T> s)
 {
     parent = s;
     index  = -1;
 }
Beispiel #22
0
        static void Main(string[] args)
        {
            Stack <string> stringStack =
                new Stack <string>();

            for (int i = 1; i <= 10; i++)
            {
                stringStack.Push(i.ToString());
            }
            try
            {
                while (true)
                {
                    string x = stringStack.Pop();
                    Console.WriteLine(x.ToString() + " ");
                }
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message + "\n");
            }

            MyStack <string> myStringStack =
                new MyStack <string>();

            for (int i = 1; i <= 10; i++)
            {
                myStringStack.Push(i.ToString());
            }
            try
            {
                while (true)
                {
                    string x = myStringStack.Pop();
                    Console.Write(x.ToString() + " ");
                }
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("\n" + e.Message);
            }

            for (int i = 1; i <= 10; i++)
            {
                stringStack.Push(i.ToString());
            }
            List <string> stringList = stringStack.ToList();

            stringList.Reverse();
            foreach (var x in stringList)
            {
                Console.Write(x + " ");
            }
            Console.WriteLine();

            for (int i = 1; i <= 10; i++)
            {
                myStringStack.Push(i.ToString());
            }
            List <string> myStringList = myStringStack.ToList();

            foreach (var x in myStringList)
            {
                Console.Write(x + " ");
            }
            Console.WriteLine();

            foreach (var x in myStringStack)
            {
                Console.Write(x.ToString() + " ");
            }
            Console.WriteLine();

            MyStack <string> secondStack = (MyStack <string>)myStringStack.Clone();

            foreach (var x in secondStack)
            {
                Console.Write(x.ToString() + " ");
            }
            Console.WriteLine();

            Stack <string> thirdStack = myStringStack.ToStack();

            foreach (var x in thirdStack)
            {
                Console.Write(x.ToString() + " ");
            }
            Console.WriteLine();


            MyStack <int> intStack = new MyStack <int>();

            for (int i = 1; i <= 10; i++)
            {
                intStack.Push(i);
            }
            try
            {
                for (int i = 0; i < 12; i++)
                {
                    Console.WriteLine(intStack[i]);
                }
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine(e.Message);
                // return;
            }
            Console.ReadKey();
        }
 public Enumerator(MyStack <T> myStack)
 {
     stack = myStack;
 }