Python Basic data structure

Source: Internet
Author: User

Stack
Class Stack:    def __init__ (self):        self.items = []
def isEmpty (self): return Self.items = = [] def push (self, item): self.items.append (item) def pops (self ): return Self.items.pop () def peek (self): return Self.items[len (Self.items)-1] def size (self) : return len (self.items)
Queue
Class Queue:    def __init__ (self):        self.items = []    def isEmpty (self):        return Self.items = = []    def Enqueue (self, item):        Self.items.insert (0,item)    def dequeue (self):        return Self.items.pop ()    def size (self):        return len (self.items)
dual-ended queues
Class Deque:    def __init__ (self):        self.items = []    def isEmpty (self):        return Self.items = = []    def addfront (self, item):        self.items.append (item)    def addrear (self, item):        Self.items.insert (0,item )    def removefront (self):        return Self.items.pop ()    def removerear (self):        return Self.items.pop (0 )    def size (self):        return len (self.items)
Applicationparentheses match:
def check (s): Lefts = [' (', ' [') ', ' {']rights = [') ', '] ', '} ']stack = Stack () for C in S:if C in Lefts:stack.push (c) else:if s Tack.is_empty (): Return falsec_pop = Stack.pop () if Lefts.index (c_pop)! = Rights.index (c): Return Falseif Stack.is_empty: return Truereturn False
Binary Conversion
def divideBy2 (decnumber): Reback = Stack () while (Decnumber > 0): Reback.push (decnumber% 2) Decnumber = decnumber//2bins TR = ' While Not Reback.is_empty (): binstr = binstr + str (reback.pop ()) return binstrdef Baseconverter (Decnumber, base): ' Convert decimal number to arbitrary binary number ' digits = ' 0123456789ABCDEF ' reback = Stack () while (Decnumber > 0): Reback.push (decnumber% Base) Decnumber = decnumber//basebasestr = "While Not Reback.is_empty (): Basestr = Basestr + digits[reback.pop ()]return basest R
two stacks to implement a queue
# coding:utf-8from Pythonds.basic.stack Import Stackclass stactoqueue (object):d EF __init__ (self): Self.stack_one = Stack () Self.stack_two = Stack () def push (self, item): Self.stack_one.push (item) def pops (self): if Self.stack_two.isEmpty (): While not Self.stack_one.isEmpty (): Self.stack_two.push (Self.stack_one.pop ()) return Self.stack_two.pop () def size (self): return len (self.stack_one) + len (self.stack_two) def isEmpty (self): return self.size () = = 0if __name__ = = ' __main__ ': Queue = Stactoqueue () for X in range (5): Queue.push (x) for x in range (5):p rint (Queue.pop ())

Python Basic data structure

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.