Data structures and algorithms - Data Structure - Linear Structure - stacks and queues

 ##################################################

"""
Third, linear structure
(1) Stack
1, the definition: the stack is a collection of data, the list will be appreciated that only one end of the insertion or deletion operation.

2, the stack features: LIFO (last-in, first-out), referred to in Table LTFO
This data structure features:
It is like a cup or a magazine, elevator,
When stored from the bottom, when reading from the top, this feature is provided with the stack
That is, LIFO,

Can be stored can be achieved when the order from the list or table,
Just let go from one end of the store, and reading, to achieve the stack,

3, the stack concept:

Stack: This allows the insertion and deletion end call stack
Bottom of the stack: the other end is called the fixed bottom of the stack
Empty stack: the stack does not contain any element referred empty stack
4, the basic operation of the stack:

Push (push): push
Pop: pop
Take the stack: gettop


5, stack Python implementation
Do not need to define, you can use a list structure.
The push function: append
Stack Function: pop
See top element: li [-1]


"""

 

################## python use the list to achieve stack #######################

# ############################################ 
# stack implementation 
# use the list to achieve

class Stack(object):
    """"""

    def __init__(self):
        Self. __list = []   # private, external operation is not allowed, use the list as a container,

    DEF is_empty (Self):
         "" " determine whether the air " "" 
        return . Self __list == []   # This is the return to a judgment, if it is an empty list is returned true, otherwise it is false,

    DEF the Push (Self, Item):
         "" " added element " "" 
        . Self __list .append (Item)   # select Add at the end of the tail to add high-efficiency,

    def pop(self):
        """弹出元素"""
        return self.__list.pop()

    DEF PEEK (Self):
         "" " Returns the top element " "" 
        IF Self. __list :   # make a judgment if it is not empty list data, empty list none 
            return Self. __list [-1 ]
         the else :
             return None

    DEF size (Self):
         "" " return stack size " "" 
        return len (Self. __list )


if __name__ == "__main__":
    stack = Stack()
    stack.push("hello")
    stack.push("world")
    stack.push("itcast")
    print(stack.size())
    print(stack.peek())
    print(stack.pop())
    print(stack.pop())
    print(stack.pop())

 

 

 ##################################################

"""

(2) Queue

1 Introduction

A queue is a set of data, allowing only the insertion end of the list, delete the other end,
One end of the tail is referred to as insert (rear), the insertion operation into the team or the team called
End to delete the call-to-head (front), delete the action team called out
Deque: both ends of the column are allowed to enter and dequeue operations

2, implementation of a queue

Can a simple implementation of the queue with the list? why?
Initial ideas: List + two pointer subscript
And create a list of two variables, front variable points to the first team, rear variable points to the tail. Initially, front and rear are zero.
Into the team operation: write element li [rear] position, rear incremented by one.
Dequeue: Returns li [front] elements, front decrement 1.

"""

 

################## python using a list queue #######################

# Achieve queue
One end of the first team is the value of #, the value is stored in the tail,

class Queue(object):
    """队列"""

    def __init__(self):
        self.__list = []

    def is_empty(self):
        return self.__list == []

    def enqueue(self, item): """进队列""" self.__list.insert(0, item) def dequeue(self): """出队列""" return self.__list.pop() def size(self): """返回大小""" return len(self.__list) if __name__ == "__main__": q = Queue() q.enqueue("hello") q.enqueue("world") q.enqueue("itcast") print(q.size()) print(q.dequeue()) print(q.dequeue()) print(q.dequeue())

 

################## python will be realized with the list deque #######################

# Deque

class Deque(object):
    "" "Deque" ""

    def __init__(self):
        self.__list = []

    def is_empty(self): "" "Whether the queue is empty." ""
        return self.__list == []
 DEF add_front (Self, Item): "" "additive element in the HOL" "" Self .__ List .insert (0, Item) DEF add_rear (Self, Item): "" "additive element in the tail" "" Self. __list .append (Item) DEF pop_front (Self): "" "remove elements from the team head" "" return Self .__ List .pop (0) DEF pop_rear (Self): "" "removed from the team last element" "" return List .__ Self .pop () DEF size (Self): "" "return queue size" "" return len (Self .__ List ) IF the __name__ == "__main__" : the deque = the Deque () deque.add_front (. 1 ) the deque. add_front (2 ) deque.add_rear (. 3 ) deque.add_rear (. 4 ) Print (deque.size ()) Print (the deque.pop_front ()) print (deque.pop_front ()) print (deque.pop_rear ()) print (deque.pop_rear ())


 

##########################################

 

 

##########################################

Guess you like

Origin www.cnblogs.com/andy0816/p/12348237.html