• 循环队列


    #include "stdafx.h"
    
    #include <iostream>
    using namespace std;
    
    typedef int DataType;
    #define MAXSIZE 5
    
    typedef struct{
    	DataType *base;
    	int front;
    	int rear;
    }CircleQueue;
    
    CircleQueue *init(CircleQueue *queue)
    {
    	queue->base = (DataType*)malloc(MAXSIZE*sizeof(DataType));
    	if(!queue) exit(0);
    	queue->rear = queue->front =0;
    	return queue;
    }
    
    int getLength(CircleQueue *queue)
    {
    	return (queue->rear - queue->front +MAXSIZE)%MAXSIZE;
    }
    
    void inQueue(CircleQueue *queue,DataType e)
    {
    	if((queue->rear+1)%MAXSIZE ==queue->front)
    	{
    		cout<<"the queue is full"<<endl;
    		exit(0);
    	}else
    	{
    		queue->base[queue->rear] = e;
    		queue->rear = (queue->rear+1)%MAXSIZE;
    	}
    }
    
    void outQueue(CircleQueue *queue)
    {
    	if(queue->front == queue->rear)
    	{
    		cout<<"the queue is empty"<<endl;
    		exit(0);
    	}else
    	{
    		cout<<"the out element is :"<<queue->base[queue->front];
    		queue->front= (queue->front+1)%MAXSIZE;
    	}
    }
    
    void main()
    {
    	CircleQueue Q;
    	Q = *init(&Q);
    	cout<<"after init queue length:"<<getLength(&Q)<<endl;
    	inQueue(&Q,1);
    	cout<<"after input queue length:"<<getLength(&Q)<<endl;
    	inQueue(&Q,2);
    	cout<<"after input queue length:"<<getLength(&Q)<<endl;
    	inQueue(&Q,3);
    	cout<<"after input queue length:"<<getLength(&Q)<<endl;
    	inQueue(&Q,4);
    	cout<<"after input queue length:"<<getLength(&Q)<<endl;
    	outQueue(&Q);
    	cout<<"after output queue length:"<<getLength(&Q)<<endl;
    	inQueue(&Q,5);
    	cout<<"after input queue length:"<<getLength(&Q)<<endl;
    	inQueue(&Q,6);
    	cout<<"after input queue length:"<<getLength(&Q)<<endl;
    
    	
    
    }
    

      

  • 相关阅读:
    递归的一些应用(一)遍历文件夹
    获取指定路径下文件夹所有文件的大小
    JavaScript Color Picker
    在ASP.NET中,用javascript给CuteEditor控件赋值
    通过URL判断文件是否有存在
    语音验证码
    [链接].net 学习网站
    [音乐]梦中的婚礼
    [转载]天堂猎人影院的爱情狩猎
    为应用程序添加消息过滤器
  • 原文地址:https://www.cnblogs.com/waiwai4701/p/4207945.html
Copyright © 2020-2023  润新知