Python implements stacks and queues

Source: Internet
Author: User

Class Node: #节点类
Next = The none# node pointer points to the next
def __init__ (Self,data):
Self.data=data
------------------------------------------
Class Queue:
First = Node (None) #头节点
Last = Node (None) #尾节点
def inQueue (self,node): #进队列
If Self.first.data==none:
Self.first = node
Self.last = node
Else
Self.last.next = node
Self.last = node
def deQueue (self): #出队列
node = Self.first
Self.first = Self.first.next
Return node
Q = Queue () #创建队列对象
Q.inqueue (Node (1)) #进队
Q.inqueue (Node (2)) #进队
n = q.dequeue () #出队
m = Q.dequeue () #出队
Print N.data,m.data
--------------------------------

Class Stack: #栈
top = Node (None)
def add (self,node): #进栈
Node.next=self.top
Self.top = node
def out (self): #出栈
Node =self.top
While Node.data!=none:
Print Node.data,
Node= Node.next
S =stack ()
S.add (Node (1)) #进栈
S.add (Node (2)) #进栈
S.add (Node (3)) #进栈
S.out () #出栈

Python implements stacks and queues

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.