zoukankan      html  css  js  c++  java
  • 18、顺序栈

    Stack.h

    #ifndef STACK_H
    #define STACK_H
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAXSIZE 100
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    
    typedef int SElemType;
    typedef int Status;
    
    typedef struct {
        SElemType data[MAXSIZE];
        int top;//栈顶指针
    }SqStack;
    
    //初始化操作,建立一个空栈
    void InitStack(SqStack *s);
    //将栈清空
    void ClearStack(SqStack *s);
    //若栈为空,返回TRUE,否则返回false
    Status IsEmpty(SqStack *s);
    //若栈存在且非空,用e返回S的栈顶元素
    void GetTop(SqStack s, SElemType *e);
    //插入元素e为新的栈顶元素
    Status Push(SqStack *s, SElemType e);
    //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
    Status Pop(SqStack *s, SElemType *e);
    //返回栈S的元素个数
    Status StackLength(SqStack s);
    
    //打印
    void PrintfStack(SqStack *s);
    
    #endif

    Stack.c

    #include"Stack.h"
    
    
    
    //初始化操作,建立一个空栈
    void InitStack(SqStack *s) {
        s->top = -1;
    }
    //将栈清空
    void ClearStack(SqStack *s) {
        if (s->top == -1) {
            return;
        }
        s->top = -1;
    }
    //若栈为空,返回TRUE,否则返回false
    Status IsEmpty(SqStack *s) {
        if (s->top == -1) {
            return TRUE;
        }
        return FALSE;
    }
    //若栈存在且非空,用e返回S的栈顶元素
    void GetTop(SqStack s, SElemType *e) {
        if (s.top == -1) {
            return ;
        }
        *e = s.data[s.top];
    }
    //进栈:插入元素e为新的栈顶元素
    Status Push(SqStack *s, SElemType e) {
        if (s->top == MAXSIZE - 1) {
            return ERROR;
        }
        if (e == NULL) {
            return ERROR;
        }
        s->top++;//栈顶指针加一
        s->data[s->top] = e; //将新插入元素赋值给栈顶空间
        return OK;         
    }
    //出栈:若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
    Status Pop(SqStack *s, SElemType *e) {
        if (IsEmpty(s)) {
            return ERROR;
        }
        *e = s->data[s->top];//将要删除的栈顶元素赋值给e
        s->top--;
        return OK;
    }
    //返回栈S的元素个数
    int StackLength(SqStack s) {
        int j = s.top+1;//栈从零开始,所以要加一
        return j;
    }
    
    //打印
    void PrintfStack(SqStack *s) {
        int i, k;
        k = s->top;
        for (i = 0; i < StackLength(*s); i++) {
            printf("%d ", s->data[k]);
            k--;
        }
        printf("
    ");
    }

    main.c

    /*
    栈:先进后出或后进先出    简称LIFO结构(last In first out)
    
    */
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include"Stack.h"
    
    
    
    int main()
    {
        SqStack a;
        SElemType e;
        int m[] = {13,24,45,46,68,78,98};
        int n = 7;
        //初始化
        InitStack(&a);
        //入栈
        for (int i = 0; i < n; i++) {
            Push(&a, m[i]);
        }
    
        //打印
        printf("栈的长度为:%d 
    ", StackLength(a));
        PrintfStack(&a);
    
        //出栈
        Pop(&a, &e);
        printf("栈的长度为:%d 
    ", a.top + 1);
        PrintfStack(&a);
        printf("出栈的值为:%d 
    ", e);
        //得到栈顶的值
        GetTop(a, &e);
        printf("栈顶的值为:%d 
    ", e);
        //将栈清空
        ClearStack(&a);
        PrintfStack(&a);//输出没有东西
    
        printf("%d 
    ", a.top);//top=-1,栈为空栈
    
        printf("
    ");
        system("pause");
        return 0;
    }

    VS2015运行结果:

  • 相关阅读:
    HDU 1075 What Are You Talking About(字典树)
    HDU 1075 What Are You Talking About (stl之map映射)
    HDU 1247 Hat’s Words(字典树活用)
    字典树HihoCoder
    HDU 1277全文检索(字典树)
    HDU 3294 Girls' research(manachar模板题)
    HDU 3294 Girls' research(manachar模板题)
    HDU 4763 Theme Section(KMP灵活应用)
    Ordering Tasks UVA
    Abbott's Revenge UVA
  • 原文地址:https://www.cnblogs.com/luanxin/p/8995926.html
Copyright © 2011-2022 走看看