Beispiel #1
0
        public KarthicStack SortStack(KarthicStack source)
        {
            KarthicStack result = new KarthicStack();

            Stack <int> s = new Stack <int>();

            while (!source.IsEmpty())
            {
                int value = source.Pop();
                //the value of the sorted stack should be always greater than the peek
                //sorted stack 5,4,3,2,1
                //Got be carefult with the while loop condition..The condition is to continue code
                //
                while (!result.IsEmpty() && value < result.Peek())
                {
                    //If the result already has larger value..push it to source and repear till it has lesser value
                    source.Push(result.Pop());
                }
                //the value that is push on the result should be always in asc order
                //the new value should be always larger than the peek
                result.Push(value);
            }

            return(result);
        }
Beispiel #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            //Solution2 with extra stacj

            string input = this.textBox4.Text;

            string[] list = input.Split(',');

            KarthicStack mystack = new KarthicStack();

            foreach (var i in list)
            {
                mystack.Push(Convert.ToInt16(i));
            }

            this.textBox3.Text = mystack.GetMin().ToString();
        }
Beispiel #3
0
        private void button5_Click(object sender, EventArgs e)
        {
            string input = this.textBox1.Text;

            string[] list = input.Split(',');

            KarthicStack mystack = new KarthicStack();

            foreach (var i in list)
            {
                mystack.Push(Convert.ToInt16(i));
            }

            StringBuilder sb = new StringBuilder();

            for (int j = 0; j < list.Length; j++)
            {
                sb.Append(mystack.Pop()).Append(',');
            }

            this.textBox2.Text = sb.ToString();
        }
Beispiel #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            string input = this.textBox6.Text;

            string[] list = input.Split(',');

            KarthicStack mystack = new KarthicStack();

            foreach (var i in list)
            {
                mystack.Push(Convert.ToInt16(i));
            }

            KarthicStack  result = SortStack(mystack);
            StringBuilder sb     = new StringBuilder();

            while (!result.IsEmpty())
            {
                sb.Append(result.Pop().ToString()).Append(',');
            }

            this.textBox5.Text = sb.ToString();
            //Sort the stack
        }