python模擬隊(duì)列和堆棧(列表練習(xí))

##################################queue#########################################
#!/usr/bin/env python

'use list as a queue'

#define a void list as a void queue
queue = []

#define in queue function
def enQ():
    queue.append(raw_input('Enter New String: ').strip())

#define out queue function
def deQ():
    #judge queue whether viod
    if len(queue) == 0:
        print('Can not pop from an empty queue!')
    else:
        print('Removed' ,queue.pop(0))

#define show queue function
def viewQ():
    print(queue)

#define a dictionary to chose opration function
cmds = {'e':enQ,'d':deQ,'v':viewQ}

#define a funtion to show menu
def showMenu():
    pr = '''
    (E)nqueue
    (D)equeue
    (V)iew
    (Q)uit
    
    Enter choice:'''

    #double while circle make program always run
    while True:
        while True:
            try:
                #use to print menu information and get valid choice number(no space, just one bit , lower)
                choice = raw_input(pr).strip()[0].lower()
            except(EOFError,KeyboardInterrupt,IndexError):
                #if get a invalid value,return 'q'
                choice = 'q'
                
            print('You picked: %s '% choice)
            if choice not in 'devq':
                print('Invalid option, try again!')
            else:
                break

        if choice == 'q':
            break
        #call functions by dictionary
        cmds[choice]()

#main function
if __name__ == '__main__':
    showMenu()
    
    
 ##################################stack######################################
 #!/usr/bin/env python

'this program use list as a stack'

stack = []

def pushit():
    'input stack '
    stack.append(raw_input('Enter New Strings: ').strip())

def popit():
    'output stack'
    if len(stack) == 0:
        print('Can not pop from an empty stack!')
    else:
        print('removed [',stack.pop(),']')

def viewStack():
    print(stack)

CMDs = {'u': pushit, 'o': popit, 'v': viewStack}

def showMenu():
    pr = '''
    p(U)sh
    p(O)p
    (V)iew
    (Q)uit
    Enter Choice:
 
'''
    while True:
        while True:
            try:
                choice = raw_input(pr).strip()[0].lower()
                print(choice)
            except(EOFError.KeyboardInterrupt,IndexError):
                choice = 'q'
            print('you picked: %s ' % choice) 
            if choice not in 'uovq':
                print('Invalid option, try again')
            else:
                break

        if choice == 'q':
            break
        CMDs[choice]()

if __name__ == '__main__':
    showMenu()

網(wǎng)站題目:python模擬隊(duì)列和堆棧(列表練習(xí))
網(wǎng)站URL:http://muchs.cn/article44/gesoee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、外貿(mào)建站、服務(wù)器托管、網(wǎng)站改版微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)