数据结构之链队列

1.队列的定义

队列是一种先进先出的线性表,它只允许在表的一端进行插入,而在另一端删除元素。这和我们日常生活中的排队是一致的,最早进入队列的元素最早离开。

2.链队列结构

链队列,是队列的一种,只不过运用链表的知识对队列进行再设计。

 3.基本操作

3.1结构体

typedef struct linkNode
{
    int data;
    struct linkNode* next;
}*linkNodeptr;

typedef struct linkQueue
{
    linkNodeptr front;
    linkNodeptr rear;
}*linkQueueptr;

3.2初始化

linkQueueptr initQueue()
{
    linkQueueptr resultptr=(linkQueueptr)malloc(sizeof(struct linkQueue));
    linkNodeptr headerptr=(linkNodeptr)malloc(sizeof(struct linkNode));
    
    headerptr->data=-1;
    headerptr->next=NULL;
    
    resultptr->front=headerptr;
    resultptr->rear=headerptr;
    return resultptr;
}

3.3进队

void enqueue(linkQueueptr pQueueptr,int pelement)
{
    linkNodeptr tempNodeptr=(linkNodeptr)malloc(sizeof(struct linkNode));
    tempNodeptr->data=pelement;
    tempNodeptr->next=NULL;
    
    pQueueptr->rear->next=tempNodeptr;
    pQueueptr->rear=tempNodeptr;
}

3.4出队

int dequeue(linkQueueptr pQueueptr)
{
    int resultValue;
    linkNodeptr tempNodeptr;
    
    if(pQueueptr->front==pQueueptr->rear)
    {
        printf("队列为空! 无法删除!\r\n");
        return -1;
    }
    tempNodeptr=pQueueptr->front->next;
    resultValue=tempNodeptr->data;
    pQueueptr->front->next=pQueueptr->front->next->next;
    
    if(pQueueptr->rear==tempNodeptr)
    {
        pQueueptr->rear=pQueueptr->front;
    }
    
    free(tempNodeptr);
    return resultValue;
}

3.5遍历

void printQueue(linkQueueptr pQueueptr)
{
    linkNodeptr tempptr=pQueueptr->front->next;
    while(tempptr)
    {
        printf("%d ",tempptr->data);
        tempptr=tempptr->next; 
    }
    printf("\r\n");
}

4.运行结果

10 100 1000 10000
dequeue gets 10!
dequeue gets 100!
dequeue gets 1000!
dequeue gets 10000!
队列为空! 无法删除!
dequeue gets -1!

5.总结

通过此章节的学习,可以尝试排队呼叫系统的编程

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值