您的位置:首页 > 编程语言

【整理】队列特征以及代码的实现

2015-05-31 10:29 330 查看
队列的基本特征和实现

1 队列的基本特征

具有先进先出特征的数据结构 叫做队列(Queue) FIFO(fist in first out)

× 入队操作在队尾进行处理,出队操作在队首进行处理,其中队列中的第一个元素叫做队首元素。 最后一个元素叫做队尾元素

2 队列的基本操作

创建 (create)、销毁(destroy)、入队(push)、出队(pop)、遍历(travel)、判断队列是否为空(empty)

判断队列是否为满(full)、查看队首元素值(get_head)、查看队尾元素值(get_tail)、计算队列中元素个数(size)

*********************************************

代码实现

基于顺序存储结构的队列的实现

#include <stdio.h>
#include <stdlib.h>

//定义队列的数据类型
typedef struct
{
int *arr;	//记录存储队列元素的首地址
int len;	//记录存储空间的大小
int front;	//记录队首元素的下标
int cnt;	//记录队列中元素的个数
} Queue;

//创建队列的操作
Queue *create(int len);
//销毁队列的操作
void destroy(Queue *pq);
//判断队列是否为满
int full(Queue *pq);
//判断队列是否为空
int empty(Queue *pq);
//入队操作
void push(Queue *pq, int data);
//遍历队列中所有的元素
void travel(Queue *pq);
//出队操作
int pop(Queue *pq);
//查看队首元素值
int get_head(Queue *pq);
//查看队尾元素值
int get_tail(Queue *pq);
//计算队列中元素个数
int size(Queue *pq);

int main(void)
{
//创建队列
Queue *pq = create(5);
//开始入队以及遍历
push(pq, 11);
travel(pq);		// 11
push(pq, 22);
travel(pq);		// 11 22
push(pq, 33);
travel(pq);		// 11 22 33
printf("%s\n", full(pq) ? "队列满了": "队列没有满");
printf("%s\n", empty(pq) ? "队列空了": "队列没有空");

printf("***************************\n");
travel(pq);		//11 22 33
printf("出队的元素是:%d\n", pop(pq)); // 11
travel(pq);		// 22 33
printf("队首的元素是:%d\n", get_head(pq)); // 22
printf("队尾的元素是:%d\n", get_tail(pq)); // 33
printf("队列中元素个数是:%d\n", size(pq)); // 2

//销毁队列
destroy(pq);
return 0;
}

//出队操作
int pop(Queue *pq)
{
if (empty(pq))
{
return -1; // 出队失败
}
pq->cnt--;
return pq->arr[(pq->front++) % pq->len];

// return empty(pq) ? -1 : (pq->cnt--, pq->arr[pq->front++ % pq->len])
}

//查看队首元素值
int get_head(Queue *pq)
{
if (empty(pq))
{
return -1;  //获取失败
}
return pq->arr[pq->front % pq->len];
}

//查看队尾元素值
int get_tail(Queue *pq)
{
if (empty(pq))
{
return -1; //获取失败
}
return pq->arr[(pq->front + pq->cnt -1) % pq->len];
}

//计算队列中元素个数
int size(Queue *pq)
{
return pq->cnt;
}

//判断队列是否为满
int full(Queue *pq)
{
return pq->cnt == pq->len;
}

//判断队列是否为空
int empty(Queue *pq)
{
return 0 == pq->cnt;
}

//入队操作
void push(Queue *pq, int data)
{
if (full(pq))
{
printf("队列已满,入队失败\n");
return ;
}

pq->arr[(pq->front + pq->cnt) % pq->len] = data;
pq->cnt++;

}

//遍历队列中所有的元素
void travel(Queue *pq)
{
int i = 0;
printf("队列中的元素有:");
for (i =pq->front; i <pq->front +pq->cnt; i++)
{
printf("%d ", pq->arr[i % pq->len]);
}
printf("\n");
}

//销毁队列的操作
void destroy(Queue *pq)
{
free(pq->arr);
pq->arr = NULL;
free(pq);
pq = NULL;
}

//创建队列的操作
Queue *create(int len)
{
//1.使用动态内存创建队列
Queue *pq = (Queue *)malloc(sizeof(Queue));
//2.给队列的成员进行初始化
pq->arr = (int *)malloc(sizeof(int) * len);
pq->len = len;
pq->front = 0;
pq->cnt = 0;
//3.返回该队列的首地址
return pq;
}


基于链式存储结构的队列实现
#include <stdio.h>
#include <stdlib.h>

//定义节点的数据类型
typedef struct Node
{
int data;	//存储数据内容
struct Node *next;	//记录下一个节点地址
} Node;

//定义队列的数据类型
typedef struct
{
Node *head;
} Queue;

//判断是否为空
int empty(Queue *pq);
//判断是否为满
int full(Queue *pq);
//入队的操作
void push(Queue *pq, int data);
//遍历队列中的所有元素
void travel(Queue *pq);
//出队操作
int pop(Queue *pq);
//查看队首元素值
int set_head(Queue *pq);
//查看队尾元素值
int set_tail(Queue *p1);
//查看队列中元素个数
int size(Queue *pq);

int main(void)
{
//创建队列并且初始化
Queue queue;
queue.head = NULL;
push(&queue, 11);
travel(&queue);	// 11
push(&queue, 22);
travel(&queue); // 11 22
push(&queue, 33);
travel(&queue); // 11 22 33
printf("%s\n", empty(&queue) ? "队列空了" : "队列没有空");
printf("%s\n", full(&queue) ? "队列满了" : "队列没有满");

printf("***************************\n");
printf("出队的元素是:%d\n", pop(&queue));
travel(&queue);
printf("队首元素是:%d\n", set_head(&queue));
printf("队尾元素是:%d\n", set_tail(&queue));
printf("队列中元素个数为:%d\n", size(&queue));

return 0;
}

//查看队列中元素个数
int size(Queue *pq)
{
int cnt = 0;
Node *p = pq->head;
while(p)
{
p = p->next;
cnt++;
}
return cnt;
}
//出队操作
int pop(Queue *pq)
{
if (empty(pq))
{
return -1; //出队错误
}
Node *p = pq->head;
pq->head = pq->head->next;
int data = p->data;
free(p);
p->next = NULL;
return data;
}

//查看队首元素值
int set_head(Queue *pq)
{
if (empty(pq))
{
return ;
}
return pq->head->data;
}

//查看队尾元素值
int set_tail(Queue *pq)
{
if (empty(pq))
{
return ;
}
Node *p = pq->head;
while(p->next)
{
p = p->next;
}
return p->data;
}

//判断是否为空
int empty(Queue *pq)
{
return NULL == pq->head;
}

//判断是否为满
int full(Queue *pq)
{
return 0;
}

//入队的操作
void push(Queue *pq, int data)
{
//1.创建新节点
Node *pn = (Node *)malloc(sizeof(Node));
pn->data = data;
pn->next = NULL;
//2. 处理队列为空的情况
if (empty(pq))
{
//直接让头指针指向新节点
pq->head = pn;
return;
}
//3.将新节点插入到队列的尾部
Node *p = pq->head;
while( p->next != NULL)
{
//指向下一个节点
p = p->next;
}
//让尾节点的next指向新节点
p->next = pn;
}

//遍历队列中的所有元素
void travel(Queue *pq)
{
Node *p = pq->head;
printf("队列中的元素有:");
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: