sqstack.h
#ifndef _SQSTACK_H
#define _SQSTACK_H
#define N 1024
typedef char sqs_date_t;
typedef struct sqstack
{
sqs_date_t buf[N];
int top;
}sqs_node,*sqs_pnode;
//创建
sqs_pnode create_sqstack();
//判空
int empty_sqstack(sqs_pnode S);
//判满
int full_sqstack(sqs_pnode S);
//打印
int show_sqstack(sqs_pnode S);
//入栈
int push_sqstack(sqs_pnode S,sqs_date_t date);
//出栈
sqs_date_t pop_sqstack(sqs_pnode S);
#endif
sqstack.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqstack.h"
//创建
sqs_pnode create_sqstack()
{
sqs_pnode S = (sqs_pnode)malloc(sizeof(sqs_node));
if(NULL == S)
{
printf("malloc is defaull\n");
return NULL;
}
S->top = 0;
return S;
}
//判空
int empty_sqstack(sqs_pnode S)
{
if(NULL == S)
{
printf("S is NULL\n");
return -1;
}
if(0 == S->top)
return 0;
else
return -1;
}
//判满
int full_sqstack(sqs_pnode S)
{
if(NULL == S)
{
printf("S is NULL\n");
return -1;
}
if(N-1 == S->top)
return 0;
else
return -1;
}
//打印
int show_sqstack(sqs_pnode S)
{
if(NULL == S)
{
printf("S is NULL\n");
return -1;
}
if(0 == empty_sqstack(S))
{
printf("S is empty\n");
return -1;
}
int i = 0;
for(i = S->top-1;i >= 0;i--)
{
printf("date = %c ",S->buf[i]);
}
puts("");
return 0;
}
//入栈
int push_sqstack(sqs_pnode S,sqs_date_t date)
{
if(NULL == S)
{
printf("S is NULL\n");
return -1;
}
if(0 == full_sqstack(S))
{
printf("S is full\n");
return -1;
}
S->buf[S->top++] = date;
return 0;
}
//出栈
sqs_date_t pop_sqstack(sqs_pnode S)
{
if(NULL == S)
{
printf("S is NULL\n");
return -1;
}
if(0 == empty_sqstack(S))
{
printf("S is empty\n");
return -1;
}
return S->buf[--S->top];
}
main.c
#include <stdio.h>
#include <string.h>
#include "sqstack.h"
int main(int argc, char *argv[])
{
sqs_pnode S = create_sqstack();
int i = 0;
for(i = 0;i < 8;i++)
{
push_sqstack(S,i+48);
}
show_sqstack(S);
for(i = 0;i < 8;i++)
{
printf("date = %c\n",pop_sqstack(S));
}
return 0;
}