zoukankan      html  css  js  c++  java
  • PTA Deque (C语言)

    A "deque" is a data structure consisting of a list of items, on which the following operations are possible:

    • Push(X,D): Insert item X on the front end of deque D.
    • Pop(D): Remove the front item from deque D and return it.
    • Inject(X,D): Insert item X on the rear end of deque D.
    • Eject(D): Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.

    Format of functions:

    Deque CreateDeque();
    int Push( ElementType X, Deque D );
    ElementType Pop( Deque D );
    int Inject( ElementType X, Deque D );
    ElementType Eject( Deque D );

    where Deque is defined as the following:

    typedef struct Node *PtrToNode;
    struct Node {
        ElementType Element;
        PtrToNode Next, Last;
    };
    typedef struct DequeRecord *Deque;
    struct DequeRecord {
        PtrToNode Front, Rear;
    };

    Here the deque is implemented by a doubly linked list with a header. Front and Rear point to the two ends of the deque respectively. Front always points to the header. The deque is empty when Front and Rear both point to the same dummy header. Note: Push and Inject are supposed to return 1 if the operations can be done successfully, or 0 if fail. If the deque is empty, Pop and Eject must return ERROR which is defined by the judge program.

    Sample program of judge:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define ElementType int
    #define ERROR 1e5
    typedef enum { push, pop, inject, eject, end } Operation;
    
    typedef struct Node *PtrToNode;
    struct Node {
        ElementType Element;
        PtrToNode Next, Last;
    };
    typedef struct DequeRecord *Deque;
    struct DequeRecord {
        PtrToNode Front, Rear;
    };
    Deque CreateDeque();
    int Push( ElementType X, Deque D );
    ElementType Pop( Deque D );
    int Inject( ElementType X, Deque D );
    ElementType Eject( Deque D );
    
    Operation GetOp();          /* details omitted */
    void PrintDeque( Deque D ); /* details omitted */
    
    int main()
    {
        ElementType X;
        Deque D;
        int done = 0;
    
        D = CreateDeque();
        while (!done) {
            switch(GetOp()) {
            case push: 
                scanf("%d", &X);
                if (!Push(X, D)) printf("Memory is Full!
    ");
                break;
            case pop:
                X = Pop(D);
                if ( X==ERROR ) printf("Deque is Empty!
    ");
                break;
            case inject: 
                scanf("%d", &X);
                if (!Inject(X, D)) printf("Memory is Full!
    ");
                break;
            case eject:
                X = Eject(D);
                if ( X==ERROR ) printf("Deque is Empty!
    ");
                break;
            case end:
                PrintDeque(D);
                done = 1;
                break;
            }
        }
        return 0;
    }
    
    /* Your function will be put here */

    Sample Input:

    Pop
    Inject 1
    Pop
    Eject
    Push 1
    Push 2
    Eject
    Inject 3
    End

    Sample Output:

    Deque is Empty!
    Deque is Empty!
    Inside Deque: 2 3

    题意:这道题要求设计一个链式双端队列 deque,并且要求有头指针。

    初始时,链表为空,头指针 Front 和尾指针 Rear 指向同一片空的空间,

    Push :将项 X 插入双端队列  D 的前端。下图为 Push(2) Push(1) 操作结果

    Pop:从双端队列 D 中删除前端一项并返回。下图为 Pop() 操作结果

    Inject:将项目 X 插入双端队列 D 的后端。下图为 Inject(3) 操作结果

     

    Eject:从双端队列 D 中删除后端一项并返回。下图为Eject() 操作结果

     

    若 Push 和  Inject操作成功,返回1,否则返回0;若双端队列为空,Pop 和 Eject 返回  ERROR,否则返回弹出结点的 Element。

    代码:

    Deque CreateDeque() {
        PtrToNode temnode = (PtrToNode)malloc(sizeof(struct Node));
        temnode->Next = temnode->Last = NULL;
        Deque D = (Deque)malloc(sizeof(struct DequeRecord));
        D->Front = D->Rear = temnode;
        return D;
    }
    int Push(ElementType X, Deque D) {
        PtrToNode temnode= (PtrToNode)malloc(sizeof(struct Node));
        temnode->Element = X;
        temnode->Next = temnode->Last = NULL;
        if (D->Front == D->Rear)
            D->Rear = temnode;
        else {
            temnode->Next = D->Front->Next;
            D->Front->Next->Last = temnode;
        }
        D->Front->Next = temnode;
        temnode->Last = D->Front;
        return 1;
    }
    ElementType Pop(Deque D) {
        if (D->Front == D->Rear)
            return ERROR;
        PtrToNode delnode = (PtrToNode)malloc(sizeof(struct Node));
        delnode = D->Front->Next;
        if (delnode->Next == NULL) 
            D->Rear = D->Front;
        else{
            delnode->Next->Last = D->Front;
            D->Front->Next = delnode->Next;
        }
        return delnode->Element;
        free(delnode);
    }
    int Inject(ElementType X, Deque D) {
        PtrToNode temnode = (PtrToNode)malloc(sizeof(struct Node));
        temnode->Element = X;
        temnode->Next = temnode->Last = NULL;
        if (D->Front == D->Rear) {
            D->Front->Next = temnode;
            temnode->Last = D->Front;
        }
        else {
            D->Rear->Next = temnode;
            temnode->Last = D->Rear;
        }
        D->Rear = temnode;
        return 1;
    }
    ElementType Eject(Deque D) {
        if (D->Front == D->Rear)
            return ERROR;
        PtrToNode delnode = (PtrToNode)malloc(sizeof(struct Node));
        delnode = D->Rear;
        if (delnode->Last == D->Front)
            D->Rear = D->Front;
        else 
            D->Rear = delnode->Last;
        return delnode->Element;
        free(delnode);
    }
  • 相关阅读:
    ActiveMQ的消息模式——队列模式(Queue)
    在foxmail上添加阿里邮箱
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 错误解决
    博客园首页新随笔联系管理订阅订阅随笔- 89 文章- 0 评论- 3 Centos7开放及查看端口
    tomcat设置为开机自启动
    Tensorflow2疑难问题---2、tensorflow2.3的GPU版本安装
    Tensorflow2疑难问题---1、课程介绍
    tensorflow2的gpu的版本安装(一些核心点)
    此环境变量太大, 此对话框允许将值设置为最长2047个字符(解决方法)
    windows下cuda的安装
  • 原文地址:https://www.cnblogs.com/bjxqmy/p/11694465.html
Copyright © 2011-2022 走看看