PTA--双向链表模拟Deque

来源:互联网 发布:汇率软件 编辑:程序博客网 时间:2024/05/21 18:41

//最重要的是细心

#include <stdio.h>#include <stdlib.h>#include <cstring>#define ElementType int#define ERROR 1e5typedef 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(){    Deque q=(Deque)malloc(sizeof(struct DequeRecord));    q->Front=(PtrToNode)malloc(sizeof(struct Node));    q->Front->Last=NULL;    q->Rear=q->Front;//初始状态头和尾指向同一个位置    q->Rear->Next=NULL;    return q;}//Insert item X on the front end of deque D.int Push( ElementType X, Deque D ){    struct Node* tmp=(struct Node *)malloc(sizeof(struct Node));    tmp->Element=X;    if(D->Front==D->Rear)    {//第一个元素,特殊处理,front->元素(更新为rear)        D->Front->Next=tmp;        tmp->Last=D->Front;        D->Rear=tmp;        tmp->Next=NULL;        return 1;    }    //front->元素->...->元素(新的rear)    tmp->Next=D->Front->Next;    D->Front->Next->Last=tmp;    D->Front->Next=tmp;    tmp->Last=D->Front;    return 1;}//Remove the front item from deque D and return it.ElementType Pop( Deque D ){    if(D->Front==D->Rear) return ERROR;    int tmp=D->Front->Next->Element;    struct Node* pre=D->Front->Next;    if(D->Front->Next==D->Rear)    {//一个元素的特殊情况        D->Rear=D->Front;        D->Rear->Next=NULL;        free(pre);        return tmp;    }    D->Front->Next->Next->Last=D->Front;    D->Front->Next=D->Front->Next->Next;    free(pre);    return tmp;}// Insert item X on the rear end of deque D.int Inject( ElementType X, Deque D ){    struct Node* tmp=(struct Node *)malloc(sizeof(struct Node));    tmp->Element=X;    if(D->Rear==D->Front)    {//第一个元素,特殊处理,front->元素(更新为rear)        D->Front->Next=tmp;        tmp->Last=D->Front;        D->Rear=tmp;        tmp->Next=NULL;        return 1;    }    tmp->Last=D->Rear;    D->Rear->Next=tmp;    tmp->Next=NULL;    D->Rear=tmp;    return 1;}//Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.ElementType Eject( Deque D ){    if(D->Front==D->Rear) return ERROR;    int tmp=D->Rear->Element;    struct Node* pre=D->Rear;    D->Rear=D->Rear->Last;    D->Rear->Next=NULL;    free(pre);    return tmp;}Operation GetOp(){    char op[11];    scanf("%s",op);    if(strcmp(op,"Push")==0) return push;    if(strcmp(op,"Pop")==0) return pop;    if(strcmp(op,"Inject")==0) return inject;    if(strcmp(op,"Eject")==0) return eject;    return end;}/* details omitted */void PrintDeque( Deque D ){    struct Node* tmp=D->Front->Next;    printf("Inside Deque: ");    if(tmp) printf("%d",tmp->Element),tmp=tmp->Next;    else return;    while(tmp)    {        printf(" %d",tmp->Element);        tmp=tmp->Next;    }}/* 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!\n");                break;            case pop:                X = Pop(D);                if ( X==ERROR ) printf("Deque is Empty!\n");                break;            case inject:                scanf("%d", &X);                if (!Inject(X, D)) printf("Memory is Full!\n");                break;            case eject:                X = Eject(D);                if ( X==ERROR ) printf("Deque is Empty!\n");                break;            case end:                PrintDeque(D);                done = 1;                break;        }    }    return 0;}
0 0
原创粉丝点击