zoukankan      html  css  js  c++  java
  • 堆栈

    一、堆栈的链式存储

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct Node{
        int data;
        struct Node* next;
    }; 
    typedef struct Node* Stack;
    
    int IsEmpty(Stack S);
    Stack CreateStack();
    void MakeEmpty(Stack S);
    void Push(Stack S);
    int Top(Stack S);
    void Pop(Stack S);
    int TopAndPop(Stack S); 
    
    int IsEmpty(Stack S)
    {
        return S->next==NULL; 
    }
    
    void MakeEmpty(Stack S)
    {
        if(S==NULL) printf("Must use create first!!!
    ");
        while(S!=NULL)
        {
            Pop(S);
        }
    }
    
    Stack CreateStack()
    {
        Stack S=(Stack)malloc(sizeof(struct Node));
        if(S==NULL) printf("Out of Space!!!
    ");
        S->next=NULL;
        if(!IsEmpty(S)) Pop(S);
        return S;
    }
    
    void Push(int x,Stack S)
    {
        Stack tp=(Stack)malloc(sizeof(struct Node));
        if(tp==NULL) printf("Out of Space!!!
    ");
        tp->data=x;
        tp->next=S->next;
        S->next=tp;
    }
    
    void Pop(Stack S)
    {
        Stack tp=S->next;
        S->next=tp->next;
        free(tp);
    }
    
    int Top(Stack S)
    {
        return S->next->data;
    }
    
    int main(void)
    {
        int n,i,x;
        Stack S=CreateStack(),p;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&x);
            Push(x,S);
        }
        while(!IsEmpty(S))
        {
            printf("%d ",Top(S));
            Pop(S);
        }
        return 0;
    }
    View Code

    复杂度:O(n),线性,所以效率很高,但要对内存是否合法进行判断。

    二、堆栈的顺序存储

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    const int maxn = 1200;
    const int EmptyTOS = -1;
    
    struct Node{
        int TopOfStack,Capcity;
        int *array;
    };
    typedef struct Node* Stack;
    
    int IsEmpty(Stack S);
    int IsFull(Stack S);
    Stack CreateStack(int size);
    void MakeEmpty(Stack S);
    int Top(Stack S);
    void Push(int x,Stack S);
    void Pop(Stack S);
    
    void MakeEmpty(Stack S)
    {
        S->TopOfStack=EmptyTOS;
    }
    
    int IsEmpty(Stack S)
    {
        return S->TopOfStack==EmptyTOS;
    }
    
    int IsFull(Stack S)
    {
        return S->TopOfStack==S->Capcity-1;
    }
    
    Stack CreateStack(int size)
    {
        Stack S;
        if(size>maxn)
        {
            printf("Stack is too small");
            return NULL;
        }
        S=(Stack)malloc(sizeof(struct Node));
        if(S==NULL) printf("Out of Space!!!");
        S->array=(int*)malloc(sizeof(int)*size);
        if(S->array==NULL)
        printf("Out of Space!!!
    ");
        S->Capcity=size;
        MakeEmpty(S); 
        return S;
    }
    
    void Push(int x,Stack S)
    {
        if(IsFull(S)) printf("the stack is full");
        S->array[++S->TopOfStack]=x;
    }
    
    int Top(Stack S)
    {
        if(!IsEmpty(S))
        return S->array[S->TopOfStack];
    }
    
    void Pop(Stack S)
    {
        if(IsEmpty(S)) printf("the stack if empty");
        else S->TopOfStack--;
    }
    
    int main(void)
    {
        int n,i,x;
        
        scanf("%d",&n);
        Stack S=CreateStack(n);
        while(n--)
        {
            scanf("%d",&x);
            Push(x,S);
        }
        while(!IsEmpty(S))
        {
            printf("%d ",Top(S));
            Pop(S);
        }
        return 0;
    } 
    View Code

    要注意堆栈空和满的时候并且分配内存给数组。

    三、堆栈的应用

    1、平衡符号

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    
    struct Node{
        char data;
        struct Node* next;
    };
    typedef struct Node* Stack;
    
    int IsEmpty(Stack S)
    {
        return S->next==NULL;
    }
    
    void Push(char x,Stack S)
    {
        Stack tp=(Stack)malloc(sizeof(struct Node));
        tp->data=x;
        tp->next=S->next;
        S->next=tp;
    }
    
    void Pop(Stack S)
    {
        if(IsEmpty(S)) printf("stack is empty");
        Stack tp=S->next;
        S->next=tp->next;
        free(tp);
    }
    
    char Top(Stack S)
    {
        return S->next->data;
    }
    
    void MakeEmpty(Stack S)
    {
        if(!IsEmpty(S))
        {
            Pop(S);
        }
    }
    
    Stack CreateStack()
    {
        Stack S;
        S=(Stack)malloc(sizeof(struct Node));
        if(S==NULL) printf("Out of Space!!!");
        S->next=NULL;
        MakeEmpty(S);
        return S;
    }
    
    int main(void)
    {
        int n,i,fg=0,len;
        char str[120],ch;
        Stack S=CreateStack();
        gets(str);
        len=strlen(str);
        for(i=0;i<len;i++)
        {
            if(!IsEmpty(S)) ch=Top(S);
            if(str[i]=='(') Push(str[i],S);
            else if(str[i]==')')
            {
                if(ch==')') Pop(S);
            }
            else if(str[i]=='{') Push(str[i],S);
            else if(str[i]=='}')
            {
                if(ch=='{') Pop(S);
            }
            else if(str[i]=='[') Push(str[i],S);
            else if(str[i]==']')
            {
                if(ch=='[') Pop(S);
            }
            
            if(fg) break;
        }
        if(fg) printf("No
    ");
        else printf("Yes
    ");
        return 0;
    }
    View Code

    2、中缀表达式转后缀表达式

    要求:如果表达式中带有'+' ,  '-'  ,  '*'  ,'  /'  ,  '('  ,  ')',这六种符号和0--9的实数,将他们的中缀表达式转换为后缀表达式。

    (1)如果是数字0--9,就直接输出

    (2)如果是符号‘(’,就让它直接入栈

    (3)如果是符号‘)’,就让找到‘(’,并且在找到之前输出栈中的符号(因为括号内的元素要先进行运算,并且不用考虑优先级的先后,因为之前入栈前已经考虑过了)

    (4)如果是加减乘除四种运算符号,就在栈中找到比即将进栈元素优先级小的元素(因为优先级大的元素要先进行运算)

    (5)最后如果栈不空,就将栈中的元素输出。

    代码实现:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    using namespace std;
    
    struct Node{
        char x;
        struct Node* next;
    };
    typedef struct Node* Stack;
    
    int IsEmpty(Stack S)
    {
        return S->next==NULL; 
    }
    
    void Pop(Stack S)
    {
        if(!IsEmpty(S))
        {
            Stack tp=S->next;
            S->next=tp->next;
            free(tp);
        }
    }
    
    void Push(char ch,Stack S)
    {
        Stack tp=(Stack)malloc(sizeof(struct Node));
        tp->x=ch;
        tp->next=S->next;
        S->next=tp;
    }
    
    char Top(Stack S)
    {
        if(S->next==NULL)
        {
            printf("Stack Empty");
        }
        else return S->next->x;
    }
    
    Stack CreateStack()
    {
        Stack S;
        S=(Stack)malloc(sizeof(struct Node));
        if(S==NULL)
        {
            printf("Out of Space!!!
    ");
        }
        S->next=NULL;
        return S;
    }
    
    int main(void)
    {
        Stack S=CreateStack();
        double x,sum=0;
        char ch;
        while(cin>>ch&&ch!='#')
        {
            if(ch>='0'&&ch<='9') cout<<ch;
            else if(ch=='(') Push(ch,S);
            else if(ch==')')
            {
                while(!IsEmpty(S)&&Top(S)!='(')
                {
                    cout<<Top(S);
                    Pop(S);
                }
                Pop(S);
            }
            else if(ch=='-'||ch=='+')
            {
                while(!IsEmpty(S)&&Top(S)!='(')
                {
                    cout<<Top(S);
                    Pop(S);
                }
                Push(ch,S);
            }
            else if(ch=='*'||ch=='/')
            {
                while(!IsEmpty(S)&&Top(S)!='+'&&Top(S)!='-'&&Top(S)!='(')
                {
                    cout<<Top(S);
                    Pop(S);
                }
                Push(ch,S);
            }
        }
        while(!IsEmpty(S)) 
        {
            cout<<Top(S);
            Pop(S);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    大数加法、乘法实现的简单版本
    hdu 4027 Can you answer these queries?
    zoj 1610 Count the Colors
    2018 徐州赛区网赛 G. Trace
    1495 中国好区间 尺取法
    LA 3938 动态最大连续区间 线段树
    51nod 1275 连续子段的差异
    caioj 1172 poj 2823 单调队列过渡题
    数据结构和算法题
    一个通用分页类
  • 原文地址:https://www.cnblogs.com/2018zxy/p/10033538.html
Copyright © 2011-2022 走看看