Algorithms for implementing queues using Python

Source: Internet
Author: User

The following is from the "Wukong" great God's explanation, listening to his video lesson, the use of Python to implement the stack algorithm, his own to do some small summary, there may be some misunderstanding of their own understanding,

1. Implementation of the Stack

the characteristics of the queue are first-in, FIFO, with the same characteristics as the queues in our Lives: First line first. There are many scenarios for queuing, such as message communication, multi-process collaboration, and multi-threaded collaboration.

Before the algorithm, we need to know 3 values 1, node (node action is to carry out a pointer (assuming) a point to 2. That is, the value of the element at this position) 2. Also need to know a head, a tail, as above two head, corresponding to the following code in the first, Tail the corresponding code below last, only to know the three content, it is good to understand if writing

The picture that goes into the queue is a new element in the back, and then a pointer to the back move

Exit the queue's picture, the exit queue is the head pointing forward, and the tail direction is unchanged

#Coding=utf-8classNode (object):#first gets to the node, the node has two functions, 1. A pointer (assuming) a point,                    #2,. is now the value of the element in this position .    def __init__(self,val): Self.next=none#the next pointer, which is used to point to the content, set the initialization to null firstSelf.val=val#Val is its original value,classQueue (object):def __init__(self):#initializes two properties, representing first and last respectivelyself.first=None Self.last=NonedefEnter (self,n):#start into queue        #Instance Noden = Node (n)#Specify in 5th position        #in the queue, you need to determine whether the queue is empty, how to judge, by judging first is not empty        ifself.first==None:self.first=n#Why is it empty and assign the value to firstself.last=Self.firstElse:#in the queue, first does not change, the last position moves backward oneSelf.last.next=n#The pointer points to a #把下移后的指向给lst at the back. NetxSelf.last=n#assign value to last    defQuit (self):#exit the queue,        #The exit queue is        ifself.first==None:returnNoneElse: tmp=self.first.val#get the value of an itemself.first=Self.first.nextreturntmpdefAllquit (self):#Exit allLists=[]#User save Data         whileself.first!=None:Lists.append (self.first.val)#deposit Each value in a listself.first=Self.first.nextreturnListsif __name__=="__main__": Q=Queue () q.enter (1) Q.enter (2) Q.enter (3)   #print q.quit ()   #print q.quit ()   #print q.quit ()    PrintQ.allquit ()

Algorithms for implementing queues using 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.