4、队列(先进先出)_顺序/链式存储

队列(先进先出)

队列是一种基于先进先出(FIFO)的数据结构,是一种只能在一端进行插入,在另一端进行删除操作的特殊线性表,它 按照先进先出的原则存储数据,先进入的数据,在读取数据时先读被读出来。
在这里插入图片描述

1、方法构建

在这里插入图片描述
入队:入队在队尾
出队:出队在队头

//front为队头 设置为0  tail为队尾(压栈时++)
front = tail   //代表队列为空
(tail + 1)%NAMEMAX == front //代表队列满 防止越界

2、顺序存储的队列

2.1 队列的创建(malloc)

QUEUE *queue_create()
{
    QUEUE *qu;

    qu = malloc(sizeof(*qu));
    if(qu == NULL)
        return NULL;
    qu->front = qu->tail = 0;
    return qu;
}
void queue_create1(QUEUE **ptr)
{
    *ptr = malloc(sizeof(QUEUE));
    if(*ptr == NULL)
        return ;
    (*ptr)->front = (*ptr)->tail = 0;
    return ;
}
typedef struct 
{
	int data[DATAMAX];
	int front;
	int tail;
}QUEUE;

2.2 入队、判满条件

int queue_en(QUEUE *ptr,const int *x)
{
    if(queue_isfull(ptr))
        return -1;

    ptr->tail = (ptr->tail + 1)%DATAMAX;
    ptr->data[ptr->tail] = *x;
    return 0;
}
int queue_isfull(QUEUE *ptr)
{   
    if((ptr->tail + 1)%DATAMAX == ptr->front)
        return 1;
    return 0;
}

2.3 出队、判空条件

int queue_de(QUEUE *ptr,int *x)
{
    if(queue_isempty(ptr))
        return -1;

    ptr->front = (ptr->front + 1)%DATAMAX;
    *x = ptr->data[ptr->front];
    return 0;
}
int queue_isempty(QUEUE *ptr)
{   
    if(ptr->front == ptr->tail)
        return 1;
    return 0;
}

2.4 销毁

void queue_destroy(QUEUE *ptr)
{
    free(ptr);
}

2.5 main.c(部分)

int main()
{
    QUEUE *qu;
    int a[] = {1,3,5,7,9,11,13};
    int i,tmp;

//  qu = queue_create();    
    queue_create1(&qu);
    /*if error*/

    for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
        queue_en(qu,&a[i]);

    queue_display(qu);

    while(!queue_isempty(qu))
    {
        queue_de(qu,&tmp);
        printf("DE:%d\n",tmp);
    }

    queue_destroy(qu);
    exit(0);
}

3、链式存储的队列

创建带头结点的双向循环链表
后插法

3.1 队列的创建(链式存储)

QUEUE *queue_create(int size)
{
    return llist_create(size);
}
LLIST* llist_create(int size)
{
    LLIST *new;

    new = malloc(sizeof(*new));
    if(new == NULL)
        return NULL;

    new->size = size;
    new->head.prev = new->head.next = &new->head;

    return new;
}

3.2 入队、判满条件

int queue_en(QUEUE *ptr, const void *data)
{
    return llist_insert(ptr, data, LLIST_BACKWARD);
}
int llist_insert(LLIST *ptr, const void *data,int mode)
{
    struct llist_node_st *newnode;

    newnode = malloc(sizeof(*newnode) + ptr->size);
    if(newnode == NULL)
        return -1;

    memcpy(newnode->data, data, ptr->size);

    if(mode == LLIST_FORWARD)
    {
        newnode->prev = &ptr->head;
        newnode->next = ptr->head.next;
    }
    else
    {
        if(mode == LLIST_BACKWARD)
        {
            newnode->prev = ptr->head.prev;
            newnode->next = &ptr->head;
        }
        else
            return -3;
    }
    newnode->prev->next = newnode;
    newnode->next->prev = newnode;

    return 0;
}

3.3 出队、判空条件

int queue_de(QUEUE *ptr, void *data)
{
    return llist_fetch(ptr, (void *)0, always_match, data);
}
int llist_fetch(LLIST* ptr, const void *key, llist_cmp *cmp,void *data)
{
    struct llist_node_st *node;

    node = find_(ptr,key,cmp);
    if(node == &ptr->head)
        return -1;

    node->prev->next = node->next;
    node->next->prev = node->prev;
    memcpy(data, node->data, ptr->size);
    free(node);
    return 0;
}

3.4 销毁

void queue_destroy(QUEUE *ptr)
{
    llist_destroy(ptr);
}
void llist_destroy(LLIST *ptr)
{
    struct llist_node_st *cur,*next;

    for(cur = ptr->head.next; cur != &ptr->head ; cur = next)
    {
        next = cur->next;
        free(cur);
    }
    free(ptr);
}

3.5 main.c(略)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值