Practical application stack

Practical application stack

The latter is advanced out of the stack data structure, which I believe we are well understood. Below that I come to help you through the practical application of two stacks of work to better understand the state of the stack.


  • Number system conversion

Tsinghua University Press reference "data structure" in the description:

Converting the decimal number N and the other binary number d is the basic problem of computer implemented calculation, many solutions thereof, wherein a simple algorithm based on the following principle:
N = (N div d) MOD N * d + d (wherein, is the div Integer division, mod is the remainder operation)

E.g:   ( 1348 ) 10 \ (1348)_{10} =   ( 2504 ) 8 \ (2504)_{8}

N N div 8 N v 8
1348 168 4
168 21 0
21 2 5
2 0 2

Let's look at the code c, where a sequence of stack

#include<stdio.h>
#include<stdlib.h>

typedef struct stack
{
    int *base;
    int *top;
}Stack,*pStack;


Stack s={0};//结构体的声明


//栈的初始化
void InitStack()
{

    s.base = (int *)malloc(100 * sizeof(int));
    if(!s.base)
    {
        printf("内存申请失败!!");
        exit(0);
    }
    s.top = s.base;
    printf("栈初始化成功!!\n");

}

//push函数,接受两个参数,要往哪个栈中压入哪个值
void push(pStack s,int value)
{
    //判断栈是否溢出
    if(s->base - s->top >= 100)
    {
        printf("栈已满,数据入栈失败!");
        exit(0);
    }
    *(s->top) = value;
    s->top--;
}

//pop函数,返回栈顶的数据
int pop(pStack s)
{
    int tmp = 0;
    //判断是否是空栈
    if(s->top == s->base)
    {
        printf("当前栈为空,不能出数据");
        exit(0);
    }

    s->top++;
    tmp = *(s->top);
    return tmp;

}

//判断栈是否为空
int StackEmpty(pStack s)
{
    if(s->base == s->top )
        return 1;
    return 0;
}

//数制转换函数
void convert(int num,int scale)
{

    while(num)
    {
        push(&s,num % scale);

        num /= scale;
    }

}

void main()
{
    int num;//要转化的十进制数
    int scale;//进制

    InitStack();
    printf("请输入要转化的十进制数:\n");
    scanf("%d",&num);
    printf("您想转化为几进制呢?\n");
    scanf("%d",&scale);
    convert(num,scale);
    while(!StackEmpty(&s))
    {
        printf("%d",pop(&s));
    }
    printf("\n");
}

Next is a link stack

#include<stdio.h>
#include<stdlib.h>

typedef struct stacknode
{
    int data;                   //节点数据
    struct stacknode *next;     //节点指针
}StackNode,*pStackNode;

//栈底,栈顶方向标
typedef struct stack
{
    pStackNode base;     //栈底
    pStackNode top;      //栈顶
    int stacksize;  //栈中元素的个数
}Stack,*pStack;

int num = 1314;
int scale = 2;

//初始化一个stack的实例,填充0
Stack s = {0};

//指针指向stack实例
pStack pS = &s;


//创建头指针
pStackNode StackNodeInit()
{

    pStackNode pHeadNode;
    //头结点
    pStackNode HeadNode = (pStackNode)malloc(sizeof(StackNode));
    HeadNode->data = 0;
    HeadNode->next = NULL;
    pHeadNode = HeadNode;//头指针指向第一个节点

    return pHeadNode;//返回头指针

}

//每次Push,调用一次
pStackNode CreatList(pStack s)
{


        pStackNode pNode = (pStackNode)malloc(sizeof(StackNode));
        pNode->next = s->top;
        pNode->data = 0;
        s->top = pNode;//向右移动指针
        return s->top;


}


void StackInit(pStackNode p)//接受一个参数,类型为pStackNode
{
    s.base = p;   //栈底指向节点1
    s.top = s.base;     //栈顶也指向节点1
    s.stacksize = 0;    //栈中元素个数为0
}

void push(pStack s,int value)
{

    s->top->data = value;
    s->top = CreatList(s);

}

//出栈,返回栈顶的数据
int pop(pStack s)
{


    int value;
    pStackNode p = s->top;
    if(s->base == s->top)
    {
        printf("栈为空,无数据输出!\n");
        exit(0);
    }

    s->top = s->top->next;
    value = s->top->data;
    free(p);
    return value;

}

