1, Stack, LIFO, more for inversion
Python The implementation of the stack, is to List Wrap it into a class and add some methods as a basic operation of the stack.
The implementation of the stack:
classStack (object):#empty list of initialization stacks def __init__(self): Self.items= []#self.__items = [] can turn items into private properties #determine if the stack is empty defIsEmpty (self):returnLen (self.items) = =0#returns the element at the top of the stack defPeek (self):returnSelf.items[-1] #returns the size of the stack defsize (self):returnLen (self.items)#adding new elements to the stack defpush (Self,a): Self.items.append (a)#Delete the element at the top of the stack defpop (self):returnSelf.items.pop ()
Stack application Example: decimal conversion into binary
def DivideBy2 (decnumber): = Stack () #实例化一个栈, because the stack structure is required to store the data, but also to use the Stack method while decnumber > 0: = Decnumber %2 #除二倒取余法, last reversed stitching all remainder remstack.push (REM) #余数依次放到栈中储存起来= decnumber//2 "while not remstack.is_empty ():= binstring + STR (Remstack.pop ()) #反序获取栈中的元素 return binstringprint divideBy2 ( 10)
2 Queue queues
The queue is actually a packaged list, from list[0] adding new elements, using pop () to get, in line with FIFO rules.
classQueue:def __init__(self): Self.items= [] defIsEmpty (self):returnSelf.items = = [] defEnqueue (Self,item):#Add a new element item to the beginning of the queue, which is called the tail of the teamSelf.items.insert (0,item)defDequeue (self):#minus one last element, this is called the first team. returnSelf.items.pop ()defsize (self):returnLen (self.items)defShow (self):#self-added, good tracking see what's in the queue returnSelf.items
Queue Application Example: Hot potatoes
#is a team of people around the circle count, starting from 0, report m out, see who live the longest. fromPythonds.basic.queueImportQueuedefHotpotato (namelist,num): Simqueue=Queue () forNameinchnamelist:simqueue.enqueue (name)#Add the first namelist to the queue, PS: from the subscript is 0 of the position to start adding, the entire insert is completed after the sequence is reversed whileSimqueue.size () >1: forIinchRange (num):#Simqueue.enqueue (Simqueue.dequeue ())#subtract anything from the end of the list to add something to the beginning of the listSimqueue.dequeue ()#Which one is the last, the hot potato, the direct exit ? returnSimqueue.dequeue ()PrintHotpotato (['Lili','Jiajia','Dahu','Wangba','Daqing','Tamato','Potato','hehe'],3)
3 double-ended queue a bit like a list, not more than 4, linked list
Basic list implementation: #链表是环环相扣形成的序列结构, each loop first defines the self variable, and its secondary tag is the next variable. So when you visit, you can only follow the order.
classNode:def __init__(self,initdata): Self.data=InitData Self.next=NonedefGetData (self):returnSelf.datadefGetNext (self):returnSelf.nextdefSetData (self,newdata): Self.data=NewDatadefSetnext (self,newnext): Self.next= Newnext
List of unordered lists:
classunorderedlist:def __init__(self): Self.head= None#indicates that the head of the linked list does not refer to any content defIsEmpty (self):returnSelf.head = =NonedefAdd (Self,item):#The list has two attributes, one is itself, the other is next,temp = Node (item)#This sets the list itself, which is data, and the unordered list is made up of node.Temp.setnext (Self.head)#This sets the nextSelf.head = Temp#set the data parameter of the list to the header of the unordered listing---- Head defsize (self): current=Self.head Count=0 whileCurrent! =None:count= Count +1 Current=Current.getnext ()returnCountdefSearch (Self,item): current=Self.head found=False whileCurrent! = None and notfound:ifCurrent.getdata () = =Item:found=TrueElse: Current=Current.getnext ()returnfounddefRemove (Self,item): current=Self.head Previous=None found=False while notfound: #找到要删除的item以后会跳出循环, at which point Current.getdata () is the item to be deletedifCurrent.getdata () = =Item:found=TrueElse: Previous= Current Current=Current.getnext ()ifprevious = =None: #只有一种情况下, previous will be None, that is to delete the first, that is, want to delete Self.head self.head=Current.getnext ()Else: Previous.setnext (Current.getnext ()) # The original point is Previous.getdata ()--item (that is, previous.getnext (), or CURRENT.G Etdata ())--current.getnext ()
#要想删除item, then change the direction of previous to Current.getnext () so that item cannot be mixed up in the original list.
Ordered linked list:
classorderedlist:def __init__(self): Self.head=NonedefIsEmpty (self):#Same unordered list returnSelf.head = =NonedefShow (self): current=Self.head whileCurrent! =None:Printcurrent.getdata () current=Current.getnext ()def __iter__(self): current=Self.head whileCurrent! =None:yieldcurrent.getdata () current=Current.getnext ()defSize (self):#Same unordered listCurrent =Self.head Count=0 whileCurrent! =None:count+=1 Current=Current.getnext ()returnCountdefSearch (Self,item):#default list from small to largeCurrent =Self.head found=False Stop=False whileCurrent! = None and notFound and notStop:ifCurrent.getdata () = =Item:found=TrueElse: ifCurrent.getdata () >Item:stop=TrueElse: Current=Current.getnext ()returnfounddefAdd (Self,item): current=Self.head Previous=None Stop=False whileCurrent! = None and notStop#There are more than one element of the case ifCurrent.getdata () >Item:stop=TrueElse: Previous= Current Current= Current.getnext ()#don't worry about adding the element to the last case, since the list has none in it.Temp=Node (item)ifprevious = =None: #添加到链表头的情况 temp.setnext (self.head) Self.head=TempElse: Temp.setnext (current) Previous.setnext (temp)defRemove (self, item): current=Self.head Previous=None found=False while notfound:#iterate over each item, get the one you want to delete, and delete it by assigning the previous one ifCurrent.getdata () = =Item:found=TrueElse: Previous= Current Current=Current.getnext ()ifprevious = =None:#If the first item is deleted, then the second item becomes the first item, otherwise the previous is assignedSelf.head =Current.getnext ()Else: Previous.setnext (Current.getnext ())
Python basic data fabric stack stack and queuing queue