Data Structure Notes - Stack

definition

Stack (stack): It is a linear list that restricts insertion and deletion operations only at the end of the list.

The end of the stack that we allow insertion and deletion is called the top of the stack, the other end is called the bottom of the stack, and the stack without any data elements is called the empty stack.

A stack is also known as a Last In First Out (Last In First Out) linear list. It should be noted here that a stack is a special kind of linear list. The special feature of stack is that it restricts the insertion and deletion position of linear list.

The stack is a linear list, then it has a linear relationship, that is, the predecessor and successor relationship.

The bottom of the stack is fixed, and the most advanced stack can only be at the bottom of the stack.

The insertion operation of the stack is called pushing into the stack, also called pushing or pushing into the stack. The deletion operation of the stack is also called popping the stack, and some are also called popping the stack. As shown in the figure:

write picture description here

Special attention, not the most advanced stack must be the last out of the stack, such as 1 in, 1 out, 2 in, 2 out, 3 in, 3 out, 1 is advanced, 1 is also first out.

The sequential storage and chained storage of linear lists are also applicable to stacks.

Sequential storage structure of stack

Stacks are implemented through arrays. As shown in the figure:

write picture description here

top represents the position of the top element of the stack, a top value of -1 indicates an empty stack, and top=StackSize-1 indicates that the stack is full.

stack, pop

write picture description here

As can be seen from the figure, neither of them involves any loop operations, so the time complexity is O(1).

Two-stack shared controls

The stack has a big flaw, that is, the size of the array storage space must be determined in advance. If it is not enough, the array capacity needs to be expanded by programming means.

For two stacks of the same type, we can make maximum use of the storage space allocated in advance to operate.

The method is: two stacks have two bottoms, let the bottom of one stack be the beginning of the array, that is, the subscript is 0, and the bottom of the other stack is the end of the array, that is, the subscript is the length of the array n-1. In this way, if the two stacks add elements, the two endpoints extend to the middle.

write picture description here

top1 and top2 are the two stack top pointers of stack 1 and stack 2. As long as they do not meet, the two stacks can be used all the time.

Generally, when the space requirements of the two stacks have an opposite relationship, that is, one stack grows and the other stack shrinks.

Chained storage structure of stack

The chain storage structure of the stack, referred to as the chain stack.

Since the singly linked list has a head pointer, and the top of the stack pointer is also necessary, the best way is to put the top of the stack at the head of the singly linked list . As shown in the figure:

write picture description here

For the chain stack, there is basically no full stack unless the memory has no space to use.

Chain stack push and pop

Look directly at the picture:

push the stack

write picture description here

The pointer field of the new node S points to the top of the original stack, and S is assigned to the top of the stack pointer.

pop

write picture description here

The stack top node pointer points to the next bit, and the node P is released.

To sum up, if the elements change unpredictably during the use of the stack, sometimes small, sometimes very large, then it is better to use the chain stack. On the contrary, if its changes are within the controllable range, it is recommended to use the sequential stack.

Application of stack - recursion

Recursive function: We call a function that directly calls itself or indirectly calls itself through a series of calling languages, called a recursive function.

Of course, writing programs are most afraid of falling into an infinite loop. Therefore, each recursive definition must have at least one condition. When it is satisfied, the recursion will not proceed, that is, it will no longer refer to itself but return the value to exit .

So what does recursion have to do with stacks?

The order in which a recursive process goes back is the reverse of the order in which it went forward. During the rollback process, certain actions may be performed, including restoring some data stored during the forward run.

Simply put, in the forward stage, for each level of recursion, the function's local variables, parameter values, and return addresses are pushed onto the stack. During the rollback phase, the layout variables, parameter values ​​and return addresses at the top of the stack are popped to return to the rest of the executing code in the call hierarchy, that is, to restore the state of the call.

In short, the backward order of a recursive process is the reverse of the forward order, so using a stack is the best data structure.

For more exciting content, pay attention to my WeChat public account - Android Motor Vehicles !
write picture description here

Guess you like

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