zoukankan      html  css  js  c++  java
  • 数据结构----栈

    栈 先进后出        必须要2个方法        分别是pop 和push 方法 

    计算器 可以使用栈来实现

      

    #include<stdio.h> 
    #include<stdlib.h>
    #include<string.h>
    #define MAXSIZE 5
    struct stack{
    	int t;
    	int stack_list[MAXSIZE];
    };
    
    typedef struct stack stack;
    
    stack *create_stack(){
    	stack *mystack = NULL;
    	mystack = (stack *)malloc(sizeof(stack));
    	mystack->t = -1;
    	return mystack;
    }
    
    bool isEmpty(stack *mystack)
    {
    	if(mystack->t==-1){
    		printf("空的
    ");
    		return true;
    	}else{
    		printf("不是空的
    ");
    		return false;
    	}
    }
    
    bool isFull(stack *mystack)
    {
    	if(mystack->t==MAXSIZE-1){
    		printf("满了
    ");
    		return true;
    	}else{
    		printf("不是满的
    ");
    		return false;
    	}
    }
    
    void push(stack *mystack,int x){
    	if(isFull(mystack)){
    		printf("无法添加
    ");
    	}else{
    		mystack->t ++;
    		mystack->stack_list[mystack->t] = x;
    		printf("添加了%d
    ",x);
    	}
    	
    }
    
    int pop(stack *mystack){
    	int x; 
    	if(isEmpty(mystack)){
    		printf("无法取出
    ");
    	}else{
    		x = mystack->stack_list[mystack->t];
    		mystack->t --;
    		printf("取出了%d
    ",x);
    		return x;
    	}
    }
    
    int top(stack *mystack){
    	return mystack->stack_list[mystack->t];
    }
    
    
    int main(){
    	stack *mystack = create_stack();
    	int a,b;
    	isEmpty(mystack);
    	isFull(mystack);
    	push(mystack,1);
    	push(mystack,2);
    	pop(mystack);
    	return 0;
    }
    

      

  • 相关阅读:
    php RSA公钥私钥加解密和验证用法
    php格式化RSA公钥私钥字符串
    你的周末时光是什么样的?
    php-redis 消息订阅笔记
    php redis 常用操作
    MySql索引笔记
    yii1框架,事务使用方法
    python 项目打包成exe可执行程序
    Linux修改默认端口
    C++字符串作为参数的传递
  • 原文地址:https://www.cnblogs.com/hywhyme/p/11580632.html
Copyright © 2011-2022 走看看