int StackEmpty(pStack s)
{
    if(s->base == s->top)
        return 1;
    return 0;
}
void convert(pStack s,int num,int scale)
{
    while(num)
    {
        push(s,num % scale);
        num /= scale;
    }
}

void output(pStack s)
{
    while(!StackEmpty(s))
    {
        printf("%d",pop(s));

    }
    printf("\n");
}

void Init()
{
    printf("***************************************************************************\n");
    printf("*                                                                         *\n");
    printf("*                      Welcome to use number convert tool                 *\n");
    printf("*                      This tools was programed by lingyin                *\n");
    printf("*                                 Version 1.0                             *\n");
    printf("*                                                                         *\n");
    printf("*                                                                         *\n");
    printf("***************************************************************************\n");

    printf("\n");
    printf("\n");
    printf("\n");
    printf("Please input the NUM that you want to convert:");
    scanf("%d",&num);
    printf("Please input the SCALE that you want to convret:");
    scanf("%d",&scale);


}
void main()
{
    //p为头指针
    pStackNode p = StackNodeInit();

    //栈的初始化,总指挥的初始化
    StackInit(p);
    //输出提示信息
    Init();
    //进行转换
    convert(pS,num,scale);

    //最后的输出
    output(pS);
}
  • Matching brackets

Tsinghua University Press reference or "data structure," a book to explain:

Suppose allow expression comprising two brackets, parentheses, brackets, and that the order of nesting random, i.e. ([] ()) or [([] [])] as the correct format, [(]) or ([()) or (()]) are incorrect format. Verify that the correct way to matching brackets can be described as "expected degree of urgency" concept.

For example, consider the following sequence in brackets:
[([] [])]
12345678

When the computer accepts the first brackets, it appears that matches it looked forward to the eighth brackets, but a long wait indeed the second bracket, this time the first brackets "[" only temporarily sidelined, while eagerly awaiting and second brackets to match, seventh parenthesis ")" appears. And so on.

If the left parenthesis point of view this issue is too complex. We can see right parenthesis.
Think about it, if we encounter a left parenthesis on the stack, and then met a right parenthesis, then the top of the stack and the data must match the right parenthesis. If not, it shows that the string in question.
Thought out.
Drawing on the experience left parenthesis, right parenthesis encountered match the data on the top of the stack

C See code sequence stack implementation

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 100

typedef struct Stack
{
    char * base;
    char * top;
    int stacksize;

}Stack,*pStack;

//定义一个struct Satck变量
Stack s={0};

//获得s的指针
pStack pS = &s;

void StackInit(pStack ps)
{
    char *addr;
    addr = (char *)malloc(MAX * sizeof(char));
    if(!addr)
    {
        printf("内存申请失败!");
        exit(0);
    }

    ps->base = addr;
    ps->top = ps->base;


}

void push(pStack ps,char ch)
{   
    *(ps->top) = ch;
    (ps->top)--;
}

void pop(pStack ps)
{
    (ps->top)++; 
}

char gettop(pStack ps)
{
    char * tmp = ps->top;
    tmp ++;
    return *tmp;
}

char reverse(char c)
{
    if(c == '(')
        return ')';
    if(c == '{')
        return '}';
    if(c == '[')
        return ']';
    else
        return 0;
}
void check(char *str,int length,pStack ps)
{
    int i;
    for(i = 0;i < length;i++)
    {
        if( str[i] == '(' || str[i] == '{' || str[i] == '[')    
            push(ps,str[i]);
        if(str[i] == ')' || str[i] == '}' || str[i] == ']')
        {
            if(reverse(gettop(ps)) == str[i])
                pop(ps);
            else
            {
                printf("括号不合法!\n");
                exit(0);
            }
        }
    }

    printf("您输入的字符串没有问题!\n");
}
void main()
{
    int length;
    char string[MAX]={0};
    //栈的初始化
    StackInit(pS);
    printf("Please input the string you want to check:");
    scanf("%s",string);
    length = strlen(string);
    check(string,length,pS);
    printf("\n");
}

Guess you like

Origin blog.csdn.net/weixin_37731316/article/details/91496644