Locked C#

  • Monday, April 30, 2012 5:46 AM
     
     

    using System;
    using System.Text;

    namespace Stack_Implementation
    {
        class Node
        {
            public int info;
            public Node next;
            public Node(int i, Node n)
            {
                info = i;
                next = n;
            }
        }

        class Stacks
        {

            Node top;

            public Stacks()
            {
                top = null;
            }

            bool empty()
            {
                if (top == null)
                    return (true);
                else
                    return (false);
            }

            public void push(int element)
            {
                Node fresh;
                fresh = new Node(element, null);

                fresh.next = top;
                top = fresh;
                Console.WriteLine("\n" + element + " pushed.");
            }

            public void pop()
            {
                Console.WriteLine("\nThe popped element is: " + top.info);
                top = top.next; //Make top point to the next node in sequence
            }

            public void display()
            {
                Node tmp;
                if (empty())    //If stack is empty
                    Console.WriteLine("\nStack Empty");
                else
                {
                    //Traverse the list from begginning till end
                    for (tmp = top; tmp != null; tmp = tmp.next)
                    {
                        Console.WriteLine(tmp.info);
                    }
                    Console.WriteLine();
                }
            }
            static void Main(string[] args)
            {
                Stacks s = new Stacks();
                while (true)
                {
                    Console.WriteLine();
                    Console.WriteLine("\n***Stack Menu***\n");
                    Console.WriteLine("1. Push.");
                    Console.WriteLine("2. Pop");
                    Console.WriteLine("3. Display");
                    Console.WriteLine("4. Exit");

                    Console.Write("\nEnter your choice: ");

                    string sInput = Console.ReadLine();

                    char ch = Convert.ToChar(sInput == "" ? "0" : sInput);
                    switch (ch)
                    {
                        case '1':
                            Console.Write("\nEnter a number: ");
                            int num = Convert.ToInt32(Console.ReadLine());
                            s.push(num);
                            break;
                        case '2':
                            if (s.empty())
                            {
                                Console.WriteLine("\nStack Empty");
                                break;
                            }
                            s.pop();
                            break;
                        case '3':
                            s.display();
                            break;
                        case '4':
                            return;
                        default:
                            Console.WriteLine("\nInvalid Choice");
                            break;

                    }
                }
            }
        }
    }

     What is the use of ? symbol in Stack in  c sharp


    Ashish

    • Moved by sipla Wednesday, May 02, 2012 11:03 AM (From:Chart Controls for .NET Framework)
    •  

All Replies

  • Tuesday, May 01, 2012 8:51 PM
     
     Answered Has Code

    sInput == "" ? "0" : sInput

    ? is to check whether the condition (sInput == "") is true or false. If it is true the statement will return "0" else it will return sInput.

    The above code is a shorthand version of if/else condition.

    if(sInput == "" )
    {
       return "0";
    }
    else
    {
       return sInput;
    }

    Hope you understand.

    Parth



    If this post answers your question "MARK AS ANSWER" If this post helps you "VOTE AS HELPFUL"

    • Proposed As Answer by DirkStrauss Wednesday, May 02, 2012 11:51 AM
    • Marked As Answer by Lie YouModerator Friday, May 04, 2012 8:30 AM
    •  
  • Wednesday, May 02, 2012 12:54 PM
     
     Answered

     Hi 

       It's just like conditional stmts  if ...else 

          sInput == "" ? "0" : sInput

      if (sInput=="") then 0 else sInput


  • Monday, May 07, 2012 12:16 PM
     
     
    Thanku..........................................

    Ashish