python模擬隊列和堆棧(列表練習)

##################################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()

網站題目:python模擬隊列和堆棧(列表練習)
網站URL:http://m.kartarina.com/article44/gesoee.html

成都網站建設公司_創新互聯,為您提供動態網站商城網站外貿建站服務器托管網站改版微信公眾號

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

h5響應式網站建設
主站蜘蛛池模板: 久久亚洲AV成人无码电影| 不卡无码人妻一区三区音频| 亚洲AV无码一区二区三区系列| 亚洲AV无码久久寂寞少妇| 亚洲精品无码久久久久久| 亚洲AV无码成人精品区狼人影院| 亚洲国产精品无码久久九九| 十八禁无码免费网站| 亚洲av麻豆aⅴ无码电影| 99久久无码一区人妻a黑| 黄A无码片内射无码视频| 国产成人无码AV麻豆| 丰满熟妇乱又伦在线无码视频| 免费无码又爽又刺激聊天APP| 三上悠亚ssⅰn939无码播放| 无码免费午夜福利片在线| 无码少妇一区二区三区| 熟妇人妻系列av无码一区二区| 国产成人无码AV在线播放无广告| 无码国产乱人伦偷精品视频| 精品无码一级毛片免费视频观看| 亚洲AV综合色区无码一二三区| 午夜无码国产理论在线| 亚洲AV无码一区二区乱孑伦AS| 不卡无码人妻一区三区音频 | 国产V亚洲V天堂A无码| 亚洲av无码专区国产不乱码| 午夜无码国产理论在线| 无码久久精品国产亚洲Av影片 | 亚洲AV无码成人精品区在线观看| 国产成人无码精品久久久露脸 | 亚洲精品无码乱码成人| 亚洲精品无码日韩国产不卡?V| 无码被窝影院午夜看片爽爽jk | 亚洲欧洲自拍拍偷午夜色无码| 无码毛片一区二区三区中文字幕 | yy111111少妇影院里无码| 亚洲?v无码国产在丝袜线观看 | 亚洲精品午夜无码专区| 中文字幕人妻无码专区| 寂寞少妇做spa按摩无码|