队列--顺序表实现

前言

个人小记


一、代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LEN 7
#define MAX_TEST 10
typedef struct Vector
{
    int *data;
    int size;
}Vector;

typedef struct Queue
{
    Vector* v;
    int size,count,head,tail;
}Queue;

Vector* init_vector(int n)
{
    Vector* v=(Vector* )malloc(sizeof(Vector));
    v->data=(int *)malloc(sizeof(int)*n);
    v->size=n;
    return v;
}

void clear_vector(Vector *v)
{
    if(v==NULL)return ;
    free(v->data);
    free(v);
    return ;
}

Queue* init_queue(int n)
{
    Queue* Q=(Queue*)malloc(sizeof(Queue));
    Q->v=init_vector(n);
    Q->count=Q->head=Q->tail=0;
    Q->size=n;
    return Q;
}

int vector_seek(Vector* v,int head)
{
    return v->data[head];
}

void insert_vector(Vector* v,int pos,int val)
{
    v->data[pos]=val;
    return ;
}

int empty(Queue* Q)
{
    return Q->count==0;
}

int search(Queue* Q)
{
    if(empty(Q))
    {
        printf("队列为空无法查看\n");
        return 0;
    }
    return vector_seek(Q->v,Q->head);
}
int full(Queue* Q)
{
    return Q->count==Q->size;
}

int push(Queue* Q,int val)
{
    if(full(Q))
    {
        printf("队列已满无法压入\n");
        return 0;
    }
    insert_vector(Q->v,Q->tail,val);
    Q->count++;
    Q->tail++;
    if(Q->tail==Q->size)Q->tail=0;
    return 1;
}

int pop(Queue* Q)
{
    if(empty(Q))
    {
        printf("队列为空无法弹出\n");
        return 0;
    }
    int h=Q->v->data[Q->head];
    Q->head++;
    Q->count--;
    if(Q->head==Q->size)Q->head=0;
    return h;
}

void clear_queue(Queue* Q)
{
    if(Q==NULL)return ;
    clear_vector(Q->v);
    free(Q);
    return ;
}

void print(Queue* Q)
{
    for(int i=0;i<Q->count;i++)
    {
        printf("%4d",Q->v->data[(Q->head+i)%Q->size]);
    }
    printf("\n");
    return ;
}

int main()
{
    srand((unsigned)time(0));
    Queue* Q=init_queue(MAX_LEN);
    for(int i=0;i<MAX_TEST;i++)
    {
    int val=rand()%100+1;
       int po=rand()%3;
        switch(po)
        {
            case 0:
            case 1:
            printf("将%d入队\n",val);
            push(Q,val);
            print(Q);
            break;
            case 2:
            printf("将%d出队\n",search(Q));
            pop(Q);
            print(Q);
        }
    }


    clear_queue(Q);
    return 0;
}

二、结果

88入队
  8849入队
  88  4952入队
  88  49  5288出队
  49  5260入队
  49  52  6094入队
  49  52  60  9474入队
  49  52  60  94  7460入队
  49  52  60  94  74  6067入队
  49  52  60  94  74  60  6728入队
队列已满无法压入
  49  52  60  94  74  60  67
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值