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

    #include <stdio.h>
    #include <stdlib.h>
    
    #define ERROR 1e5
    
    typedef struct Node {
        int Element;
        struct Node *next, *last;
    }*PtrToNode;
    
    typedef struct DequeRecord {
        struct Node *front, *rear;
    }*Deque;
    
    typedef enum { push, pop, inject, eject, disp, end } Operation;
     
    Deque CreateDeque(){
        Deque deque = (Deque)malloc(sizeof(struct DequeRecord));
        PtrToNode temp = (PtrToNode)malloc(sizeof(struct Node));
        temp->last = NULL;
        temp->next = NULL;
        deque->front = temp;
        deque->rear = temp;
        return deque;
     
    }
     
    int Push(int X, Deque D)
    {
        PtrToNode temp;
        temp = (PtrToNode)malloc(sizeof(struct Node));
        if (!temp)
            return 0;
        temp->Element = X;
        // 空队列情况
        if (D->front == D->rear){
            D->front->next = temp;
            temp->last = D->front;
            temp->next = NULL;
            D->rear = temp;
            return 1;
        }
        // 一般情况
        temp->next = D->front->next;
        temp->last = D->front;
        D->front->next->last = temp;
        D->front->next = temp;
        return 1;
    }
     
    int Pop(Deque D){
        PtrToNode temp;
        int X;
        // 空队列
        if (D->front == D->rear)
            return ERROR;
     
        temp = D->front->next;
        X = temp->Element;
        // 如果只有一个节点的情况
        if (D->front->next == D->rear){
            D->rear = D->front;
            D->rear->next = NULL;
            free(temp);
            return X;
        }
        // 一般情况
        temp->next->last = D->front;
        D->front->next = temp->next;
        free(temp);
        return X;
    }
     
    // 末尾添加一个元素
    int Inject(int X, Deque D){
        PtrToNode temp;
        temp = (PtrToNode)malloc(sizeof(struct Node));
        if (!temp)
            return 0;
        temp->Element = X;
        // 空队列
        if (D->front == D->rear){
            D->front->next = temp;
            temp->last = D->front;
            D->rear = temp;
            return 1;
        }
        D->rear->next = temp;
        temp->next = NULL;
        temp->last = D->rear;
        D->rear = temp;
        return 1;
    }
     
    // 从末尾移除一个元素
    int Eject(Deque D){
        PtrToNode temp;
        int X;
        if (D->front == D->rear)
            return ERROR;
     
        temp = D->rear;
        X = temp->Element;
        D->rear = temp->last;
        D->rear->next = NULL;
        free(temp);
        return X;
    }
    
    Operation GetOp(){
        int op;
        puts("请输入操作码:1 左入队,2 左出队,3 右入队,4 右出队,5 显示队列,6 退出");
        scanf("%d",&op);
        switch(op){
            case 1:return push;
            case 2:return pop;
            case 3:return inject;
            case 4:return eject;
            case 5:return disp;
            case 6:return end;
            default:exit(1);
        }
    }
    
    // 左边front,右边rear,next指向右,带头节点
    void PrintDeque(Deque D){
        PtrToNode p1;
        p1=D->front->next;          // 头节点,front所指单元的next域
        printf("队列中剩余元素:");
        while(p1!=D->rear->next){
            printf("%d ",p1->Element);
            p1=p1->next;
        }
        printf("
    ");
    }     
    
    // 该双端队列可在两端进行入队和出队操作,在左边进行操作的时候要考虑头节点
    
    int main(){
        int e;
        Deque D;
        int done = 0;
     
        D = CreateDeque();
        while (!done) {
            switch(GetOp()) {
            case push:
                puts("push:");
                scanf("%d", &e);
                if (!Push(e, D)) printf("Memory is Full!
    ");
                break;
            case pop:
                e = Pop(D);
                if ( e==ERROR ) printf("Deque is Empty!
    ");
                break;
            case inject: 
                puts("inject:");
                scanf("%d", &e);
                if (!Inject(e, D)) printf("Memory is Full!
    ");
                break;
            case eject:
                e = Eject(D);
                if ( e==ERROR ) printf("Deque is Empty!
    ");
                break;
            case disp:
                PrintDeque(D);
                break;
            case end:
                done = 1;
                break;
            }
        }
        return 0;
    }
  • 相关阅读:
    多线程之 Final变量 详解
    多线程之 Volatile 变量 详解
    并发安全问题之HashMap
    探索设计模式目录
    MYsql 锁详解 锁 与索引的关系
    JVM GC 相关
    sql 注入 及 in 注入
    00
    03
    02
  • 原文地址:https://www.cnblogs.com/Alexagender/p/10806313.html
Copyright © 2011-2022 走看看