In-depth understanding of data structures

 

Brief

  Data structure is organized to store data, so that we can effectively access, change data. Stack, the queue is defined in the computer the first data structure. Is a LIFO stack (fixed end and the other end floating), the queue is a data storage format and organization of the FIFO.

Code

Stack code

lists = [] # define the empty stack, it can be understood as a metro (underground that only one access door)

 

lists.append ( 'a') # Stack to add data, it can be understood as a person to squeeze into the subway latest entry will be pushed to recognize the last head

lists.append('b')

lists.append('f')

lists.append('g')

 

last_out = lists.pop () #pop the last to enter the list of 'g' stack (the last person to squeeze into the subway)

print last_out

 

last_out = lists.pop () #pop entering the penultimate list 'f' stack (have just out a)

print last_out

I write to you think the code offset list represents the element has a stack, the first offset is 0

queue

fruits  =  []

 

fruits.append('apple')

fruits.append('banana')

fruits.append('oragin')

fruits.append('grapes')

 

first_out = fruits.pop(0)  #pop出‘grapes’

print fruits

 

first_out = fruits.pop(0)  #pop出‘oragin’

print fruits

Heap, stack expansion

They reached the ultimate goal is the same as the advanced configuration, the fixed end of the other end of the float. But they are slightly different implementations.

The same point: they achieve the ultimate goal is the same as the advanced configuration, the fixed end and the other end float.

difference:

1) Cache

After the stack using a cache in the storage space, call completion to free up space 

Heap using secondary cache, the statement cycle garbage collection decision 

2) release

Stack area (stack): The compiler automatically released, along with the life cycle of the thread cycle.

Heap (heap): releasing assigned by the programmer, or a programmer release released by os recovered waste.

3) storage of content

Stack: parameters for function names, local variable names (basic data types, the calling object, etc.).

Heap: storage arrays, objects (such as objects used to create the new new String ( 'asd')).

4) Performance

Stack: fast, good fixed memory size, a small degree of freedom.

Heap: slow, prone to memory fragmentation, large degree of freedom.

Guess you like

Origin www.cnblogs.com/wangdadada/p/12112554.html