简单的栈操作和多文件编程

stack.h   函数的声明

#ifndef __STACK_H__
#define __STACK_H__
#define SIZE 10
static int arr[SIZE];
static int top;
void init();
void deinit();
int full();
int empty();
void push(int num);
int tp();
int size();
void pop();
#endif



stack.c  栈的操作

#include <stdio.h>
#include "stack.h"
int main()
{
	int tmp[5] = {100,31,12,24,10},num;
	for (num = 0;num < 5;num++){
	   printf("%d ",tmp[num]);
	}
	printf("\n");

	if(empty()){
	   printf("There is no elements in the stack.\n");
	}
	push(tmp[0]);
	printf("the first element is %d\n",tp());
	push(tmp[1]);
    printf("the second element is %d\n",tp());
	pop();
	printf("because it removed one, so the top element is %d\n",tp());
	while(!full()){
	    push(12);
	}
	printf("full and top is %d\n",tp());
    while(!empty()){
		printf("size is %d\n",size());
		printf("top is %d\n",tp());
	    pop();
	}

	return 0;
}


fun.c   函数的具体代码描述

#include "stack.h"
#include <string.h>
#include <stdio.h>
void init(){
     memset(arr,0,sizeof(arr));
	 top = 0;
}
void deinit(){
    memset(arr,0,sizeof(arr));
	top = 0;
}
int full(){
   if (top == 10){
   	  return 1;
   }
   return 0;   
}
int empty(){
   if(top == 0) {
	   return 1;
   }
   return 0;
}
void push(int num){
	if(full()){
		return ;
	}
	arr[top] = num;
	top++;
}
int tp(){
    return arr[top-1];
}
int size(){
    return top;
}
void pop(){
    if (empty()){
	   return ;
	}
	top--;
}
直接在linux控制台下运行gcc stack.c fun.c 即可





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值