The basic data structure ---- stack

Linear data structure

 - We began to study the data structure from four simple but important concepts. Stack, queue, deques (deque), the list is a kind of container data, the order between them is determined by the sequence of data elements added or deleted. Once a data element to be added, before and after it with respect to the position of the element remains unchanged. Like data structure is called a linear data structure.

  - linear data structure ends, sometimes referred to as left and right, back and forth is referred to in some cases. You can also be called the top and bottom, the name is not important. Separating the two regions is linear data structure element adding and removing manner, and in particular to add the position of the removed elements. For example, some additive elements from one end of the structure allows, remove elements from the other to allow the other end.

Stack

  - Concept: Stack (sometimes called a "last in, first-out stack") is an ordered collection of elements, add a new element to remove always occur at the same end. This system is usually referred to as "the top." Corresponding to the top end referred to as "bottom." The bottom of the stack is important because near the bottom of the stack elements are stored the longest. Recently added is the first element will be removed. This ordering principle is sometimes called LIFO, last in, first out. It does sorted based on the length of time in the set. Newer entries near the top, older entries near the bottom.

  - Case: very common example of the stack. Almost all have a cafeteria tray or a pile of plates, you get a from the top, there will be a new tray to the next guest. Imagine a pile of books on the table, only the top cover of the book is visible to see the cover of other books, only to remove the book above them. The following figure illustrates another stack contains a lot of Python objects.

 

Analysis and Application Stack

 - Analysis: One of the most useful ideas from observation and stack related to it. Suppose to start from a clean desktop, and now the books stacked up a book, you construct a stack. Consider removing a book what would happen. With the order to remove the order is placed just opposite. Stack is important because it can reverse the order of items. Delete inserted with the reverse order.

 - 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.

python achieve stack

- abstract data type definitions stack: the stack of abstract data types should be defined by the following structure and operation. Stack operation is as follows: 

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.

  - code implementation: Python class provides a list ordered collection mechanism and a set of methods. For example, if we have a list [2,5,3,6,7,4], we only need to determine which end of the list will be considered to be top of the stack. Once identified, such as a list of methods may be used to append and pop operation is implemented.

class Stack():
    def __init__(self):
        self.items = []
    def push(self,item):
        self.items.append(item)
    def pop(self):
        if self.isEmpty():
            return '空'
        else:
            return self.items.pop()
    def isEmpty(self):
        #空=》True
        return self.items == []
    def peek(self):
        return self.items[len(self.items) - 1]
    def size(self):
        return len(self.items)

application:

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.size())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.peek())
3
3
2
1
空

Case 2:

Stack = S () 
# Analog browser back button 
DEF getRequest (url): 
    s.push (url) 
DEF showCurrentPage (): 
    Print (s.pop ()) 
DEF the Back (): 
    return s.pop () 
getRequest ( 'www.1.com') 
getRequest ( 'www.2.com') 
getRequest ( 'www.3.com') 
showCurrentPage () 
Print (Back ()) 
Print (Back ()) 
results of: 
www.3. COM 
www.2.com 
www.1.com

  

Guess you like

Origin www.cnblogs.com/xinjie123/p/10941972.html
Recommended