Ejemplo n.º 1
0
int main()
{
    SList<int> list;
    list.PushFront(4);
    list.PushBack(5);
    list.PushFront(3);
    list.PushBack(6);
    list.PushBack(7);
    list.PushBack(8);
    list.PushBack(9);
    list.PushFront(2);
    list.Insert(1, 11);
    list.Insert(3, 13);
    list.Insert(5, 15);
    list.Insert(20, 20);

    list.Print();
    list.Delete(1);
    list.Print();
    list.Delete(11);
    list.Print();
    list.Delete(7);
    list.Print();

    SList<int> list2;
    list2.PushBack(1);
    list2.Print();
    list2.Reverse();
    list2.Print();

    SList<int> list3;
    list3.PushBack(1);
    list3.PushBack(2);
    list3.Print();
    list3.Reverse();
    list3.Print();

    SList<int> list4;
    list4.PushBack(5);
    list4.PushBack(4);
    list4.PushBack(3);
    list4.PushBack(2);
    list4.PushBack(1);
    list4.Print();
    list4.Reverse();
    list4.Print();


    return 0;
}
	//Convert infix to postfix using shunting yard
	SList<std::string> XMLParseHelperExpression::ShuntingYard(const SList<std::string>& expressionTokens)
	{

		//Stack used with conversion algorithm
		SList<std::string> stack;

		//Stores return value
		SList<std::string> output;

		for (std::string& item : expressionTokens)
		{
			if (!mLegalOperators.ContainsKey(item))//If the item is not an operator, it must be a number and is there for immediately pushed onto the stack
			{
				output.PushBack(item);
			}
			else if(item == "(")//Check for open parentheses
			{
				stack.PushFront(item);
			}
			else if (item == ")")//Check for close parentheses
			{
				while (stack.Front() != "(")//Add operators to the output until we find the other parentheses
				{
					output.PushBack(stack.Front());
					stack.PopFront();
					if (stack.Size() == 0)//There's a problem if we couldn't find the other parentheses at all
					{
						throw std::exception("Expression formed with uneven number of parentheses");
					}
				}
				stack.PopFront();//Get rid of the open parentheses
			}
			else//Otherwise, we're a normal operator. 
			{
				if (stack.Size() == 0)//We go into the stack if its empty.
				{
					stack.PushFront(item);
				}
				else//Otherwise we have to compare precedence
				{
					//Make sure our operators are actually legal
					if (!mLegalOperators.ContainsKey(stack.Front()) || !mLegalOperators.ContainsKey(item))
					{
						throw std::exception("Unknown operator put into expression");
					}

					int stackPrecedence = mLegalOperators.Find(stack.Front())->second;
					int itemPrecedence = mLegalOperators.Find(item)->second;

					if (itemPrecedence > stackPrecedence)//If the new item has greater precedence, it is put onto the stack
					{
						stack.PushFront(item);
					}
					else//If the item has less precedence or the same precedence, we put the current operator into the output and then push this item into the stack
					{
						output.PushBack(stack.Front());
						stack.PushFront(item);
					}

				}
			}
		}
		//Push the remaining operators on the stack into the output
		while (stack.Size() > 0)
		{
			output.PushBack(stack.Front());
			stack.PopFront();
		}		

		return output;

	}