堆栈的顺序存储结构
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
#define ElementType int
typedef struct node{
ElementType data[MAXSIZE];
int top;
}Node;
基本操作
- 判空
bool isEmpty(Node* List)
{
if(List->top==-1) return true;
return false;
}
- 判满
bool isFull(Node* List)
{
if(List->top==MAXSIZE-1) return true;
return false;
}
- 入栈
void push(Node* List,ElementType value)
{
if(isFull(List)==true){
printf("栈满,不能入栈!\n");
return;
}
else{
List->top++;
List->data[List->top]=value;
}
}
- 出栈
ElementType pop(Node* List)
{
if(isEmpty(List)==true){
printf("栈空,不能出栈!\n");
return 0;
}
else{
return List->data[(List->top)--];
}
}