顺序栈的定义和基本算法

xiaoxiao2021-02-28  14

//顺序栈的定义和基本算法 #include<stdio.h> #include<stdlib.h> #define stacksize 100 typedef char datatype; //定义顺序栈 typedef struct { datatype data[stacksize]; int top; }seqstack; //置空栈 seqstack *initstack(seqstack *s) { s = (seqstack *)malloc(sizeof(seqstack)); s->top = -1; return s; } //判断栈空 int stackempty(seqstack *s) { if (s->top == -1) return 1; else return 0; } //判断栈满 int stackfull(seqstack *s) { if (s->top == stacksize - 1) return 1; else return 0; } //进栈 int push(seqstack *s, int x) { if (stackfull(s)) { printf("error,stack overflow\n"); return 0; } s->data[++s->top] = x; return 1; } //出栈 int pop(seqstack *s, int *x) { if (stackempty(s)) { printf("error,stack underflow\n"); return 0; } *x = s->data[s->top]; s->top--; return 1; } //取栈顶元素 int stacktop(seqstack *s) { if (stackempty(s)) { printf("error, stack is empty\n"); return 0; } return (s->data[s->top]); }
转载请注明原文地址: https://www.6miu.com/read-2150213.html

最新回复(0)