예제 #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);
        }
예제 #2
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
        }