Python data structure--stack, queue implementation (II)

Source: Internet
Author: User
1. A list of two stacks implemented

Class Twostacks (object):    def __init__ (self):        self.stack=[]        self.a_size=0        self.b_size=0        Self.top=0    def a_isempty (self):        return self.a_size==0    def a_push (self,item):        Self.stack.insert ( Self.a_size,item)        self.a_size+=1            def a_pop (self):        if self.a_size>=1:            item=self.stack[ Self.a_size-1]            self.stack.remove (item)            self.a_size-=1            return item    def b_isempty (self):        return self.b_size==0    def b_push (self,item):        self.stack.insert (self.a_size,item)        self.b_size+=1    def b_pop (self):        if self.b_size>=1:            item=self.stack[self.a_size]            self.stack.remove (item)            self.b_size-=1            Return item

2. Two stacks to implement a queue

The

has two stacks of s1,s2. When you queue, press the element into the S1. When out of the team, determine whether S2 is empty, if not empty, the top element is directly ejected, if empty, S1 elements are "poured" S2, the last element ejected and out of the team.

class Stack (object): Def __init__ (self): self.stack=[] def isEmpty (SE LF): return self.stack==[] def push (Self,item): Self.stack.append (item) def pop (self): if self. IsEmpty (): Raise Indexerror, ' Pop from empty stack ' return Self.stack.pop () def size (self): RET Urn Len (Self.stack) class Queue_with_stacks (object): Def __init__ (self): Self.stacka=stack () self.stackb=s        Tack () def isEmpty (self): return Self.stackA.isEmpty () and Self.stackB.isEmpty () def enqueue (Self,item):                Self.stackA.push (item) def dequeue (self): if Self.stackB.isEmpty (): If Self.stackA.isEmpty ():            Raise Indexerror, ' queue is empty. ' While Self.stackA.size () >=2:self.stackb.push (Self.stackA.pop ()) return SELF.STAC Ka.pop () Else:return self.stackB.pop () 

3. Two queues implementation of a stack

Class Queue (object): Def __init__ (self): self.queue=[] def isEmpty (self): return self.queue==[] def            Enqueue (self,x): Self.queue.append (x) def dequeue (self): if self.queue:a=self.queue[0] Self.queue.remove (a) return a else:raise indexerror, ' queue is empty ' def size (self) : Return Len (Self.queue) class Stack_with_queues (object): Def __init__ (self): Self.queuea=queue () s Elf.queueb=queue () def isEmpty (self): return Self.queueA.isEmpty () and Self.queueB.isEmpty () def push (self,it EM): If Self.queueB.isEmpty (): Self.queueA.enqueue (item) Else:self.queueB.enqueue (ITE m) def pop (self): if Self.isempty (): Raise Indexerror, ' stack is empty ' elif self.queueB.isEmpt Y (): While not Self.queueA.isEmpty (): Cur=self.queuea.dequeue () if self.queueA.is                Empty ():    Return cur self.queueB.enqueue (cur) else:while not self.queueB.isEmpty ()                : Cur=self.queueb.dequeue () if Self.queueB.isEmpty (): Return cur Self.queueA.enqueue (cur)

The above is the Python data structure-stack, queue implementation (ii) of the content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.