数据结构 day08 基础知识学习 (利用数组来实现 栈)

一.知识都在代码里面!

二.有四个文件

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  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值