<数据结构|顺序栈及循环队列>

顺序栈及顺序队列的实现

顺序栈

#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 100

#define ElemType int

#define Status int
#define ok 1
#define error 0

//采用的是最普通的定义,有时还可以在结构体中增加栈的长度,进栈出栈时要对length进行操作
/*顺序栈的实现方式-静态数组实现*/
//数组越界无法申请新的空间
typedef struct{
    ElemType data[MAXSIZE];
    int top;
}stack;

void InitStack(stack *S){
    S->top = -1;
}

Status Push(stack *S, ElemType e){
    if(S->top==MAXSIZE-1) return error;//栈满 无法进栈
    //此处也可以再定义一个判断栈满的函数,调用它并用其返回值判断也可,但好像不太常用(还是看具体需求
    S->data[++S->top] = e;//指针先上移 再赋值
    return ok;
}

int EmptyStack(stack *S){
    if(S->top==-1) return 1;//栈空
    else return 0;//栈不空
}

Status GetTopStack(stack *S, ElemType *x){//指针传递
    if(EmptyStack(S)==1) return error;//栈空,无法获取栈顶元素
    //if(S->top==-1) return error;
    //两种方式均可,若是整个程序中只有此处涉及判断栈空,用第二种方式也未尝不可
    *x  = S->data[S->top];
    return ok;
}

Status Pop(stack *S,ElemType *x){
    if(EmptyStack(S)==1) return error;
    *x = S->data[S->top];
    S->top--;
    return ok;
}
Status DestroyStack(stack *S);
Status ClearStack(stack *S){//直接重置栈顶指针即可,后续进栈的元素会完成对之前元素的覆盖,因为有栈顶指针作为标记,重置之前栈中的元素并没有什么影响
    S->top = -1;
    return ok;
}

int StackLength(stack *S){
    if(S->top==-1) return 0;
    return S->top+1;
}

void Display(stack *S){
    if(EmptyStack(S)==1){
        printf("stack is null\n");
    }
    //最好不要用和MAXSIZE扯上关系,因为不确定是否是满栈
    while(S->top!=-1){
        printf("%d ",S->data[S->top]);
        S->top--;
    }
}

int main(void){
    stack S;//stack型结点,不是地址
    InitStack(&S);//&不是引用,是取地址
    int i=5,n,m=0;

    printf("请输入5个元素:\n");
    while(i){
        scanf("%d",&n);
        Push(&S,n);
        i--;
    }

    printf("此时栈顶元素为:");
    GetTopStack(&S,&m);
    printf("%d\n",m);

    printf("弹出栈顶元素为:");
    Pop(&S,&m);
    printf("%d\n",m);

    printf("栈中还有元素个数/栈的长度为:");
    printf("%d\n",StackLength(&S));

    printf("栈中元素为:");
    Display(&S);

    return 0;
}

循环队列

#include <stdio.h>

#define MAXSIZE 10

#define ElemType int

#define Status int
#define ok 1
#define error 0

typedef struct {
    ElemType data[MAXSIZE];
    int front;//非指针类型
    int rear;
}queue;

void InitQueue (queue *Q);
Status EnQueue (queue *Q, ElemType e);
Status DeQueue (queue *Q, ElemType *x);
Status GetTop (queue *Q, ElemType *x);
int EmptyQueue (queue *Q);
int FullQueue (queue *Q);
void ClearQueue (queue *Q);
int QueueLen (queue *Q);
void Display (queue *Q);

int main (void) {
    queue Q;
    InitQueue (&Q);
    int n,t;
    ElemType m, x,y;
    printf ("请输入要输入的元素个数:\n");
    scanf ("%d", &n);
    int i;
    for (i = 0; i < n; i++){
        scanf ("%d", &m);
        EnQueue (&Q, m);
    }

    printf ("当前队列的队首元素为:\n");
    GetTop (&Q, &x);
    printf ("%d\n", x);

    printf ("请输入出队元素个数:\n");
    scanf ("%d", &t);
    for (i = 0; i < t; i++) {
        DeQueue (&Q, &y);
    }

    printf ("当前队列元素为:\n");
    Display (&Q);
    return 0;
}

void InitQueue (queue *Q) {
    Q->front = Q->rear = 0;
}
Status EnQueue (queue *Q, ElemType e) {
    if (FullQueue (Q) == 1) {
        printf ("队列满,入队失败\n");
        return error;
    }
    Q->data[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MAXSIZE;
    return ok;
}
Status DeQueue (queue *Q, ElemType *x) {
    if (EmptyQueue (Q) == 1) {
        printf ("队列空,出队失败\n");
        return error;
    }
    *x = Q->data[Q->front];
    Q->front++;
    return ok;
}
Status GetTop (queue *Q, ElemType *x) {
    if (EmptyQueue (Q) ==1) {
        printf ("队列空,获取队首元素失败\n");
        return error;
    }
    *x = Q->data[Q->front];
    return ok;
}
int EmptyQueue (queue *Q) {
    if (Q->front == Q->rear) return 1;//队列空
    return 0;
}
int FullQueue (queue *Q) {
    if (Q->front == (Q->rear + 1) % MAXSIZE) return 1;
    return 0;
}
void ClearQueue (queue *Q) {
    Q->front = Q->rear = 0;
}
//!!
int QueueLen (queue *Q) {
    return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
}
//!!
void Display (queue *Q) {
    int i, n = QueueLen (Q);
    for (i = 0; i < n; i++) {
        printf ("%d", Q->data[ (Q->front + i) % MAXSIZE]);
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值