sequential stack

One: Basic Concepts

1. The function of the stack: a change from a sequence of data elements to another sequence of data elements.
2. Features: FIFO
3. Other storage forms of stack: chain stack, shared stack
4. Stack memory:

  • The stack memory is first of all a memory area, which stores all local variables. All the variables defined in the method are local variables (the ones outside the method are global variables), and the for loop defines local variables. The function is loaded first before the local variable can be executed. The definition of variables, so the method goes to the stack first, and then defines the variable. The variable has its own scope. Once it leaves the scope, the variable will be released. The update speed of stack memory is very fast, because the life cycle of local variables is very short.
Two: the storage representation method of the stack

1. Sequential stack (sequential storage structure)

  • A group of storage units with consecutive addresses are used to store data elements from the bottom of the stack to the top of the stack in turn, and a pointer top is attached to indicate the position of the top element of the stack in the sequential stack. (top = 0 means empty stack)

2. Design ideas of stack

  • The empty stack is initialized in the design process by first allocating a basic capacity for the stack, and then gradually expanding it during the application process. So two constants can be set for this
STACK_INIT_SIZE  (存储空间初始分配量) 
STACKINCREMENT  (存储空间分配增量)
  • initialize stack

    The first storage allocation is performed according to the set initial allocation amount. The base is the pointer to the bottom of the stack. In the sequential stack, it always points to the position of the bottom of the stack. If the value of base is NULL, it indicates that the stack structure does not exist. Top is called the top pointer of the stack, and its initial value points to the bottom of the stack, that is, top=base means the stack is empty. When a new stack top element is inserted, top is incremented by 1; when a stack top element is deleted, the pointer top is decremented by 1, so the stack top pointer in a non-empty stack is always at the next position of the stack top element (in reverse, The top of the stack pointer points to the position above the top element of the stack, and in terms of storage structure, the top of the stack pointer points to the position of the next element in the storage location of the element).

  • The relationship between the top of the stack pointer and the elements in the stack
    write picture description here

3. The difference between heap and stack

1. The stack memory stores local variables and the heap memory stores entities;

2. The update speed of stack memory is faster than that of heap memory, because the life cycle of local variables is very short;

3. The variables stored in the stack memory will be released once the life cycle ends, and the entities stored in the heap memory will be recycled from time to time by the garbage collection mechanism.

4. Heap memory:

Arrays and objects are stored (in fact, arrays are objects), all new creations are in the heap, and the heap stores all entities (objects), and entities are used to encapsulate data, and they encapsulate multiple (multiple entities) attribute), if a data disappears, the entity does not disappear and can still be used, so the heap will not be released at any time, but the stack is different, the stack stores all single variables, and the variable is released, then there is no more . Although the entities in the heap will not be released, they will be treated as garbage. Java has a garbage collection mechanism to collect them from time to time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728755&siteId=291194637