zoukankan      html  css  js  c++  java
  • 双端队列的应用

    双端队列是一种特殊队列。它是在线性表的两端对插入和删除操作限制的线性表。双端队列能够在队列的不论什么一端进行插入删除操作。
    #include <stdio.h>  
    #define QUEUESIZE 8  
    typedef char ElemType;  
    typedef struct DQueue  
    {  
        ElemType queue[QUEUESIZE];  
        int end1;  
        int end2;  
    }DQueue;  
      
    int EnQueue(DQueue *DQ,ElemType e,int tag);  
    int DeQueue(DQueue *DQ,ElemType *e,int tag);  
    
    
    
    #include "DQ.h"  
      
    int EnQueue(DQueue *DQ,ElemType e,int tag)  
    {  
        switch(tag)  
        {  
        case 1:  
            if(DQ->end1 != DQ->end2)  
            {  
                DQ->queue[DQ->end1] = e;  
                DQ->end1 = (DQ->end1-1)%QUEUESIZE;  
                return 1;  
            }  
            else  
            {  
                return 0;  
            }  
            break;  
        case 2:  
            if(DQ->end1 != DQ->end2)  
            {  
                DQ->queue[DQ->end2] = e;  
                DQ->end2 = (DQ->end2+1)%QUEUESIZE;  
                return 1;  
            }  
            else  
            {  
                return 0;  
            }  
            break;  
        }  
        return 0;  
    }  
      
    int DeQueue(DQueue *DQ,ElemType *e,int tag)  
    {  
        switch(tag)  
        {  
        case 1:  
            if((DQ->end1+1)%QUEUESIZE != DQ->end2)  
            {  
                DQ->end1 = (DQ->end1+1)%QUEUESIZE;  
                *e = DQ->queue[DQ->end1];  
                return 1;  
            }  
            else  
            {  
                return 0;  
            }  
            break;  
        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;  
            }  
            break;  
        }  
        return 0;  
    }  
    
    
    
    
    #include "DQ.h"  
    //利用顺序存储结构实现双端队列的入队和出队操作  
    int main(void)  
    {  
        DQueue Q;  
        char ch;  
        Q.end1 = 3;  
        Q.end2 = 4;  
        if(!EnQueue(&Q,'a',1))  
        {  
            printf("队列已满,不能入队。");  
        }  
        else  
        {  
            printf("a左端入队:
    ");  
        }  
        if(!EnQueue(&Q,'b',1))  
        {  
            printf("队列已满,不能入队!");  
        }  
        else  
        {  
            printf("b左端入队:
    ");  
        }  
            if(!EnQueue(&Q,'c',1))  
        {  
            printf("队列已满。不能入队。");  
        }  
        else  
        {  
            printf("c左端入队:
    ");  
        }  
            if(!EnQueue(&Q,'d',2))  
        {  
            printf("队列已满。不能入队!");  
        }  
        else  
        {  
            printf("d右端入队:
    ");  
        }  
            if(!EnQueue(&Q,'e',2))  
        {  
            printf("队列已满,不能入队!

    "); } else { printf("e右端入队: "); } printf("队列左端出队一次:"); DeQueue(&Q,&ch,1); printf("%c ",ch); printf("队列左端出队一次:"); DeQueue(&Q,&ch,1); printf("%c ",ch); printf("队列右端出队一次:"); DeQueue(&Q,&ch,2); printf("%c ",ch); printf("队列右端出队一次:"); DeQueue(&Q,&ch,2); printf("%c ",ch); return 0; }

    执行结果例如以下:


  • 相关阅读:
    5. Longest Palindromic Substring
    24. Swap Nodes in Pairs
    23. Merge k Sorted Lists
    22. Generate Parentheses
    21. Merge Two Sorted Lists
    20. Valid Parentheses
    19. Remove Nth Node From End of List
    18. 4Sum
    17. Letter Combinations of a Phone Number
    14. Longest Common Prefix
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6940555.html
Copyright © 2011-2022 走看看