数据结构概述<4>队列
来源:互联网 发布:淘宝卖家遇到职业打假 编辑:程序博客网 时间:2024/06/05 16:00
队列也是一种比较常用的数据结构,和栈不同的地方在于它是先进先出的,就像我们平时的排队一样。
由于队列和栈非常相似,就不详细讲述概念了,可以参考上一篇博客数据结构概述<3>栈。
和栈一样,在这里直接给出队列的接口(queue.h),以及接口的数组实现(queue1.c)和链表实现(queue2.c)。分别如下:
//queue.hvoid queue_init(int);int queue_empty();void queue_put(int);int queue_get();
//queue1.c#include <stdlib.h>#include <stdio.h>static int *q;static int N,head,tail;void queue_init(int maxN){q = malloc((maxN + 1) * sizeof(int));N = maxN + 1;head = N;tail = 0;}int queue_empty(){return head % N == tail;}void queue_put(int item){if (tail == head % N) {printf("error:overflow\n");}q[tail++] = item;tail = tail % N;}int queue_get(){head = head % N;return q[head++];}
//queue2.c#include <stdlib.h>typedef struct queuenode* link;struct queuenode {int item;link next;};static link head,tail;link NEW(int item,link next){link x = malloc(sizeof *x);x->item = item;x->next = next;return x;}void queue_init(int maxN){head = NULL;}int queue_empty(){return head == NULL;}void queue_put(int item){if (head == NULL) {head = (tail = NEW(item,head));return;}tail->next = NEW(item,tail->next);tail = tail->next;}int queue_get(){int item = head->item;link t = head->next;free(head);head = t;return item;}
0 0
- 数据结构概述<4>队列
- javascript数据结构4-队列
- 【数据结构4】队列
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构-概述
- 数据结构概述
- 数据结构 概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- 数据结构概述
- ACM经典算法
- Java程序执行Linux命令
- android 源码获取
- 第十五周项目三(2) 指针的基本操作(2)
- javascript学习基础篇
- 数据结构概述<4>队列
- 第 15周项目3两数和与差(指针)
- const char *,char * const,char const *的区别
- Python十分钟入门
- 快速排序(C语言实现)
- Android事件传递机制(全面)
- Codeforces 4A. Watermelon
- QtQuick中调用c++的重载函数
- 艾森豪威尔时间管理法则