顺序栈、链栈已及队列的实现

#include<iostream>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define QUEUE_INIT_SIZE 100
#define MAXSIZE 100
typedef int SElemType;
typedef int QElemType;
//typedef int Status;
enum Status{ERROR = 0,OK = 1};
typedef struct {
    SElemType * base;
    SElemType * top;
    int stacksize;
}SqStack;
//=======顺序栈=====
Status SInitStack(SqStack &S) {
    S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (!S.base)exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return  OK;
}

Status SGetTop(SqStack S, SElemType&e) {
    if (S.top == S.base)return ERROR;
    e = *(S.top - 1);
    return OK;
}

Status SPush(SqStack &S, SElemType e) {
    if (S.top - S.base >= S.stacksize) {
        S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (!S.base)exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return OK;
}

Status SPop(SqStack &S, SElemType &e) {
    if (S.top == S.base)return ERROR;
    e = *--S.top;
    return OK;
}

//==========链式栈========
typedef struct LElem {
    SElemType data;
    LElem *next;
};

typedef struct LStack {
    LElem *top;
    int stacksize;
};

Status LInitStack(LStack &S) {
    S.stacksize = 0;
    S.top = NULL;
    return OK;
}

Status LPush(LStack &S, SElemType &e) {
    LElem *p = (LElem *)malloc(sizeof(LElem));
    if(!p)
    exit(OVERFLOW);
    p->data = e;
    p->next = S.top;
    S.top = p;
    S.stacksize++;
    return OK;
}

Status LPop(LStack &S, SElemType &e) {      
    LElem *p = S.top;
    S.top = S.top->next;
    e = p->data;
    free(p);
    S.stacksize--;
    return OK;
}

Status LGetTop(LStack &S, SElemType &e) {
    if (!S.top) {
        printf("栈已空\n");
        return ERROR;
    }
    e = S.top->data;
    return OK;
}

//============顺序队列============
typedef struct SqQ {
    int front;
    int rear;
    int len;
    QElemType E[QUEUE_INIT_SIZE];
};

Status Init_SqQ(SqQ &Q) {
    Q.front = 0;
    Q.rear = 0;
    Q.len = 0;
    return OK;
}

Status SEnQ(SqQ &Q,QElemType e) {
    if (Q.len == QUEUE_INIT_SIZE) {
        printf("队列已满\n");
        return ERROR;
    }
    Q.E[Q.rear] = e;
    Q.rear++;
    Q.len++;
    return OK;
}

Status SGetQ(SqQ &Q, QElemType &e) {
    if (Q.front == Q.rear) {
        printf("队列为空\n");
        return OK;
    }
    e = Q.E[Q.front];
    return OK;
}
Status SDeQ(SqQ &Q, QElemType &e) {
    if (Q.front == Q.rear) {
        printf("队列为空\n");
        return ERROR;
    }
    e = Q.E[Q.front];
    Q.front++;
    Q.len--;
    return OK;
}

//==========链队列==========
typedef struct QNode {
    QElemType data;
    struct QNode * next;
}QNode,* QueuePtr;

typedef struct {
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

Status InitQueue(LinkQueue &Q) {
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    if (!Q.front)exit(OVERFLOW);
    Q.front->next = NULL;
    return OK;
}

Status EnQueue(LinkQueue &Q, QElemType e) {
    QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
    if (!p)exit(OVERFLOW);
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    return OK;
}

Status DeQueue(LinkQueue &Q, QElemType e) {
    if (Q.front == Q.rear)return ERROR;
    QueuePtr p = Q.front->next;
    e = p->data;
    Q.front->next = p->next;
    if (Q.rear == p)Q.rear = Q.front;
    free(p);
    return OK;
}

Status GetHead(LinkQueue Q, QElemType &e) {
    if (Q.front == Q.rear)return ERROR;
    e = Q.front->next->data;
    return OK;
}

//=========循环队列========
typedef struct {
    QElemType *base;
    int front;
    int rear;
}SqQueue;

Status InitQueue(SqQueue &Q) {
    Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType));
    if (!Q.base)exit(OVERFLOW);
    Q.front = Q.rear = 0;
    return OK;
}

