Esempio n. 1
0
def nextGreaterElement(lst):
    returnArr = [-1] * len(lst)
    stack = myStack()
    for x in range(len(lst)):
        cur = lst[x]
        while stack.isEmpty() is False and lst[stack.top()] < cur:
            returnArr[stack.top()] = cur
            stack.pop()
        stack.push(x)
    return returnArr
def evaluatePostFix(exp):
    stack = myStack()
    for char in exp:
        if char.isdigit():
            stack.push(char)
        else:
            right = stack.pop()
            left = stack.pop()
            res = str(eval(left + char + right))
            stack.push(res)
    return int(float(stack.pop()))
Esempio n. 3
0
def isBalanced(exp):
    stack = myStack()
    map = {'{': '}', '[': ']', '(': ')'}
    for char in exp:
        if char in map:
            stack.push(char)
        else:
            if char == map[stack.top()]:
                stack.pop()
            else:
                return False

    return stack.size() is 0
Esempio n. 4
0
def sortStack(stack):
    other_stack = myStack()
    other_stack.push(stack.pop())
    while stack.isEmpty() is False:
        cur = stack.pop()
        if other_stack.top() is not None and cur >= int(other_stack.top()):
            other_stack.push(cur)
        else:
            while other_stack.isEmpty() is False:
                stack.push(other_stack.pop())
            other_stack.push(cur)
    while other_stack.isEmpty() is False:
        stack.push(other_stack.pop())
    return stack
def reverseK(queue, k):
    if queue.isEmpty() or k > queue.size() or k < 0:
        return None

    stack = myStack()
    size = queue.size()

    for x in range(k):
        stack.push(queue.dequeue())

    while stack.isEmpty() is False:
        queue.enqueue(stack.pop())

    for x in range(size - k):
        queue.enqueue(queue.dequeue())
    return queue
Esempio n. 6
0
 def __init__(self):
     self.orderStack = myStack()
     self.mainStack = myStack()
     return None
Esempio n. 7
0
def main():

    theStack = myStack()
    done = False
    
    print("This program will match parentheses, brackets, and braces.")
    print("The input can be anything.  All symbols in the input", end = " ")
    print("will be ignored except the parenthese, brackets, and braces.")

    print("\nEnter any expression that contains parentheses, brackets, ")
    inputString = input("and braces (empty string to quit): ")
    if (inputString == ""):
        done = True

    while (not done):

        for symbol in inputString:
            # push left-hand symbols on the stack
            if (symbol == '(' or symbol == '[' or symbol == '{'):
                theStack.push(symbol)

            # if we see a right-hand symbol, make sure the symbol
            # on the top of the stack matches. If the stack is
            # empty, then clearly they don't match

            elif(symbol == ')'):
                if (theStack.isEmpty()):
                    print ("Parentheses do not match.")
                    break

                matchingSymbol = theStack.pop()
                if (matchingSymbol != '('):
                    print ("Parentheses do not match.")
                    break

            elif(symbol == ']'):
                if (theStack.isEmpty()):
                    print ("Parentheses do not match.")
                    break

                matchingSymbol = theStack.pop()
                if (matchingSymbol != '['):
                    print ("Brackets do not match.")
                    break

            elif(symbol == '}'):
                if (theStack.isEmpty()):
                    print ("Parentheses do not match.")
                    break

                matchingSymbol = theStack.pop()
                if (matchingSymbol != '{'):
                    print ("Braces do not match.")
                    break

            # else ...  ignore all other symbols


	# Once we are done processing the input, the stack had better be
        # empty.  If not, then there was an open symbol without a closing one.

        if (not theStack.isEmpty()):
            print("symbol", theStack.peek(), "was not closed")

            # We need to empty the stack before processing the next input
            # or else we will misinterpret a non-empty stack as an error

            while (not theStack.isEmpty()):
                theStack.pop()
        else:
            print("Parenthese, brackets, and braces match.")

        print("\nEnter any expression that contains parentheses, brackets, ")
        inputString = input("and braces (empty string to quit): ")
        if (inputString == ""):
            done = True
Esempio n. 8
0
from Stack import myStack


def sortStack(stack):
    other_stack = myStack()
    other_stack.push(stack.pop())
    while stack.isEmpty() is False:
        cur = stack.pop()
        if other_stack.top() is not None and cur >= int(other_stack.top()):
            other_stack.push(cur)
        else:
            while other_stack.isEmpty() is False:
                stack.push(other_stack.pop())
            other_stack.push(cur)
    while other_stack.isEmpty() is False:
        stack.push(other_stack.pop())
    return stack


# The outer and inner loops both traverse all the n elements of the stack. Hence, the time complexity is O(n2).

stacky = myStack()
stacky.push(19)
stacky.push(9)
stacky.push(9)
stacky.push(90)

sortStack(stacky)
Esempio n. 9
0
 def __init__(self):
     # Can use size from argument to create stack
     self.mainStack = myStack()
     self.tempStack = myStack()
Esempio n. 10
0
from Stack import myStack
s = myStack()
print(s.isEmpty())
s.push(1)
s.push("two")
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
print(s.pop())
print(s.pop())
print(s.pop())
print(s.isEmpty())
print(" ")
from Queues import Queue
q = Queue()
print(q.size())
print(q.isEmpty())
q.enqueue(1)
q.enqueue(2)
print(q.dequeue())
print(" ")
from Deque import myDeque
d = myDeque()
d.addFront('1')
d.addRear('2')
print(d.size())
print(d.removeFront() + ' ' + d.removeRear())
print(d.size())
print(d.isEmpty())
 def __init__(self):
     self.mainStack = myStack()
     self.sideStack = myStack()