Python data structure-implementation of stacks and queues (2)

Source: Internet
Author: User
Python data structure-implementation of stacks and queues (2) 1. one list implements two stacks

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 implement one queue

There are two stacks s1 and s2. Force the element into S1. When the team leaves, judge whether s2 is empty. if not empty, the top element is displayed. if it is empty, the s1 elements are "poured" into s2 one by one, bring up and release the last element.

class Stack(object):    def __init__(self):        self.stack=[]    def isEmpty(self):        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):        return len(self.stack)class Queue_with_stacks(object):    def __init__(self):        self.stackA=Stack()        self.stackB=Stack()    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.stackA.pop()        else:            return self.stackB.pop()

3. two queues implement one 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()        self.queueB=Queue()    def isEmpty(self):        return self.queueA.isEmpty() and self.queueB.isEmpty()    def push(self,item):        if self.queueB.isEmpty():            self.queueA.enqueue(item)        else:            self.queueB.enqueue(item)    def pop(self):        if self.isEmpty():            raise IndexError,'stack is empty'        elif self.queueB.isEmpty():            while not self.queueA.isEmpty():                cur=self.queueA.dequeue()                if self.queueA.isEmpty():                    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 implementation of the Python data structure-stack and queue (2). For more articles, please follow the PHP Chinese network (www.php1.cn )!

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.