zoukankan      html  css  js  c++  java
  • _DataStructure_C_Impl:双端队列

    //_DataStructure_C_Impl:双端队列
    #include<stdio.h>
    #include<stdlib.h>
    #define QueueSize 8		//定义双端队列的大小
    typedef char DataType;
    typedef struct DQueue{	//双端队列的类型定义
    	DataType queue[QueueSize];
    	int end1,end2;		//双端队列的队尾指针
    }DQueue;
    //将元素e插入到双端队列中。如果成功返回1,否则返回0
    int EnterQueue(DQueue *DQ,DataType e,int tag){
    	switch(tag){
    	case 1:		//1表示在队列的左端入队
    		if(DQ->end1!=DQ->end2){		//元素入队之前判断队列是否为满
    			DQ->queue[DQ->end1]=e;	//元素e入队
    			DQ->end1=(DQ->end1-1)%QueueSize;	//向左移动队列指针
    			return 1;
    		}else
    			return 0;
    	case 2:				//2表示在队列的右端入队
    		if(DQ->end1!=DQ->end2){
    			DQ->queue[DQ->end2]=e;
    			DQ->end2=(DQ->end2+1)%QueueSize;	//向右移动队列指针
    			return 1;
    		}else
    			return 0;
    	}
    	return 0;
    }
    //将元素出队列,并将出队列的元素赋值给e。如果出队列成功返回1,否则返回0
    int DeleteQueue(DQueue *DQ,DataType *e,int tag){
    	switch(tag){
    	case 1:		//1表示在队列的左端出队
    		if(((DQ->end1+1)%QueueSize)!=DQ->end2){		//在元素出队列之前判断队列是否为空
    			DQ->end1=(DQ->end1+1)%QueueSize;		//向右移动队列指针,将元素出队列
    			*e=DQ->queue[DQ->end1];			//将出队列的元素赋值给e
    			return 1;
    		}else
    			return 0;
    	case 2:
    		if(((DQ->end2-1)%QueueSize)!=DQ->end1){
    			DQ->end2=(DQ->end2-1)%QueueSize;		//向左移动队列指针,将元素出队列
    			*e=DQ->queue[DQ->end2];
    			return 1;
    		}else
    			return 0;
    	}
    	return 0;
    }
    void main(){
    	DQueue Q;			/*定义双端队列Q*/
    	char ch;			/*定义字符*/
    	Q.end1=3;			/*设置队列的初始状态*/
    	Q.end2=4;
    	/*将元素a,b,c在队列左端入队*/	
    	if(!EnterQueue(&Q,'a',1))			/*元素左端入队*/
    		printf("队列已满,不能入队!");
    	else
    		printf("a左端入队:
    ");
    	if(!EnterQueue(&Q,'b',1))
    		printf("队列已满,不能入队!");
    	else
    		printf("b左端入队:
    ");
    	if(!EnterQueue(&Q,'c',1))
    		printf("队列已满,不能入队!");
    	else
    		printf("c左端入队:
    ");
    	/*将元素d,e在队列右端入队*/
    	if(!EnterQueue(&Q,'d',2))			/*元素右端入队*/
    		printf("队列已满,不能入队!");
    	else
    		printf("d右端入队:
    ");
    	if(!EnterQueue(&Q,'e',2))
    		printf("队列已满,不能入队!");
    	else
    		printf("e右端入队:
    ");
    	/*元素c,b,e,d依次出队列*/
    	printf("队列左端出队一次:");
    	DeleteQueue(&Q,&ch,1);				/*元素左端出队列*/
    	printf("%c
    ",ch);
    	printf("队列左端出队一次:");
    	DeleteQueue(&Q,&ch,1);
    	printf("%c
    ",ch);
    	printf("队列右端出队一次:");
    	DeleteQueue(&Q,&ch,2);				/*元素右端出队列*/
    	printf("%c
    ",ch);
    	printf("队列右端出队一次:");
    	DeleteQueue(&Q,&ch,2);
    	printf("%c
    ",ch);
    	system("pause");
    }

  • 相关阅读:
    ATOMac
    基于Python3 + appium的Ui自动化测试框架
    记 被难到的第一个算法题
    Requests发Post请求data里面嵌套字典
    Struts,Sping和Spirng MVC等框架分析
    雷军的留名,不是以程序员身份
    你有考虑过如果不做程序员,你会从事什么职业吗?或者你现在正在发展什么第二职业?
    java中的运算运算符 与或非(转)
    记录常用函数
    SQLServer记录常用函数(转)
  • 原文地址:https://www.cnblogs.com/javafly/p/6037150.html
Copyright © 2011-2022 走看看