Annoying chatters data structure (II)

Talk data structure stack

Sui Suinian

In the data structure annoying chatters (a) , the reference to the stack, it can be said to be a very important concept in the computer field, we can find its use (such as in high-level languages stack<int>), we can also find in assembly language (mnemonic push, and concepts related to the stack), it will be seen even stack (e.g., x86 floating-point register structure specific) in hardware.

We're talking about the stack, it usually refers to the following two conditions:

  1. Stack (used in programming stack<int>) and its associated operations.

  2. (Not the stack pointer stack<int>in topquestion variables, but this kind of memory allocated stack overflow program runs)

There is a problem: we usually refer to the stack , in fact, heap and stack two elements. For understanding the heap, most of the talk is heap sort , for a better understanding of the underlying language, will talk about certain variables are allocated on the heap, and certain variables are allocated on the stack , and then will talk a bit deeper memory (talk like growth direction, caching, life cycles) of the heap and stack area, more in-depth ...... emmm ...... I am sorry, my limited level, not talk anymore.

Stack

Stack in the definition of the data structure is very simple: one can only linear operating table top of the stack .

This definition clearly defines the stack must be a linear table, and implicitly limits the number of top of the stack.

Some people would argue that this should be on top of the stack is not only a right? How we would discuss the number of top of the stack?

If the stack is an absolute concept (top is the top, is above the top), then the top of the stack really can say only one.

But the definition with respect to the top of the stack is in fact the bottom of the stack is, in the above stack, then just below the bottom of the stack, and vice versa.

Like I asked, two parallel lines intersect it? Almost everyone will answer subconsciously, two parallel lines never intersect, wrong? Yes. But before answering this question, we have acquiesced in this issue is discussed in Euclidean geometry system. In other non-Euclidean geometry, the answer to this question will not be the case.

The number of top of the stack

  1. Top of the stack is the number 0, can it?

The answer is no, if not specified stack (not stack), then there is no entrance operation, meaning that the stack did not exist.

Or someone might say, this is not degenerate from the stack into an ordinary linear form of? This I can not give a clear answer, because the stack itself is a linear table, but imposes a constraint , the stack only has its particularity. Without this constraint, that it can not be called a stack.

  1. Top of the stack is the number 1, the standard stack

This type of stack we first think bottom-up growth of the stack:

Bottom-up growth of the stack

Its implementation is as follows:

const int MAXSIZE = 10;
// 1. 声明一个栈
int stack[MAXSIZE];
int top = -1;

// 2. 栈空
top == -1;

// 3. 栈满
top == MAXSIZE;

// 4. 压栈push(x)
stack[top++] = x;

// 5. 弹栈pop()
int x = stack[top--];

// 6. 读取栈顶元素
int x = stack[top];
复制代码

This implementation can be said to be universal, as long as the language supports arrays, basically can achieve a simple fixed-size stack using this method.

In this implementation, we will default at the bottom of the stack is provided at the index position -1, the growth direction of the stack -1-> 10 , in turn, it is also possible provided, since the position of the bottom of the stack and the stack is relative.

NOTE : In most languages, -1, is an illegal index, using the stack [-1] will access violation error, and this error is a fatal error (fetal error), causes the program to terminate. But some languages, -1 is a legitimate index, the index represents a forward from the back, that is stack[-1] = stack[MAXSIZE-1];. 1 Index here say, is just before the first element of the array, do not attempt to access, access to a value of -1 position, it's not just the language of the provisions of the illegal operation, while in memory, -1 corresponding position may not be initialized, access the location, you may get an invalid value (if really successful visit, there usually is a sign of the value of int type), this position may have been distributed to other variables, or program, so this time the visit is successful, will make the other programs, and even the operating system crashes, irreparable losses. Of course, the compiler and virtual opportunity to examine this issue, so in the array, we can not access illegal content indexing, or even just access will be given. If the list is implemented chain stack , we need to be careful to use the pointer.

  1. The number of the stack for the stack 2, exist?

There are of course, are two stacks share the stack, respectively corresponding to the MAXSIZE position and, at the same time increase on both sides toward the middle -1.

1 and the array index 10

Share Stack

top1 = -1;
top2 = MAXSIZE;

// top1压栈做自增操作,弹栈做自减操作
// top2压栈做自减操作,弹栈做自增操作
// 栈满是top1+1 == top2;
复制代码

In memory, stack and heap is shared free space, a bit like a shared stack, we refer to "Stack" or a reason.

  1. Top of the stack is greater than the number 2, exist?

So far, we have discussed the contents are one-dimensional, then how could not find the hole open brain top of the stack is greater than 2.

And so, if forced to share the stack to expand, we really can find the top of the stack where the number is greater than 2, but this time, we should'll find a logical flaw in this article, this loophole for discussion in this section is fatal.

Fatal flaw

This article is based on the number of top of the stack in the stack when discussing the standard division, but see here, we should be able to find a logical flaw, that is, when the number of top of the stack is greater than or equal to 2, he is more than stack, rather than a stack up.

Therefore, the number is not top of the stack as the stack type of division basis, and that what is discussed in this article? Composite stack it? A bit like eh. But for us, it seems like only following such use complex:

template <class T>
class MinStack<T> {
private:
  stack<T> st;
  stack<T> min;
}

// 最小栈的算法,还是挺经典的,可以自己查一查。
复制代码

The minimum stack

to sum up

Contents of this article, the one summed up, on the stack, but also talk about two basic concepts.

Depending on the physical structure, the stack is divided into two broad categories:

  1. Stack order

Sequential storage order referred stack Stack: is a set of applications in memory contiguous storage unit storing an element of the stack, that is, the array may be achieved.

  1. Chain stack

Chain using chain called stack memory stack: the stack memory element in a discontinuous , while the address of each memory cell to reserve a small element in a storage position, it is a single linked list implementation can be considered.

And chain sequence of stack Stack

Thought, is not difficult to implement a stack, where to put a realization, the article is a bit long, so I hold code.

Eggs | ू · ω ·): What to get from this article is not to say I can count, just hope that anyone can send in this article, no one dared to send the article, who say he is very in-depth articles age, do not be seemingly very reasonable, very in-depth article misleading, to learn to think independently. Not for what they say, the person responsible has written articles, it is a nuisance.

Guess you like

Origin juejin.im/post/5d87539bf265da03d2117d3d