当前位置: 首页>>代码示例>>Python>>正文


Python Stack.peek方法代码示例

本文整理汇总了Python中Stack.peek方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.peek方法的具体用法?Python Stack.peek怎么用?Python Stack.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Stack的用法示例。


在下文中一共展示了Stack.peek方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: NewStack

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
class NewStack():
	def __init__(self):
		self.Stack = Stack()
		self.minStack = Stack()
		self.tempMin = None

	def isEmpty(self):
		return self.Stack.isEmpty()

	def push(self, item):
		if self.tempMin == None or item < self.tempMin:
			self.tempMin = item
		self.Stack.push(item)
		self.minStack.push(self.tempMin)

	def pop(self):
		self.minStack.pop()
		return self.Stack.pop()

	def min(self):
		return self.minStack.peek()

	def peek(self):
		return self.Stack.peek()

	def size(self):
		return self.Stack.size()
开发者ID:Jason-Yuan,项目名称:Interview-Code,代码行数:29,代码来源:Chapter3-2.py

示例2: __init__

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
class MaxStack:
     def __init__(self):
         self.base = Stack()
         self.max = Stack()
     def push(self, item):
        if item >= self.max.peek():
            self.max.push(item)
        self.base.push(item)
     def pop(self):
         val = self.base.pop()
         if (val == self.max.peek()):
             self.max.pop()
         return val
     def getmax(self):
         return self.max.peek()
开发者ID:gautamgitspace,项目名称:CTCIv5.0,代码行数:17,代码来源:largest-stack.py

示例3: infix_to_postfix

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
def infix_to_postfix(infix_expr):
	
	token_list = infix_expr.split()
	op_stack = Stack()
	postfix_list = []
	prec = {"*":3, "/":3, "+":2, "-":2, "(":1}
	
	for token in token_list:
		if token in "0123456789" or token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
			postfix_list.append(token)
		
		elif token == "(":
			op_stack.push(token)
		elif token == ")":
			top_token = op_stack.pop()
			while top_token != "(":
				postfix_list.append(top_token)
				top_token = op_stack.pop()
		else:
			while (not op_stack.is_empty()) and prec[op_stack.peek()] >= prec[token]:
				postfix_list.append(op_stack.pop())
			op_stack.push(token)
	
	while (not op_stack.is_empty()):
		postfix_list.append(op_stack.pop())
	
	return " ".join(postfix_list)

#print infix_to_postfix("A + B + C + D")
#print infix_to_postfix("( A + B ) * ( C + D ) ")
#print infix_to_postfix("A + B * C + D")
开发者ID:Xiaoke1982,项目名称:AlgorithmAndDataStructureInPython,代码行数:33,代码来源:infix_to_postfix.py

示例4: SortStack

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
def SortStack(mystack):
	HelpStack = Stack()
	while not mystack.isEmpty():
		MaxValue = mystack.pop()
		while not HelpStack.isEmpty() and HelpStack.peek() > MaxValue:
			mystack.push(HelpStack.pop())
		HelpStack.push(MaxValue)

	return HelpStack
开发者ID:Jason-Yuan,项目名称:Interview-Code,代码行数:11,代码来源:Chapter3-6.py

示例5: DFS_iterative

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
def DFS_iterative(Graph, node, nodes_list):
    global time
    stack = Stack()
    stack.push(node)
    while not stack.isEmpty():
        nodes_list[node].explore()
        nodes_list[node].set_leader(source_node)
        node = stack.peek()
        if len(Graph[node]) != 0:
            linked_node = Graph[node].pop()
            if nodes_list[linked_node].get_not_explored():
                stack.push(linked_node)
        else:
            time += 1
            nodes_list[stack.pop()].set_time(time)
开发者ID:LorenzoBi,项目名称:algorithm_design,代码行数:17,代码来源:Kosaraju_algorithm.py

示例6: MyQueue

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
class MyQueue(object):
    def __init__(self):
        self.firstStack = Stack()
        self.secondStack = Stack()
        
    def enqueue(self, item):
        self.firstStack.push(item)
        
    def dequeue(self):
        if self.secondStack.size() == 0:
            while self.firstStack.size() > 0:
                self.secondStack.push(self.firstStack.pop())
        return self.secondStack.pop()

    def peek(self):
        if self.secondStack.size() == 0:
            while self.firstStack.size() > 0:
                self.secondStack.push(self.firstStack.pop())
        return self.secondStack.peek()

    def isEmpty(self):
        return self.firstStack.size() == 0 and self.secondStack.size() == 0
