Programming Topic: Two Queue implementation stacks (Python)

Source: Internet
Author: User

Feel two queue implementation stack than two stacks to implement queue trouble

1. Stack is empty: When two queues are empty, the stack is empty

2. Into the stack operation: When queue 2 is empty, the element is enqueued to queue 1, when the queue 1 bits empty, the element is enqueued to queue 2;

If both queue 1 and queue 2 are empty, select queue 1.

3. Team operation: When two queues are empty, the error "Stack is empty" is thrown;

When the queue is 2 bits empty, if there is only one element in queue 1, the elements in queue 1 are directly out of the team;

If there is more than one element in queue 1, the elements of queue 1 are then queued up to queue 2, knowing that there is only one element in queue 1, and then the elements in queue 1 will be out of the team.

When the queue is 1 bits empty, if there is only one element in queue 2, the elements in queue 2 are directly out of the team;

If there is more than one element in queue 2, the elements of queue 2 are then queued up to queue 1, knowing that there is only one element in queue 2, and then the elements in queue 2 will be out of the team.

The code is as follows:

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4 classStack (object):5         def __init__(self):6SELF.Q1 =Queue ()7SELF.Q2 =Queue ()8 9         defIs_empty (self):Tenresult = Self.q1.is_empty () andSelf.q2.is_empty () One                 returnresult A  -         defpush (self, elem): -                 ifself.q2.is_empty (): the self.q1.enqueue (elem) -                 elifself.q1.is_empty (): - self.q2.enqueue (elem) -                 Else: + self.q1.enqueue (elem) -  +         defpop (self): A                 ifSelf.q1.is_empty () andself.q2.is_empty (): at                         RaiseValueError ("Stack is Empty") -                 ifself.q2.is_empty (): -                         ifSelf.q1.head.next isNone: -                                 returnSelf.q1.dequeue () -                          while  notSelf.q1.head.next isNone: - Self.q2.enqueue (Self.q1.dequeue ()) in                         returnSelf.q1.dequeue () -                 ifself.q1.is_empty (): to                         ifSelf.q2.head.next isNone: +                                 returnSelf.q2.dequeue () -                          while  notSelf.q2.head.next isNone: the Self.q1.enqueue (Self.q2.dequeue ()) *                         returnSelf.q2.dequeue () $ Panax Notoginseng  - classNode (object): the         def __init__(Self, elem, next_=None): +Self.elem =Elem ASelf.next =Next_ the  + classQueue (object): -         def __init__(self): $Self.head =None $Self.rear =None -  -         defIs_empty (self): the                 returnSelf.head isNone - Wuyi         defEnqueue (Self, elem): the                 ifself.is_empty (): -Self.head =Node (elem) WuSelf.rear =Self.head -                 Else: AboutSelf.rear.next =Node (elem) $Self.rear =Self.rear.next -  -         defdequeue (self): -                 ifself.is_empty (): A                         RaiseValueError ("Queue is Empty") +                 ifSelf.head.next isNone: theE =Self.head.elem -Self.head =None $Self.rear =None the                         returne the                 Else: theE =Self.head.elem theSelf.head =Self.head.next -                         returne in  the         defPeek (self): the                 ifself.is_empty (): About                         RaiseValueError ("Queue is Empty") the                 returnSelf.head.elem the  the         defBianli (self): +p =Self.head -Li = [] the                  whileP:Bayi li.append (P.elem) thep =P.next the                 returnLi -  - if __name__=="__main__": thes =Stack () the          forIinchRange (5): the S.push (i) the          while  nots.is_empty (): -                 Print(S.pop ())

Programming Topic: Two Queue implementation stacks (Python)

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.