• c语言实现队列


    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXQSIZE 100
    typedef int QElemType;
    typedef struct{
    	QElemType *base;
    	int front;
    	int rear;
    }Queue;
    
    void Init(Queue &Q){
    	Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
    	Q.front = Q.rear = 0;
    	printf("init success\n");
    }
    
    void Get(Queue Q){
    	for (int i=0;i<11;i++){
    		printf("data is %d\n",Q.base[i]);
    	}
    }
    
    int enQueue(Queue &Q,QElemType e){
    	if ((Q.rear+1)%MAXQSIZE==Q.front){
    		printf("pull");
    		return 0;
    	}
    	Q.base[Q.rear] = e;
    	Q.rear = (Q.rear+1)%MAXQSIZE;
    	return 1;
    }
    
    int outQueue(Queue &Q){
    	if (Q.front == Q.rear){
    		printf("empty");
    		return 0;
    	}
    	Q.base[Q.front] = 0;
    	Q.front = (Q.front+1)%MAXQSIZE;
    	return 0;
    }
    
    void Length(Queue Q){
    	int i;
    	i = (Q.rear-Q.front+MAXQSIZE) % MAXQSIZE;
    	printf("length is %d\n",i);
    }
    
    int main(){
    	Queue Q;
    	Init(Q);
    	for(int i=0;i<10;i++){
    		enQueue(Q,i+1);
    	}
    	outQueue(Q);
    	outQueue(Q);
    	enQueue(Q,2000);
    	Get(Q);
    	Length(Q);
    }
    

      

  • 相关阅读:
    Git 命令使用小笔记
    一个关于if else容易迷惑的问题
    浏览器与Node环境下的Event Loop
    镜面反射
    Socket通信原理
    Git
    vba工具
    为什么要用prototype
    Oracle 列转换为行, 逗号拼接.
    JS中的phototype
  • 原文地址:https://www.cnblogs.com/Pynu/p/16327782.html
Copyright © 2020-2023  润新知