一.知识都在代码里面!
二.有四个文件
1.sxb_stack.c
2.sxb_stack.h
3.main.c
4.makefile
三.代码
1.sxb_stack.h
#ifndef __SXB_STACK_H__
#define __SXB_STACK_H__
#define SIZE 1000
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
typedef struct sxb_stack
{
int data[SIZE];
int top;
}stack,*pstack;
void sxb_stack_init(stack* p); //初始化
void sxb_stack_push(stack* p,int data); //入栈
void sxb_stack_pop(stack* p); //出栈
bool sxb_stack_empty(stack *p); //判断栈空
bool sxb_stack_full(stack* p); //判断栈满
void sxb_stack_delete(stack* p); //清空栈的元素
void sxb_stack_print(stack* p); //打印栈的元素
#endif
2.sxb_stack.c
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include "sxb_stack.h"
void sxb_stack_init(stack *p) //初始化
{
if(p==NULL)
{
printf("参数传递失败!\n");
return ;
}
p->top=-1;
}
void sxb_stack_push(stack* p,int data) //入栈
{
if(p==NULL)
{
printf("参数传递失败!\n");
return ;
}
if(sxb_stack_full(p))
{
printf("栈已经满了!\n");
return ;
}
(p->top)++;
(p->data)[p->top]=data;
}
void sxb_stack_pop(stack* p) //出栈
{
if(p==NULL)
{
printf("参数传递失败!\n");
return ;
}
if(sxb_stack_empty(p))
{
printf("栈已经空了!\n");
return ;
}
--(p->top);
}
bool sxb_stack_empty(stack *p) //判断栈空
{
if(p==NULL)
{
printf("参数传递失败!\n");
return true;
}
if((p->top)==-1)
{
return true;
}
else
{
return false;
}
}
bool sxb_stack_full(stack* p) //判断栈满
{
if(p==NULL)
{
printf("参数传递失败!\n");
return false;
}
if((p->top)==SIZE-1)
{
return true;
}
else
{
return false;
}
}
void sxb_stack_print(stack* p) //打印栈的元素
{
if(p==NULL)
{
printf("参数传递失败!\n");
return ;
}
int i;
for(i=0;i<=(p->top);++i)
{
printf("%d\t ",(p->data)[i]);
}
printf("\n");
}
void sxb_stack_delete(stack* p) //清空栈的元素
{
if(p==NULL)
{
printf("参数传递失败!\n");
return ;
}
if(sxb_stack_empty(p))
{
printf("栈空了!\n");
return;
}
p->top=-1;
}
3.main.c
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include"sxb_stack.h"
int main(int argc,char *argv[])
{
stack kk;
sxb_stack_init(&kk);
for(int i=0 ;i<10;i++)
{
sxb_stack_push(&kk,i);
}
sxb_stack_print(&kk);
sxb_stack_pop(&kk);
sxb_stack_print(&kk);
sxb_stack_delete(&kk);
sxb_stack_print(&kk);
if(sxb_stack_empty(&kk))
{
printf("栈空了\n");
}
return 0;
}
makefile
stack_s: main.o sxb_stack.o
gcc -g -o stack_s main.o sxb_stack.o
main.o :main.c
gcc -g -c main.c -o main.o
sxb_stack.o: sxb_stack.c
gcc -g -c sxb_stack.c -o sxb_stack.o