太自由

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define INIT_SIZE_STACK 100
#define STACKINCREMENT 10
typedef int Status;
typedef char ElemType;
 
typedef struct
{
    ElemType *top;
    ElemType *base;
    int stacksize;
 }SqStack;

//构造一个空栈
Status InitStack(SqStack &S)
{
    S.base = (ElemType *)malloc(INIT_SIZE_STACK*sizeof(ElemType));
    if(!S.base)    exit(OVERFLOW);
    S.top = S.base;        //栈空的标志
    S.stacksize = INIT_SIZE_STACK;
    return OK;
}

//入栈
Status Push(SqStack &S,ElemType e)
{//插入为新的栈顶元素
    if(S.top - S.base >= S.stacksize)
    {//栈满,追加空间
        S.base = (ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
        if(!S.base)    exit(OVERFLOW);
        S.top = S.base+S.stacksize;
        S.stacksize +=STACKINCREMENT;
    }
    //*S.top = e;
    //S.top++;
    *(S.top++) = e;
    printf("s.[%d]=%c\n",S.top-S.base-1,e);
    return OK;
}

//删除栈顶元素
Status Pop(SqStack &S,ElemType &e)
{
    if(S.top == S.base)        return ERROR;
    //S.top--;
    //e = *S.top;
    e+*(--S.top);
    return OK;
}

//判断栈是否为空
int StackEmpty(SqStack S)
{
    if(S.top == S.base)
        return 1;
    else 
        return 0;
}

//得到栈顶元素
Status GetTop(SqStack S)
{
    ElemType e;
    if(S.top == S.base)        return ERROR;
    e = *(S.top-1);
    return e;
}

#define QueueSize 100
//循环队列的存储结构
typedef struct
{
    ElemType *base;
    int front,rear;
}SeqQueue;

//构造一个循环队列
Status InitQueue(SeqQueue &Q)
{
    Q.base = (ElemType *)malloc(QueueSize*sizeof(ElemType));
    if(!Q.base)
            exit(OVERFLOW);
        Q.front = Q.rear = 0;
        return OK;
}

//插入为新的队尾元素
Status EnQueue(SeqQueue &Q,ElemType e)
{
    if((Q.rear+1)%QueueSize==Q.front)
    {
        printf("Queue overflow");
        return ERROR;
    }
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear+1)%QueueSize;
    return OK;
}

//删除队头元素
Status DeQueue(SeqQueue &Q,ElemType &e)
{
    if(Q.front == Q.rear)
    {
        printf("Queue empty");
        return ERROR;
    }
    e = Q.base[Q.front];
    Q.front = (Q.front+1)%QueueSize;
    return OK;
}

//得到队头元素
ElemType GetHead(SeqQueue Q)
{
    if(Q.front == Q.rear)
    {
        printf("Queue enpty");
        exit(ERROR);
    }
    else
        return Q.base[Q.front];
}

//遍历循环队列
Status QueueTraverse(SeqQueue Q)
{
    int p;
    if(Q.front == Q.rear)
    {
        printf("Queue empty");
        return ERROR;
    }
    p = Q.front;
    do
    {
        printf("%2c",Q.base[p]);
        p = (p+1)%QueueSize;
    }while(p!=Q.rear);
    return OK;
}

void main()
{
    SqStack s;
    SeqQueue q;
    ElemType ch,e1,e2;
    int state;
    InitStack(s); InitQueue(q);
    printf("input a string endding by#:");
    scanf("%c",&ch);
    while(ch!='#')
    {
        Push(s,ch);
        EnQueue(q,ch);
        scanf("%c",&ch);
    }
    printf("\nThe Queue is;");
    QueueTraverse(q);
    printf("\n");
    state = TRUE;
    while(!StackEmpty(s) && state)
    {
        if(GetTop(s)==GetHead(q))
        {
            Pop(s,e1);
            DeQueue(q,e2);
        }
        else
            state = FALSE;
    }
    if(state)
        printf("This string is HuiWen!\n");
    else
        printf("The string is not HuiWen!\n");
}

 

 

posted on 2015-10-29 08:46  太自由  阅读(400)  评论(0编辑  收藏  举报