头文件.h
#ifndef _SQSTACK_H
#define _SQSTACK_H
#define N 1024
typedef char sqstack_data_t;
typedef struct sqstack{
sqstack_data_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, sqstack_data_t data);
sqstack_data_t pop_sqstack(sqs_pnode S);
#endif
源文件.c
#include "sqstack.h"
#include <stdio.h>
#include <stdlib.h>
sqs_pnode create_sqstack()
{
sqs_pnode S = (sqs_pnode)malloc(sizeof(sqs_node));
if(NULL == S)
{
printf("malloc is default\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;
for(i = S->top-1; i >= 0; i--)
{
printf("buf[%d]=%c ", i, S->buf[i]);
}
puts("");
return 0;
}
int push_sqstack(sqs_pnode S, sqstk_data_t data)//入栈
{
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++] = data;
return 0;
}
sqstack_data_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函数调用
#include "sqstack.h"
#include <stdio.h>
int main()
{
sqs_pnode S = create_sqstack();
int i;
for(i = 0; i < 7; i++)
{
push_sqstack(S,i+48);
}
show_sqstack(S);
for(i = 0; i < 7; i++)
{
printf("data=%c\n", pop_sqstack(S));
}
}