Status EnQueue(SqQueue &Q, QElemType e) {
    if (Q.rear == Q.front+MAXSIZE)return ERROR;
    Q.base[Q.rear%MAXSIZE] = e;
    Q.rear++;
    return OK;
}

Status DeQueue(SqQueue &Q, QElemType &e) {
    if (Q.front == Q.rear)return ERROR;
    e = Q.base[Q.front%MAXSIZE];
    Q.front++;
    return OK;
}

Status GetHead(SqQueue Q, QElemType &e) {
    if (Q.rear == Q.front)return ERROR;
    e = Q.base[Q.front%MAXSIZE];
    return OK;
}

int GetLength(SqQueue Q) {
    return Q.rear - Q.front;
}

//指针循环队列
typedef struct {
    QElemType *base;
    QElemType *front;
    QElemType *rear;
    int len;
}pQueue;

Status InitQueue(pQueue &Q) {
    Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType));
    if (!Q.base)exit(OVERFLOW);
    Q.front = Q.rear = Q.base;
    Q.len = 0;
    return OK;
}

Status EnQueue(pQueue &Q, QElemType e) {
    if (Q.len == MAXSIZE)return ERROR;
    *Q.rear = e;
    Q.rear = Q.base + (Q.rear - Q.base+1) % MAXSIZE;
    Q.len++;
    return OK;
}

Status DeQueue(pQueue &Q, QElemType e) {
    if (Q.len == 0)return ERROR;
    Q.front = Q.base + (Q.front - Q.base + 1) % MAXSIZE;
    return OK;
}

Status GetHead(pQueue &Q, QElemType &e) {
    if (Q.len == 0)return ERROR;
    e = *Q.front;
    return OK;
}

int main() {
    //顺序栈
    /*printf("=========顺序栈============\n");
    SqStack s1;
    SElemType e;
    SInitStack(s1);
    for (int i = 0; i < 3; i++) {
        scanf("%d", &e);
        SPush(s1, e);
    }
    printf("stacksize: %d\n", s1.stacksize);
    for (int i = 0; i < 3; i++) {
        SGetTop(s1, e);
        printf("%d ", e);
        SPop(s1, e);
    }
    printf("\n");

    //链式栈
    printf("==========链栈==========\n");
    LStack s2;
    LInitStack(s2);
    for (int i = 0; i < 3; i++) {
        scanf("%d", &e);
        LPush(s2, e);
    }
    printf("stacksize: %d\n", s2.stacksize);
    for (int i = 0; i < 3; i++) {
        LGetTop(s2, e);
        printf("%d ", e);
        LPop(s2, e);
    }
    printf("\n");*/

//========顺序队列=========
    QElemType t;
    /*printf("============顺序队列==========\n");
    SqQ q1;
    Init_SqQ(q1);
    for (int i = 0; i < 3; i++) {
        scanf("%d", &t);
        SEnQ(q1, t);
    }
    printf("len: %d\n",q1.len);
    for (int i = 0; i < 3; i++) {
        SGetQ(q1, t);
        printf("%d ", t);
        SDeQ(q1, t);
    }
    printf("\n");

//=======链式队列==========
    printf("========链式队列=======\n");
    LinkQueue q2;
    InitQueue(q2);
    for (int i = 0; i < 3; i++) {
        scanf("%d", &t);
        EnQueue(q2, t);
    }
    for (int i = 0; i < 3; i++) {
        GetHead(q2, t);
        printf("%d ", t);
        DeQueue(q2, t);
    }
    printf("\n");

//=========循环队列==========
    printf("==========循环队列=========\n");
    SqQueue q3;
    InitQueue(q3);
    for (int i = 0; i < 3; i++) {
        scanf("%d", &t);
        EnQueue(q3, t);
    }
    printf("len: %d\n", GetLength(q3));
    for (int i = 0; i < 3; i++) {
        GetHead(q3, t);
        printf("%d ", t);
        DeQueue(q3, t);
    }
    printf("\n");*/
    printf("========指针循环队列========\n");
    pQueue q4;
    InitQueue(q4);
    for (int i = 0; i < 3; i++) {
        scanf("%d", &t);
        EnQueue(q4, t);
    }
    printf("len: %d\n", q4.len);
    for (int i = 0; i < 3; i++) {
        GetHead(q4, t);
        printf("%d ", t);
        DeQueue(q4, t);
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值