• 栈/队列API(push和pop)


      栈和队列主要的操作就是push和pop,栈的特点是“先入后出”,”从嘴里进进出出“。队列和实际站的队列差不多,从尾部插入,从队头出。”从后面进从嘴里出“ 嘿嘿嘿。。。

    ------------------------------------2017年9月30日11:49:33更新----------------------------------------------

    要注意的是top,head,tail都指向当前要放入的位置。

    比如入栈的时候top++是在放入以后进行的,所以当前的top指针存的是传入的key值。如果从1循环存到20的话就是top等于零的时候存的是1,top等于壹的时候存的是2

    取的时候因为指向要放入的位置上,相当于当前有数的位置的上一位处。所以取值的时候需要先往下--然后取当前位置里面的值。

    示意图:

      完整代码:

    #include <stdio.h>
    int stack[20];
    int queue[20];
    int top,head,tail;
    void push(int x){
        stack[top++] = x;
    }
    int pop(){
        return stack[--top];
    }
    void enqueue(int x){
        queue[tail++] = x;
    }
    int dequeue(){
        return queue[head++];
    }
    
    int main(){
    int temp;
    	printf("Enqueue:
    ");
    	for (int i = 0; i < 10; i++){
    		enqueue(data[i]);
    		printf("data:%d tail:%d 
    ",data[i],tail);
    	}
    	printf("Dequeue:
    ");
    	for (int j = 0; j < 10; j++){
    		temp = dequeue();
    		printf("data: %d head: %d
    ",temp,head);
    	}
    
    return 0; }
    大多数想法要么平庸,要么更糟糕,这很大程度上因为绝妙的想法难得一见,而且他们还要在我们身边这个充斥了各种恶俗的所谓常识的环境中孕育生长。
  • 相关阅读:
    Sublime Text3快捷键大全
    IntelliJ IDEA常用快捷键(Mac)
    shell脚本执行错误 $' ':command not found
    Shell脚本中"command not found"报错处理
    Shell 数值、字符串比较
    Java线程池的构造以及使用
    Host 'xxx' is not allowed to connect to this MySQL server
    Linux下Mysql安装(tar安装)
    Linux下Mysql安装(RPM安装)
    Mac安装Mysql
  • 原文地址:https://www.cnblogs.com/linux0537/p/7598396.html
Copyright © 2020-2023  润新知