Piggy's data structure auxiliary tutorials--3.1 stacks and sequential stacks in queues

Source: Internet
Author: User

Piggy's data structure auxiliary tutorials--3.1 stacks and sequential stacks in queues

tags (space delimited): Data structure

Learning Roadmap and Learning Essentials in this section

Learning Essentials

1. Stack and queue introduction, stack top, stack bottom, into the stack, the concept of the stack
2. Familiar with the characteristics of the sequential stack and the storage structure
3. Mastering the implementation logic of the basic operation of the sequential stack
4. The classic example of mastering the sequential stack: the implementation logic of the binary transformation

1. Stack and queue concepts:

Well, this sectionto is a stack + sequential structure = sequential stack !
Perhaps everyone on the concept of the stack is still very vague, we find a common thing to be materialized ~
Do not know people like to eat snacks do not--" bucket of potato chips " can be used to demonstrate the stack!

production , the process of putting a piece of potato chips into a bucket can be seen as putting elements into the stack ( into the stack ),
when eating potato chips, a piece of chips out of the process, can be seen in the stack to remove the elements (out of the stack )
Here we assume

Let's take a look at the next-to-first-out features of the stack by simulating the process of putting potato chips in the bucket and removing the chips!
Let's say the size of the potato bucket is 5, just put or take a piece of potato chips at a time!

To Place potato chips :

...
All right, the potato chips are full! Then wait for the foodie to eat ~

Take potato chips :


Well, the potato chips just finished eating it ~

The process is very simple, if still feel unable to understand, go directly to the supermarket to buy a bucket of potato chips, their own kind
Analog simulation will know! There's an excuse to buy junk food ~

Foodie tips:
Many barrels of potato chips on the market are not potato chips, but potato flour + practical starch, and the basic bag
Are all slices, when you can buy the time to see the ingredients table Oh ~

2. Terminology in the stack and conceptual analysis:

With the above bucket of potato chips in the example, I believe that the stack of some nouns, we will not confused the ~
into the stack (Push): Also known as the pressure stack, the insertion operation of the stack;
out of the stack (POP): Also known as the stack, stack delete operation;
As we have said above, stacks and queues are linear tables with limited operation, and the operation is limited by:
We can only insert and delete at the end of the table !
The table header and footer of the linear table correspond to the bottom of the stack (Bottom) and the top of the stack (top) respectively.
The top of the stack always points to the location of the new element! The bottom of the stack points to the table header element!
Another point: The maximum amount of space the stack needs to use is difficult to estimate, so the general
is to first allocate a basic capacity for the stack, in the course of use, when the stack of space is not enough to try again
Expand by paragraph!

The term is similar, next to the storage structure of the sequential stack!

3. Storage structure of the sequential stack
struct{    ElemType *base;   //栈底指针,始终指向栈底,如果为null说明栈不存在    //栈顶指针,当top == base时,为空栈;    int//当前已分配的存储空间,以元素为单位}SqStack;

In addition, Top-base equals the number of elements in the current stack!
The stack top pointer of a non-empty stack is always on the next position of the top element of the stack!

4. Code implementation of the basic operation of the sequential stack

The code is very simple, not too much explanation ~

1) Constructing an empty stack
void InitStack(SqStack S){    ->=** sizeof(SElemType));    if(!S->base)exit(ERROR);    ->=->base;  //栈顶指向栈底    ->= STACK_INIT_SIZE; }
2) Place the stack empty
void ClearStack(SqStack S){    ->=->base;  //栈顶指向栈底 
3) Determine if the stack is empty
Status StackEmpty(SqStack S){    return->==->base?TRUE:FALSE
4) Destroy Stack
void DestoryStack(SqStack S){    ->//释放栈空间    ->=->=NULL;  //将栈顶和栈底设置为NULL     ->=0;     //存储空间设置为0 
5) Get the number of elements in the stack
int StackLength(SqStack S){    return S ->top - S ->base
6) Get stack top element
Status GetTop(SqStack S,SElemType *e){    if(S ->top > S ->base)    {        e = S ->1;        return OK;    }else{        return ERROR;    }}
7) Insert elements into the stack (press stack)
voidPushstack (Sqstack S,selemtype e) {//To determine if the current stack capacity is full, full of the need to increase space    if(S -Top-S -Base==S -stacksize) {S -Base=(Selemtype*) ReAlloc (S -Base, (S -StackSize+Stack_increment)*sizeof (Selemtype));if(!S -Base) exit (ERROR); S -Top=S -Base+S -StackSize//Modify the top pointer of the stack to point to the new stack topS -StackSize+=Stack_increment;//Update capacity}*(S -Top++)=E }
8) Popup elements in the stack
 Status PopStack(SqStack S,SElemType e)  {    if->==->base)return ERROR;  //栈为空    =*(--->top);   //栈顶元素值付给e,栈顶指针下移     return OK;       }
9) Traversing the elements in the stack
void StackTraverse(SqStack S,void*visit(SElemType)) {    //从栈底开始到栈顶     while->>->base)    visit(*->++));    printf("\n"); }
5. Application example of sequential stack: binary conversion

Input conversion I believe you will not be unfamiliar, should have written such a program ~
And this section of the pig is the stack, so we must use the stack to solve the problem of the conversion of the binary!
Let's use the sequential stack to write down a simple example of decimal to a variety of binary systems ~

Operation result :

Code Implementation :

#include <Stdio.H>#defineStack_init_sizeTen Initial allocation of//storage space#defineStack_increment2 //Allocation increment#defineOk1#defineERROR0#define TRUE 1#define FALSE 0typedef int SELEMTYPE;TYPEDEF int STATUS;TYPEDEF struct sqstack{selemtype*Base//Stack bottom pointer variableSelemtype*Top//stack top pointer variableint stacksize;//maximum capacity currently available for trial}sqstack;//Initialize stackvoidInitstack (Sqstack*s) {s -Base=(Selemtype*) malloc (stack_init_size*sizeof (Selemtype));if(!S -Base) Exit (0); S -Top=S -Base S -StackSize=Stack_init_size;}//Gets the current length of the stackint Stacklength (Sqstack S) {return(S.Top-S.Base);}//into the stackvoidPushstack (Sqstack*S, Selemtype e) {if(S -Top-S -Base>=S -stacksize) {S -Base=(Selemtype*) ReAlloc (S -Base, (S -StackSize+Stack_increment)*sizeof (Selemtype));if(!S -Base) Exit (0); S -Top=S -Base+S -StackSize S -StackSize=S -StackSize+Stack_increment; }*(S -Top=E S -Top++;}//out of StackvoidPopstack (Sqstack*S, Selemtype*E) {if(S -Top==S -Base)return;*E= *--(S -top);}    int main () {sqstack s;    Selemtype n,m,e; Initstack (&s); printf"Please enter the binary to be converted: N >= 0\n"); scanf"%d",&n); printf"Please enter the decimal number to be converted: \ n"); scanf"%d",&m); while(m) {Pushstack (&S,m%n); M=M/N } printf ("Output decimal to%d binary value: \ n", n); while(Stacklength (s)) {Popstack (&S&e);if(n==  -) {printf ("%x", e);//output hexadecimal result}Elseprintf"%d", e); } printf ("\ n");return 0; }

The code is simple, is nothing more than seeking redundancy, and then the results of the remainder into the stack, anxious to divide into the number, continue to seek surplus, until equal to 0,
And then the element out of the stack ~

6. Download the example code for this section:

https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/Stack/Stack1.c
Https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/Stack/Stack2.c

Piggy's data structure auxiliary tutorials--3.1 stacks and sequential stacks in queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.