(C语言)数据结构代码实现笔记2:顺序栈和循环队列的构造及基本操作的实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef char ET;

#define MAXSIZE 100
///栈/
//栈的存储结构
typedef struct stack
{
    ET *bottom;//栈底指针
    ET *top;//栈顶指针
    int stacksize;//栈可用最大容量
}stack;

//栈的初始化
Status Initstack(stack *S)
{
    S->bottom = (ET *)malloc(sizeof(ET)*MAXSIZE);
    S->top = S->bottom;
    S->stacksize = MAXSIZE;
    return OK;
}

//入栈
Status Push(stack *S, ET e)
{
    if(S->top - S->bottom == S->stacksize) return ERROR;
    *(S->top++) = e;
    return OK;
}

//出栈
Status Pop(stack *S, ET *e)
{
    if(S->bottom==S->top) return ERROR;
    *e = *(--S->top);
    return OK;
}

//取栈顶元素
ET GEttop(stack S)
{
    if(S.bottom!=S.top) return *(S.top-1);
}

//测试是否为空栈
Status Isempty(stack *S)
{
    if(S->bottom == S->top) return ERROR;
    else return OK;
}

//销毁栈
Status Destroystack(stack *S)
{
    if(S->bottom == NULL) return ERROR;
    //free(S->bottom);
    S->bottom=S->top=NULL;
    return OK;
}

//清空栈
Status Clearstack(stack *S)
{
    if(S->bottom==NULL) return ERROR;
    S->bottom = S->top;
    return OK;
}

//遍历栈
void Browsestack(stack S)
{
    int j = 0;
    ET *p;
    p = S.bottom;
    printf("the stack is ");
    while(p != S.top)
    {
        printf("%c ",*p);
        p++;
    }
    printf("\n");
}

/队列//
typedef char QET;

//队列为空则front=rear
//队列已满则front=(rear+1)%MAXSIZE,队列空间为m,有m-1个元素就算满 


//顺序队列的存储结构
typedef struct quene
{
    QET *base;    //存储空间基地址
    int front;    //头指针
    int rear;     //尾指针
}quene;

//循环队列初始化
Status Initquene(quene *Q)
{
    if(!( Q->base = (QET *)malloc(sizeof(QET)*MAXSIZE)))//动态分配存储空间 
        exit(OVERFLOW);//存储空间分配失败
    Q->front=Q->rear=0;//头尾指针初始化为0,队列为空
    return OK;
}

//判断队列是否已满
Status Fullquene(quene Q)
{
    if(Q.front==(Q.rear+1)%MAXSIZE)
        return OK;
    return ERROR;
}

//入队
Status Enquene(quene *Q, QET e)
{
    if(Fullquene(*Q)) return ERROR;
    Q->base[Q->rear] = e;
    Q->rear=(Q->rear+1)%MAXSIZE;//循环队列注意不能++
    return OK;
}

//出队
Status Dequene(quene *Q, QET *e)
{
    if(Q->front==Q->rear) return ERROR;
    *e = Q->base[Q->front];
    Q->front = (Q->front+1)%MAXSIZE;
    return OK;
}

//取队头元素
QET Gethead(quene Q)
{
    if(Q.front!=Q.rear)
        return Q.base[Q.front];
}

//求队列长度
int Quenelength(quene Q)
{
    return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}

//遍历队列
void Browsequene(quene Q)
{
    int k = Q.front;
    printf("the quene is ");
    while(k!=Q.rear)
    {
        printf("%c ",Q.base[k]);
        k=(k+1)%MAXSIZE;
    }
    printf("\n");
}

int main(void)
{
    ///顺序栈的实现
    stack S;
    //测试初始化
    Initstack(&S);
    //测试入栈
    Push(&S,'a');
    Push(&S,'b');
    Push(&S,'c');
    Push(&S,'d');
    //测试遍历
    Browsestack(S);
    //测试取栈顶元素
    printf("the top is %c \n",GEttop(S));
    //测试判断栈空及出栈
    printf("the stack will pop as ");
    while (Isempty(&S))
    {
        ET e;
        Pop(&S,&e);
        printf("%c ",e);
    }
    printf("\n");
    Push(&S,'a');
    Push(&S,'b');
    Push(&S,'c');
    Push(&S,'d');
    //测试清空栈
    Clearstack(&S);
    Browsestack(S);
    //测试销毁栈
    Destroystack(&S);
    if(S.bottom==NULL) printf("the stack is gone\n"); 
    
    //顺序队列的实现///
    quene Q;
    //测试初始化
    Initquene(&Q);
    //测试入队
    Enquene(&Q,'e');
    Enquene(&Q,'f');
    Enquene(&Q,'g');
    Enquene(&Q,'h');
    Browsequene(Q);
    //测试出队
    QET t;
    Dequene(&Q,&t);
    printf("the %c is out\n",t);
    Browsequene(Q);
    //测试取队头元素
    printf("the head of the quene is %c\n",Gethead(Q));
    //测试求队列长度
    printf("the length of the quene is %d\n",Quenelength(Q));
    
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值