Data Structures and Algorithms stack queue deque

Stack

  • Features: last-out data structure
  • Top of the stack, the stack tail
  • Application: Each web browser has a back button. When you browse the Web, these pages are placed in a stack (actually the URL of the page). You now see the web page at the top, the first page you see at the bottom. If you press the 'Back' button will reverse order just browse the pages.

---------------------

  • Stack () Creates a new empty stack. It takes no arguments and returns an empty stack.
  • push (item) will add a new item to the top of the stack. It needs to do item parameter does not return anything.
  • pop () Remove top item from the stack. It takes no arguments and returns the item. Stack has been modified.
  • peek () returns the top item from the stack, but does not delete it. No arguments. Does not modify the stack.
  • Whether isEmpty () Tests if this stack is empty. No arguments and returns a Boolean value.
  • size () Returns the number of item in the stack. No arguments and returns an integer.
class Stack():
    def __init__(self):
        self.items=[]
    # 添加数据 进栈
    def push(self,item):
        self.items.append(item)
    #取出数据 出栈
    def pop(self):
        return self.items.pop()
  
    def isEmpty(self):
        return self.items==[]
def size(self):
return len(self.items)
def peek(self):
return self.items[len(self.items)-1]
def show(self):
return self.items stack=Stack() stack.push(1) stack.push(2) stack.push(3) print(stack.show()) # print(stack.pop()) # print(stack.pop()) # print(stack.peek()) print(stack.isEmpty())

Web access simulation

= S Stack () 
DEF getRequest (URL): 
    s.push (URL) 
DEF showcurrenturl (): 
    Print ( ' current page ' , s.peek ()) 
DEF Back (): 
    s.pop () 
    Print ( ' backoff click the button to display url: ' , s.peek ()) 


getRequest ( ' www.1.com ' ) 
getRequest ( ' www.2.com ' ) 
getRequest ( ' www.3.com ' ) 
getRequest ( ' the WWW. 4.com ' ) 

showcurrenturl () 
showcurrenturl () 
Back ()  
Back ()
showcurrenturl ()
back()

queue

  • Queue: FIFO
  • Scenario:
    • Our computer lab has 30 computers and a printer networking. When students want to print their print jobs with all other print job is waiting for the "consistent." The first task is to complete the entry. If you are the last one, you have to wait for you in front of all other tasks Print

 

 

  • Queue () to create a new empty queue. It takes no parameters and returns an empty queue.
  • enqueue (item) to add a new item to the tail. It requires item as an argument, it does not return anything.
  • dequeue () Removes an item from the head of the queue. It takes no arguments and returns the item. Queue is modified.
  • isEmpty () to see if the queue is empty. It takes no arguments and returns a Boolean value.
  • size () Returns the number of items in the queue. It takes no parameters and returns an integer.

 

 

class Queue():
    def __init__(self):
        self.items=[]
    def enqueue(self,item):
        self.items.insert(0,item)
    def dequeue(self):
        return self.items.pop()
    def isEmpty(self):
        return self.items==[]
    def size(self):
        return len(self.items)

    def show(self):
        return self.items


q=Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
print(q.show())

print(q.dequeue())
print(q.dequeue())

print(q.isEmpty())
print(q.size())
Results: 
[ . 3 , 2 , . 1 ]
 . 1 
2 
False 
. 1
  • Case: hot potato
    • Hot potato game: Siege of six children in a circle, specify the order of the children themselves. The first child of the hands of a hot potato, sweet potato will need timing 1 second timer is passed to the next child, and so on. Rule is that when the timer counting every 7 seconds, the hands of a potato kids out of the game. The rest of the game until the end of a child, the last remaining child wins. Please use the queue implementation strategy of the game, came in several locations will eventually win.
  • in conclusion:
    • Seven seconds after instant to, potato is passed six times.
    • Guarantee the hands of a potato kids always stand on the head of the queue

 

= Q Queue () 
Kids = [ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' ]
 for Kid in Kids: 
    q.enqueue (Kid) 
# Print (q.show ( )) 
the while q.size ()> . 1 :
     for I in Range ( . 6 ): 
        # inner loop is used to discharge the hands of a child in a team potato head 
        kid = q.dequeue ()  
        q.enqueue (kid)
    q.dequeue ()
 
Print (q.dequeue ()) 

# e Results

 

 

Deque

  • Compared with the same column, there are two head and tail. You can insert and delete data in the dual port, a single data structure characteristic stacks and queues
 
  • Deque () to create a new, empty deque. It takes no arguments and returns an empty deque.
  • addFront (item) will add a new item to the header of the deque. It takes the item parameter does not return anything.
  • addRear (item) will add a new item to the end of the deque. It takes the item parameter does not return anything.
  • removeFront () to delete the first item in the deque. It takes no arguments and returns the item. deque is modified.
  • removeRear () delete the item from the tail of the deque. It takes no arguments and returns the item. deque is modified.
  • Whether isEmpty () Test deque is empty. It takes no arguments and returns a Boolean value.
  • size () Returns the number of items in the deque. It takes no parameters and returns an integer

 

 

class the Dequeue (): 
    DEF the __init __ (Self): 
        self.items = [] 

    # insert data from the head of the queue 
    DEF addFront (Self, Item): 
        self.items.insert ( 0 , Item) 

    # inserted from the tail of the queue data 
    def addRear (Self, Item): 
        self.items.append (Item) 

    # HOL element taken 
    DEF removeFront (Self): 
        return self.items.pop () 

    # of elements having the tail 
    DEF removeRear (Self): 
        return self.items.pop ( 0 ) 

    DEF size (Self): 
        return len (self.items) 


Q = the Dequeue () 
q.addFront ( . 1  )
q.addFront (2)
q.addFront(3)

print(q.removeFront())
print(q.removeRear())

Deque Applications: palindrome inspection

  • Palindrome is a string, reading the same first and last characters, e.g., radar toot madam.
def huiwen(s):
    de=Dequeue()
    flag=True
    for i in s:
        de.addFront(i)
    while de.size()>1:
        if de.removeFront()!=de.removeRear():
            flag=Fale
    return flag
print(huiwen('abcdcba'))

 

Guess you like

Origin www.cnblogs.com/XLHIT/p/11360648.html