Esempio n. 1
0
def infix_to_rpn(input_data):
    stack = Stack()
    temp = ""
    output = []
    dictionary = {'^': 3, '*': 2, '/': 2, '+': 1, '-': 1, '(': 0}
    for item in input_data:
        for char in item:
            if char in dictionary and char != '(':
                if stack.isEmpty():
                    stack.Push(char)
                elif dictionary.get(char) <= dictionary.get(stack.Top()):
                    temp += stack.Top()
                    stack.Pop()
                    stack.Push(char)
                elif dictionary.get(char) > dictionary.get(stack.Top()):
                    stack.Push(char)
            elif char == '(':
                stack.Push(char)
            elif char == ')':
                temp = clear_stack(stack, temp)
            elif char == '=':
                temp = clear_stack(stack, temp)
                output.append(temp)
                temp = ""
            else:
                temp += char
    return output
Esempio n. 2
0
choice = "Yes"

while choice:
    print(a)
    b = int(input('Enter your choice: '))
    if b == 1:
        c = int(input('Enter the element you want to push: '))
        stack.Push(c)
        print(stack.display())

    elif b == 2:
        if stack.Empty():
            print('Stack is empty')
        else:
            print('Popped element {} from the stack {}'.format(
                stack.Pop(), stack.display()))

    elif b == 3:
        print("The top element in the stack is {}", stack.Top())

    elif b == 4:
        if not stack.Empty():
            print('Stack is not empty')
        else:
            print('Stack is Empty')

    elif b == 5:
        print('Maximum element from the stack is {}', stack.max())
    else:
        print('Invalid choice')
    choice = input('Do you want to continue? Yes/No')
Esempio n. 3
0
from Stack import Stack

The_Stack = Stack()

The_Stack.Push('Hello')
The_Stack.Push('World')
The_Stack.Push('Hady')

print(The_Stack.Top())

print(The_Stack.Pop())
print(The_Stack.Pop())
print(The_Stack.Pop())
Esempio n. 4
0
choice = "Yes"

while choice:
    print(a)
    b = int(input('Enter your choice: '))
    if b == 1:
        c = int(input('Enter the element you want to push: '))
        stack.Push(c)
        print(stack.display())

    elif b == 2:
        if stack.Empty():
            print('Stack is empty')
        else:
            print('Popped element {} from the stack {}'.format(
                stack.Pop(), stack.display()))

    elif b == 3:
        print("The top element in the stack is {}".format(stack.Top()))

    elif b == 4:
        if not stack.Empty():
            print('Stack is not empty')
        else:
            print('Stack is Empty')

    elif b == 5:
        print('Maximum element from the stack is {}'.format(stack.max()))
    else:
        print('Invalid choice')
    choice = input('Do you want to continue? Yes/No')