Python Learning path 15--list implementation stacks and queues

Source: Internet
Author: User

Stack

A stack is a last-in, first-out (LIFO) data structure. You can add an object to the stack by using the push operation, or you can return and delete the top object by using a pop operation.

The following is the code for the list emulation stack:

<span style= "FONT-SIZE:14PX;" >#!/usr/bin/env python ' stack.py create a stack ' stack = []def pushit (): Stack.append (Raw_input (' Enter New string: '). Strip ()) def popit (): If Len (stack) = = 0:print ' cannot pop from an empty stack! '  Else:print ' Remove [', Repr (Stack.pop ()), '] ' def viewstack ():p rint stackcmds = {' U ': pushit, ' o ':p opit, ' V ': viewstack}def ShowMenu ():p r = "Push<span style=" White-space:pre "></span> #u represent Pushpop<span style=" White-space:pre "></span> #o represent Popview<span style=" White-space:pre "></span> #v represent Viewquit<span style= "White-space:pre" ></span> #q represent quitenter choice: ' While True:while true:try: Choice = raw_input (pr). Strip () [0].lower () except (Eoferror, Keyboardinterrupt, indexerror): choice = ' q ' print ' \nyou Picked: [%s] '% choiceif choice not in ' uovq ':p rint ' Invalid option, try again ' else:breakif choice = = ' Q ': break; Cmds[choice] () if __name__ = = ' __main__ ': ShowMenu () </span>

Here is the result of the operation:

<span style= "FONT-SIZE:14PX;"                >[email protected]:~/python> python stack.py push pop view                Quit Enter choice:vyou picked: [v][] Push pop view                Quit Enter choice:pyou picked: [p]invalid option, try again push                Pop view quit Enter choice:uyou picked: [U]enter New String:one Push pop view quit Enter choice:uyou picked: [U]enter New string:two push pop view quit Enter choice                : Uyou picked: [U]enter New string:three push pop view quit        Enter choice:vyou picked: [v][' One ', ' both ', ' three '] push pop        View quit Enter choice:oyou picked: [O]remove [' three '] push                Pop view quit Enter choice:vyou picked: [v][' One ', ' both '] Push pop view quit Enter choice:pyou picked: [P]invali d option, try again push pop view quit Enter Ch                Oice:oyou picked: [O]remove [' both '] push pop view quit                Enter choice:vyou picked: [v][' one '] push pop view Quit Enter Choice: </span>


Queue

A queue is a first in, Out (FIFO) data structure

The following is the code for the list emulation queue:

<span style= "FONT-SIZE:14PX;"        >#!/usr/bin/env pythonqueue = []def EnQ (): Queue.append (' Enter New string: '). Raw_input ()) def strip ():        If Len (queue) = = 0:print ' cannot pop from an empty queue! ' Else:print ' Removed [', repr (queue.pop (0)), '] ' Def viewq (): Print queue #calls str () in Ternailycmds = {' E ': EnQ, ' d ': DeQ, ' V ': Viewq}def showmenu (): PR = ' </span> <span style= ' fon t-size:14px; " > (E) nqueue (D) equeue (V) iew (Q) uit Enter choice: "WHI Le true:while True:try:choice = raw_input (pr). stri P () [0].lower () except (Eoferror, Keyboardinterrupt, indexerror): Cho                       Ice = ' Q ' print ' \nyou picked: [%s] '% choice if choice not in ' Devq ':         print ' Invalid option, try Again ' else:break if choice = = ' Q ': Break Cmds[choice] () if __name__ = = ' __main__ ': sh Owmenu () </span>
here is the result of the operation:

<span style= "FONT-SIZE:14PX;"                >[email protected]:~/python> python queue.py (E) nqueue (D) equeue                (V) iew (Q) uit Enter choice:eyou picked: [E]enter New String:one (e) nqueue                (D) Equeue (V) iew (Q) uit Enter choice:eyou picked: [E]enter New String:two (E) Nqueue (D) equeue (V) iew (Q) uit Enter choice:eyou Picke D: [E]enter New String:three (e) nqueue (D) equeue (V) Iew (Q) UI                T Enter Choice:vyou picked: [v][' One ', ' both ', ' three '] (E) nqueue (D) equeue                (V) iew (Q) uit Enter choice:dyou picked: [d]removed [' One '] (E) nqueue (D) Equeue (V) iew (Q) uit Enter choice:vyou picked: [v][', ' three '] (E) nqueue (D) equeue (V) iew (Q) uit Enter choice:dyou         Picked: [D]removed ['] (E) nqueue (d) equeue (V) iew (Q) uit                Enter choice:vyou picked: [v][' three '] (E) nqueue (D) equeue (v) iew (Q) uit Enter choice: </span>




Python Learning path 15--list implementation stacks and queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.