数据结构||栈和队列的数组实现 c语言

//队列
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

typedef struct queue{
        int data[SIZE];
        int right;
        int left;
} * pQueue;

pQueue creat(void) {
    pQueue p = (pQueue) malloc (sizeof(pQueue));
    p->left = 0;
    p->right = -1;
    return p;
}

void push (pQueue p, int value) {
    if(p->right != SIZE){
        p->right++;
        p->data[p->right] = value;
    }
    if(p->right == SIZE - 1) {
        printf("aaa\n");
        p->right = -1;
    }
}

void shift (pQueue p) {
    if(p->left != p->right) {
        p->left++;
    }
    if(p->left == p->right) {
        //队列是0
    }
    if(p->left == SIZE) {
        p->left = 0;
    }
}

void show_list (pQueue p) {
    for (int i = 0; i < SIZE; i++){
        printf("%d ", p->data[i]);
    }
    printf("\n");
}
int main() {
    pQueue p = creat();
    push(p, 1);
    push(p, 2);
    push(p, 3);
    shift(p);
    push(p, 4);
    push(p, 5);
    push(p, 6);
    push(p, 7);
    push(p, 8);
    show_list(p);
    
    
    return 0;
}
//栈
#include <stdio.h>
#include <stdlib.h>
#define SIZE 60 

typedef struct Stack{
        int data[SIZE];
        int top;
} * pStack;

void push (pStack p, int value) {
    if(p->top != SIZE){
        p->top++;
        p->data[p->top] = value;
    }
}

void pop (pStack p) {
    if(p->top != -1) {
        p->top--;
    }
}

pStack creat(void) {
    pStack p = (pStack) malloc (sizeof(struct Stack));
    p->top = -1;
    return p;
}

void show_list (pStack p) {
    for (int i = 0; i <= p->top; i++){
        printf("%d ", p->data[i]);
    }
    printf("\n");
}
int main() {
    pStack p = creat();
    push(p, 1);
    push(p, 2);
    pop(p);
    pop(p);
    show_list(p);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值