zoukankan      html  css  js  c++  java
  • 顺序栈的基本操作 C语言

    #include<stdio.h>
    #include<stdlib.h>
    #define MaxSize 10
    typedef struct node* SeqStack;
    typedef char ElementType;
    struct node {
        ElementType data[MaxSize];
        int top;  //标记栈顶数据  
    };
    //初始化
    void InitNode(SeqStack *L) {
        (*L) = (SeqStack)malloc(sizeof(struct node));
        (*L)->top = -1;
    }
    //进栈
    void PushStack(SeqStack L, ElementType x) {
        if (L->top == MaxSize - 1) {
            printf("满了");
        }
        else {
            L->top++;  //入栈所以加1
            L->data[L->top] = x;
        }
    }
    //出栈
    void PopStack(SeqStack L, ElementType *x) {
        if (L->top == -1) {
            printf("空的");
        }
        else {
            *x = L->data[L->top];
            L->top--;
        }
    }
    //遍历输出
    void PrintNode(SeqStack L) {
        for (int i = 0; i <= L->top; i++) {
            printf("%c", L->data[i]);
        }
        printf("
    ");
    }
    int main() {
        SeqStack s;
        ElementType c;
        ElementType* y;
        y = &c;  //y指向c,为了出栈用
        InitNode(&s);
        printf("输入入栈数据");
        scanf("%c", &c);
        while (c != '
    ') {
            PushStack(s, c);
            scanf("%c", &c);
        }
        PrintNode(s);
        PopStack(s, y);
        printf("出栈元素是%c
    ", *y);
        PrintNode(s);
    }
  • 相关阅读:
    GRUB引导——menu.lst的写法
    条形码类型及常见条形码介绍
    Tmux:终端复用器
    find+*的问题
    find命令之exec
    Linux core 文件介绍
    C语言中返回字符串函数的四种实现方法
    C语言中的volatile
    Stars
    Game with Pearls
  • 原文地址:https://www.cnblogs.com/yudongxuan/p/7727605.html
Copyright © 2011-2022 走看看