順序棧的基本操作 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("\n");
}
int main() {
    SeqStack s;
    ElementType c;
    ElementType* y;
    y = &c;  //y指向c,為了出棧用
    InitNode(&s);
    printf("輸入入棧數據");
    scanf("%c", &c);
    while (c != '\n') {
        PushStack(s, c);
        scanf("%c", &c);
    }
    PrintNode(s);
    PopStack(s, y);
    printf("出棧元素是%c\n", *y);
    PrintNode(s);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM