/**************************
栈的顺序存储结构
功能代码包含:
1)栈的数据结构定义
2)栈的创建--初始化
5)往栈中插入元素
6)删除栈顶元素
注意:代码不进行debug,只实现基本功能
Author:tmw
date:2018-3-9
**************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 100
#define OVERFLOW 0
#define error -65530
/**栈的数据结构定义**/
typedef struct Sq_stack
{
int data[MAXSIZE];
int top;
}Sq_stack;
/**栈的创建--初始化**/
void initStack(Sq_stack *S)
{
S = (Sq_stack*)malloc(sizeof(Sq_stack));
if(!S)
exit(OVERFLOW);//栈空间分配失败
S->top = -1; //栈顶元素从0开始算起
}
/**插入栈顶元素e**/
bool insertElement_in_SqStack(Sq_stack *S, int e)
{
/**插入栈顶元素:判断栈是否已满**/
if( S->top == MAXSIZE-1 )
return false;
S->top++;
S->data[S->top] = e;
return true;
}
/**删除栈顶元素,并返回栈顶元素值**/
int deleteElement_from_SqStack(Sq_stack *S)
{
/**删除栈顶元素:判断栈是否为空**/
if(S->top == -1)
return error;
int e = S->data[S->top];
S->top--;
return e;
}
梦想还是要有的,万一实现了呢~~~ヾ(◍°∇°◍)ノ゙