Basic concepts of stack and queue data structures and related Python implementations

Source: Internet
Author: User
This article mainly introduces the basic concepts of stack and queue data structures and related Python implementations. the knowledge of first-in-first-out and later-in-first-out has become a common topic in computer learning: d. you can refer to the following to review the basic concepts of stack and queue:

Similarities: From the perspective of "data structure", they are all linear structures, that is, the relationship between data elements is the same.

Difference: Stack is a linear table that can only be inserted or deleted at one end of the table. A Queue is a linear table that can only be inserted at one end of the table and deleted at the other end. They are completely different data types. Apart from their different basic operation sets, the main difference is the "limitation" of the insert and delete operations ".

The stack must be operated according to the rule of "first-in-first-out". for example, if primary school teachers correct students' homework, the first assignment corrected by the teacher must be the assignment handed in by the last student. if we regard all the assignments as an element in a stack, the exercise book handed in by the last student is the top element of the stack, and the exercise book handed in by the first student is the bottom element of the stack, which is the read rule for the stack.

Queues must be operated according to the "first-in-first-out" rule. for example, when some people go to the bank to handle their business, they must first get the services in the queue, of course, he is also the first to leave the bank (assuming these people are queuing in a window ). If we think of all these waiting persons as the elements of the team, the first person is the opposite element, and the last person is the element at the end of the team. This is the reading rule of the team.


Stack is implemented using Python. this is an example of core Python programming:

#! /Usr/bin/env python # define a list to simulate stack = [] # into the stack. call the list's append () function to add it to the end of the list, strip () if no parameter is specified, the space at the beginning and end is removed. def pushit (): stack. append (raw_input ('enter new string :'). strip () # The pop () function def popit (): if len (stack) = 0: print 'cannot pop from an empty stack! 'Else: print 'removed [', stack. pop (), ']' # Edit the history stack def viewstack (): print stack # CMDs is used for dictionaries. CMDs = {'U': pushit, 'O': popit, 'V': viewstack} # pr is the prompt character def showmenu (): pr = "" p (U) sh p (O) p (v) iew (Q) uit Enter choice: "while True: try: # Remove spaces with strip () and convert the first character to choice = raw_input (pr) in lower case ). strip () [0]. lower () handle T (EOFError, KeyboardInterrupt, IndexError): choice = 'Q' print '\ nYou picked: [% s]' % choice if choice not in 'uovq ': print 'invalid option, try again 'else: break # CMDs [] returns the corresponding value from the dictionary based on the input choice. for example, if the input u is used, the value obtained from the dictionary is pushit, execute pushit () into the stack operation if choice = 'Q': break CMDs [choice] () # determine whether the entry is from this file, instead of being called if _ name _ = '_ main _': showmenu ()

Use Python to implement queues:

#! /Usr/bin/env python queue = [] def enQ (): queue. append (raw_input ('enter new string :'). strip () # Call the pop () function of the list. pop (0) is the first element in the list def deQ (): if len (queue) = 0: print 'cannot pop from an empty queue! 'Else: print 'removed [', queue. pop (0), '] 'Def viewQ (): print queue CMDs = {'e': enQ, 'D': deQ, 'V': viewQ} def showmenu (): pr = "(E) nqueue (D) equeue (V) iew (Q) uit Enter choice:" while True: try: choice = raw_input (pr ). strip () [0]. lower () handle T (EOFError, KeyboardInterrupt, IndexError): choice = '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 _': showmenu ()

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.