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

来源:互联网 发布:mac出现五国怎么解决 编辑:程序博客网 时间:2024/06/07 06:54

stack.h   函数的声明

#ifndef __STACK_H__#define __STACK_H__#define SIZE 10static 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 即可





0 0