private static string getPostFixExpressionForInfix(string inputString) { DoublyLinkedListGenericStack <char> operatorStack = new DoublyLinkedListGenericStack <char>(); string outputString = ""; for (int i = 0; i < inputString.Length; i++) { if (inputString[i] == '+' || inputString[i] == '-' || inputString[i] == '*' || inputString[i] == '/' || inputString[i] == '^' || inputString[i] == '(' || inputString[i] == '{' || inputString[i] == '[') { operatorStack.push(inputString[i]); } if ((inputString[i] >= 65 && inputString[i] <= 90) || (inputString[i] >= 97 && inputString[i] <= 122)) { outputString += inputString[i]; } if (inputString[i] == ')' || inputString[i] == '}' || inputString[i] == ']') { while (operatorStack.peek() != null && (char)operatorStack.peek() != '(' && (char)operatorStack.peek() != '{' && (char)operatorStack.peek() != '[') { outputString += operatorStack.pop(); } operatorStack.pop(); } } return(outputString); }
private static void SolveTowerOfHanoiRecursively(int n, ref DoublyLinkedListGenericStack <int> sourceStack, ref DoublyLinkedListGenericStack <int> destinationStack, ref DoublyLinkedListGenericStack <int> spareStack) { if (sourceStack.Top != null) { if (/*sourceStack.Top == sourceStack.Head && sourceStack.Top != null &&*/ n == 1) { int value = (int)sourceStack.pop(); destinationStack.push(value); } else { //while (sourceStack.Top != sourceStack.Head) //{ SolveTowerOfHanoiRecursively(n - 1, ref sourceStack, ref spareStack, ref destinationStack); //} //while (sourceStack.Top != null) //{ SolveTowerOfHanoiRecursively(1, ref sourceStack, ref destinationStack, ref spareStack); //} //while (spareStack.Top != null) //{ SolveTowerOfHanoiRecursively(n - 1, ref spareStack, ref destinationStack, ref sourceStack); //} } } }
private static void GenericDoublyLinkedListStackDemo() { DoublyLinkedListGenericStack <int> linkedListStack = new DoublyLinkedListGenericStack <int>(); int choice = 1; do { Console.Clear(); Console.WriteLine("1.Display the array\n2.Push An Element\n3.Pop an element\n4.Peek an element\n5.Exit\nPlease enter a choice"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: linkedListStack.display(); Console.ReadKey(); break; case 2: Console.WriteLine("Enter the element to push\n"); int value = Convert.ToInt32(Console.ReadLine()); linkedListStack.push(value); Console.WriteLine("Pushed successfully"); Console.ReadKey(); break; case 3: var poppedvalue = linkedListStack.pop(); if (poppedvalue == null) { Console.WriteLine("Stack Underflow"); } else { Console.WriteLine("The element popped is " + (int)poppedvalue); } Console.ReadKey(); break; case 4: var peekedvalue = linkedListStack.peek(); if (peekedvalue == null) { Console.WriteLine("Stack Underflow"); } else { Console.WriteLine("The element peeked is " + peekedvalue); } Console.ReadKey(); break; default: Console.WriteLine("Please enter a valid choice"); Console.ReadKey(); break; } } while (choice != 5); }