C language features sequential stacks and stacks

What is the stack?

With the order table and the linked list , as the stack is used to store a logical relationship "one" linear data storage structure , as shown in FIG.
Figure I
From the figure we see that the linear memory structure stored learned configuration prior to differ stack, which stack due to the data "storage" and the process of "taking" the special requirements:

  1. Stack can access data from one table, the other end is closed, as shown above;
  2. In the stack, whether stored data or access to data, must follow the principle of "last out", that is the most advanced elements of the final stack the stack. FIG take the stack, the data from the storage state may be judged in FIG., 1 is the most advanced element stack. Therefore, when the need to remove an element from the stack 1, according to the principle "last out", the advance elements of the element 3 and 2 removed from the stack before you can successfully remove the element 1.

Thus, we can give a definition of the stack, i.e. the stack is a data only accessible from one end of the table and follow the "last out" principle linear memory structure .

Typically, the open end is called a stack top of the stack ; accordingly, is referred to as the closed end bottom of the stack . Thus, the top element refers to the distance from the nearest stack of elements, FIG scored, the top element of the element 4; similarly, stack bottom element refers to an element located at the very bottom stack, the lower stack bottom in FIG. element to element.

1MHLQO.gif

The push and pop

Based on the characteristics of the stack structure, in practical applications, usually only performs two operations on the stack:

  • Add elements to the stack, this process is called " push " ( push or push );
  • Extracting the specified element from the stack, this process is called " popped " (or popping );

Concrete realization of the stack

A stack is a "special" linear memory structure, thus specific implementation stack following two ways:

  1. Stack order : sequential storage structure for storing data can simulate the characteristics of the stack, thereby achieving stack storage structure;
  2. Link stack : the stack implemented using Storage Structure structure;

The difference between the two implementation ways, only the relative position of the data elements stored in the actual physical space, the bottom of the stack is used in order of the array, it is used in the bottom layer link stack list. Is implementation dependent and sequential link stack Stack will be explained in detail in subsequent sections.

Basic operation sequence of stack

Stack order , i.e. with the order table implemented stack storage structure. We know that described earlier by using stack storage structure operating data elements must comply with the principle of "last out", here on "how to use the sequence table simulation stack and basic operation (the stack and stack) of the data stack" to we do some introduction.

If you look order form (underlying implementation is an array ) and the stack structure will find the way they store data highly similar, except that the stack has a special restricting access to process data, but not the sequence table.

For example, we use the first sequence table (a arrays) storage {1,2,3,4}, the storage state as shown below:

Here Insert Picture Description
Similarly, using the stack memory structure is stored {1,2,3,4} , which storage state as shown below:
Here Insert Picture Description
Comparison is easy to see from the above two graphs, the use of analog sequence table structure is very simple stack, simply a data array subscript 0 position stored sequentially It can be.

Labeled analog from the stack array to store data 0 is commonly used method, the stored data from the standard array also can other, only to assist understanding beginners.

Learn Table simulated stack storage data sequentially, the next element to see how to simulate the stack pop operation. Since the stack has "memory elements in order to stack the last out " requirement, if you want to store the elements in FIG 1 is removed from the stack, to be 4, the elements 3 and element 2 are sequentially taken out from the first element in the stack.

Here are simulated using the sequence table stored in the stack structure common implementation ideas, i.e. a real-time variable set points to the top element in the sequence table (generally designated top), top initial value is -1, nothing is stored in the stack data elements, and the stack is "empty stack." Once the data elements into the stack, the top do +1; the other hand, if the data element from the stack, do top 1 operation.

Sequence of stack elements "stack"

For example, analog or stack storage {1,2,3,4}process. Initially, the stack is "empty stack", i.e., the array is empty, top initial values -1, as shown below:
Here Insert Picture Description
First, add elements to the stack 1, our default array subscript 0 represents the bottom end of the stack, and therefore, 1 is stored in the element [1] of the array a, while the top value of +1, as shown below:
Here Insert Picture Description
in the above way, the elements 2, 3 and 4 are sequentially stored in the final, top value becomes 3, as shown below:
Here Insert Picture Description

Therefore, C language code:

//元素elem进栈,a为数组,top值为当前栈的栈顶位置
int push(int* a,int top,int elem){
    a[++top]=elem;
    return top;
}

Code a [++ top] = elem, equivalent to the first implementation ++ top, then perform a [top] = elem.

Sequence of stack elements "pop"

In fact, the top set of variables on the "stack" analog data operation is not practical help, which is to achieve data "pop" operations to prepare.

For example, the two elements of the figure above the stack, it is necessary first element 4 and the element 3 are sequentially stack. Note that, when the stack data, to do the top 1 operation. Thus, the element 4 and the element 3 are below the stack process a) and a lower panel b) below:
Here Insert Picture Description

Note that, in FIG disappear elements in the array is easy for beginners to learn, in fact, there only needs to be done on the top value to 1 operation only, since top stack value itself represents the position of the stack, and therefore the equivalent of top-1 the top element in the stack. And when the latter elements are added to the stack, so the old elements in a similar position on the 4 elements, the elements of the new elements will overwrite the old store.

After the 4 elements and all elements of the stack 3, element 2 to the stack. Thus, using simulated data sequence table illustrating the operation of the stack implemented in C language code:

//数据元素出栈
int pop(int * a,int top){
    if (top==-1) {
        printf("空栈");
        return -1;
    }
    printf("弹栈元素:%d\n",a[top]);
    top--;
    return top;
}

Code if the statement is to prevent users from doing the wrong operation "is no longer stack data but also the data stack" of. Code, about the elements of the stack pop operation is achieved, only a value of -1 to top.

Those are the times to share in order to achieve a simple stack using the C language and to introduce the characteristics of the stack, the complete code has been uploaded to GitHub, C language sequence of stack Welcome to Star! Want to learn about other relevant data structure algorithms achieve little friends can come to my blog MyBlog , we communicate together and progress together ah!

Published 19 original articles · won praise 8 · views 610

Guess you like

Origin blog.csdn.net/qq_43336390/article/details/104088918