开发者ID:Jason-Yuan,项目名称:Interview-Code,代码行数:24,代码来源:Chapter3-5.py

示例7: infix2postfix

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
def infix2postfix(expr):
	stck=Stack()
	pof_expr=[]
	for each in expr:
		x=typeof(each)
		if x is "lpar":
			stck.push(each)
		elif x is "rpar":
			next_item=stck.pop()
			while typeof(next_item) is not "lpar":
				pof_expr.append(next_item)
				next_item=stck.pop()
		elif x is "oprand":
			pof_expr.append(each)
		elif x is "empty":
			continue
		elif x is "oprator":
			while stck.size() is not 0 and precedence(each)<=precedence(stck.peek()):
				pof_expr.append(stck.pop())
			stck.push(each)

	while stck.size()>0:
		pof_expr.append(stck.pop())
	print ''.join(pof_expr)
开发者ID:sanchiaman,项目名称:python_github,代码行数:26,代码来源:infix2postfix.py

示例8: infixToPostfix

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
def infixToPostfix(infixexpr):
    # First set up a dictionary mapping symbols to
    # precedence.
    prec = {}
    prec["*"]= 3
    prec["/"]= 3
    prec["+"]= 2
    prec["-"]= 2
    prec["("]= 1
    # Split the input into tokens.
    tokenList = infixexpr.split()
    # We’ll need a stack and an output stream.
    opStack = Stack()
    outputList = []
    for token in tokenList:
        # Is token a number?
        if token.isdigit():
            outputList.append(token)
        elif token == "(":
            opStack.push(token)
        elif token == ")":
            topToken = opStack.pop()
            while topToken != "(":
                outputList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
                  (prec[opStack.peek()] >= prec[token]):
                outputList.append(opStack.pop())

            opStack.push(token)
                                
    while not opStack.isEmpty():
        outputList.append(opStack.pop())
    #print(" ".join(outputList))
    return " ".join(outputList)
开发者ID:keachico,项目名称:CS303E_and_CS313E,代码行数:38,代码来源:Calculator.py

示例9: infixToPostfix

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
def infixToPostfix(infixexpr):
    """Input may contain positive integer constants and the following operators:
    + (plus), - (minus), * (times), and parentheses. Use spaces to
    separate tokens in your input. That is, you should input "( 12 + 7 ) * 3"
    rather than "(12+7)*3"."""

    # First set up a dictionary mapping symbols to
    # precedence.
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    prec[""]  = 0
    
    # Split the input into tokens.
    tokenList = infixexpr.split()
    
    # We’ll need a stack and an output stream.
    opStack = Stack()
    outputList = []

    for token in tokenList:
        # Is token a number?
        if token.isdigit():
            outputList.append(token)
        elif token.isalpha():
            print("Variables are not allowed")
            return None
        elif token == "(":
            opStack.push(token)
        elif token == ")":
            if opStack.isEmpty():
                print("Ill-formed expression")
                return None
            else:
                topToken = opStack.pop()
                while topToken != "(":
                    outputList.append(topToken)
                    if opStack.isEmpty():
                        print("Ill-formed expression")
                        return None
                    else:
                        topToken = opStack.pop()
        elif token in ('+', '-', '/', '*', '('):
            while (not opStack.isEmpty()) and \
            (prec[opStack.peek()] >= prec[token]):
                if opStack.isEmpty():
                    print("Ill-formed expression")
                    return None
                else:
                    outputList.append(opStack.pop())
                            
            opStack.push(token)
        else:
            print("Ill-formed expression")
            return None
    while not opStack.isEmpty():
        outputList.append(opStack.pop())
    return " ".join(outputList)
开发者ID:Taylor4484,项目名称:CS-313E---Elements-of-Software-Design,代码行数:63,代码来源:Calculator.py

示例10: Stack

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import peek [as 别名]
import Stack as Stack

s = Stack()

print(s.isEmpty())
s.push(4)
s.push("dog")
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())
开发者ID:dksingh04,项目名称:DataStructure-And-Algorithm,代码行数:17,代码来源:testStack.py


注:本文中的Stack.peek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。