static void Main(string[] args) {//معرفی ثوابت int andis_ofint = 0, andis_ofstring = 0, andis_ofclass = 0; //معرفی ارایه جهت استک اینت int[] arrayint = null; //معرفی ارایه جهت استک استرینگ string[] arraystring = null; //ساخت کلاس و ارایه جهت استک کلاس StackClass c = new StackClass(); StackClass d = new StackClass(); StackClass[] arrayclass = null; //ساخت کلاس جهت رفتارهای مرتبط با استک اینت Stack_int stackint = new Stack_int(); arrayint = stackint.PushInt(4, ref andis_ofint, arrayint); arrayint = stackint.PushInt(7, ref andis_ofint, arrayint); arrayint = stackint.PopInt(ref andis_ofint, arrayint); int peckint = stackint.PeckInt(arrayint, andis_ofint); //ساخت کلاس جهت رفتارهای مرتبط با استک استرینگ StringClass stackstring = new StringClass(); arraystring = stackstring.Pushstring("ali", ref andis_ofstring, arraystring); arraystring = stackstring.Pushstring("reza", ref andis_ofstring, arraystring); arraystring = stackstring.Popstring(ref andis_ofstring, arraystring); string peckstring = stackstring.Peckstring(arraystring, andis_ofstring); //ساخت کلاس جهت رفتارهای مرتبط با استک کلاس ClassClass stackclass = new ClassClass(); arrayclass = stackclass.PushClass(c, ref andis_ofclass, arrayclass); arrayclass = stackclass.PushClass(d, ref andis_ofclass, arrayclass); arrayclass = stackclass.Popclass(ref andis_ofclass, arrayclass); StackClass f = stackclass.Peckclass(arrayclass, andis_ofclass); }
// entrance of the class static void Main(string[] args) { // creat a StackClass object named myStack StackClass myStack = new StackClass(19); // use while loop to show users some information about the stack and some choices for them to choose while (true) { // clear the console Console.Clear(); Console.WriteLine("\nStack MENU(size - - {0})", myStack.StackSizeSet); Console.WriteLine("1. Add an element."); Console.WriteLine("2. See the Top element."); Console.WriteLine("3. Remove top element."); Console.WriteLine("4.Display stack element."); Console.WriteLine("5. Exit"); Console.Write("Select your choice: "); //int choice accept value from user int choice = Convert.ToInt32(Console.ReadLine()); // using switch statement to deal with different choice. The expression in switch should be integral or enumerated type switch (choice) { case 1: // use Push method in StackClass class myStack.Push(Console.ReadLine()); break; // use Peek method in StackClass class case 2: Console.WriteLine("Top element is: {0}", myStack.Peek()); break; // use Pop method in StackClass class case 3: Console.WriteLine("Element removed: {0}", myStack.Pop()); break; // use Display method in StackClass class case 4: myStack.Display(); break; // use Environment.Exit method to terminate process case 5: System.Environment.Exit(0); break; } Console.ReadKey(); } }
//متد پوش public StackClass[] PushClass(StackClass input, ref int m, StackClass[] a) { m++; StackClass[] b = new StackClass[m]; if (m == 1) { b[0] = input; } else { for (int i = 0; i < (b.Length) - 1; i++) { b[i] = a[i]; } b[m - 1] = input; } return(b); }
//متد پاپ public StackClass[] Popclass(ref int m, StackClass[] a) { StackClass[] b; if (m < 1) { Console.WriteLine("stack is empty"); } else { m--; b = new StackClass[m]; for (int i = 0; i < b.Length; i++) { b[i] = a[i]; } a = b; } return(a); }
static void Main(string[] args) { int sizeofStack; Console.Write("Enter the size of Stack: "); sizeofStack = int.Parse(Console.ReadLine()); StackClass Stack = new StackClass(sizeofStack); // For Push for (;;) { if (!Stack.ISFull()) { Console.Write("Enter A Value For Stack : "); char Chartoenter = Convert.ToChar(Console.ReadLine()); Stack.push(Chartoenter); } else { Console.WriteLine("Stack is Full. Now Fetching Value to empty the Stack "); for (;;) { if (!Stack.ISEmpty()) { char value = Stack.pop(); Console.WriteLine("Value For Stack :{0} ", value); } else { break; } } Console.Read(); break; } } }
static void Main(string[] args) { StackClass stack = new StackClass(); }
public static void Main(string[] args) { int size; Console.WriteLine("Enter the size of the stack: "); size = int.Parse(Console.ReadLine()); StackClass <object> stck = new StackClass <object>(size); while (true) { Console.WriteLine("1.Push"); Console.WriteLine("2.Pop"); Console.WriteLine("3.Search (given position)"); Console.WriteLine("4.Print stack elements: "); Console.WriteLine("5.Exit"); Console.WriteLine("Enter your choice: "); int choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: { Console.WriteLine("Enter the element to push : "); object temp = Console.ReadLine(); int result = stck.push(temp); if (result != -1) { Console.WriteLine("Element has been pushed to stack!"); } else { Console.WriteLine("Stack overflow!"); } break; } case 2: { object result = stck.pop(); if (result != null) { Console.WriteLine("Deletd Element : " + result); } else { Console.WriteLine("Stack underflow!"); } break; } case 3: { Console.WriteLine("Enter the position of the element to search:"); int position = int.Parse(Console.ReadLine()); object result = stck.peep(position); if (result != null) { Console.WriteLine("Element at Position " + position + "is " + result); } else { Console.WriteLine("Entered element id Out of Stack Range"); } break; } case 4: { object[] Elements = stck.GetAllStackElements(); Console.WriteLine("*******Stack Content**********"); foreach (object obj in Elements) { Console.WriteLine(obj); } break; } case 5: { System.Diagnostics.Process.GetCurrentProcess().Kill(); break; } default: { Console.WriteLine("You have Entered wrong choice "); break; } } } }