队列
队列其最主要的特点就是先进先出,因此在用顺序表和链表实现它的时候就要注意,它的入栈和出栈就要使用头插和尾删或者尾插和头插,他们的对应关系不能搞错。
队列实现有以下动作:
- 创建队列
- 入队列
- 出队列
- 取队列首元素
- 销毁队列
队列的两种实现:
顺序表实现:
//SqQueue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000
typedef char SqQueueType;
typedef struct SqQueue{
SqQueueType data[MAXSIZE];
size_t size;
size_t head;
size_t tail;
}SqQueue;
//初始化
void SqQueueInit(SqQueue* sq);
//入队列
void SqQueuePush(SqQueue* sq,SqQueueType value);
//出队列
void SqQueuePop(SqQueue* );
//取队列首元素
int SqQueueFront(SqQueue* sq, SqQueueType* value);
//销毁队列
void SqQueueDestroy(SqQueue* sq);
//SqQueue.c
#include "SqQueue.h"
void SqQueueInit(SqQueue* sq){
if(sq == NULL){
return ;//非法输入
}
sq -> head = 0;
sq -> tail = 0;
sq -> size = 0;
}
void SqQueuePush(SqQueue* sq, SqQueueType value){
if(sq == NULL){
return ;//非法输入
}
if(sq -> size >= MAXSIZE){
return;//队列满
}
sq -> data[sq -> tail++] = value;
if(sq -> tail >= MAXSIZE){
sq -> tail = 0;
}
//q -> tail %= MAXSIZE;
++sq -> size;
return;
}
void SqQueuePop(SqQueue* sq){
if(sq == NULL){
return ;//非法输入
}
if(sq -> size == 0){
return ;//空队列
}
++sq -> head;
if(sq -> head >= MAXSIZE){
sq -> head = 0;
}
--sq -> size;
return;
}
int SqQueueFront(SqQueue* sq, SqQueueType* value){
if(sq == NULL){
return 0;
}
if(sq -> size == 0){
return 0;
}
*value = sq -> data[sq -> head];
return 1;
}
void SqQueueDestroy(SqQueue* sq){
if(sq == NULL){
return;//非法输入
}
sq -> head = 0;
sq -> tail = 0;
sq -> size = 0;
}
///////////////////////////////////////
/////////////测试代码//////////////////
/////////////////////////////////////
#define TESTHEADER printf("##############%s############\n",__FUNCTION__)
void PrintChar(SqQueue* sq,char* msg){
if(sq == NULL){
return;//非法输入
}
printf("%s\n",msg);
if(sq -> size == 0){
return;//队列满
}
size_t i =0;
for(; i < sq -> size; i++){
printf("%c ",sq -> data[i]);
}
printf("\n");
}
void TestInit(){
TESTHEADER;
SqQueue sq;
SqQueueInit(&sq);
printf("head excepted 0,actual %lu\n",sq.head);
printf("tail excepted 0,actual %lu\n",sq.tail);
printf("size excepted 0,actual %lu\n",sq.size);
}
void TestSqQueuePush(){
TESTHEADER;
SqQueue sq;
SqQueueInit(&sq);
SqQueuePush(&sq,'a');
SqQueuePush(&sq,'b');
SqQueuePush(&sq,'c');
SqQueuePush(&sq,'d');
PrintChar(&sq,"入队四个元素");
}
void TestSqQueuePop(){
TESTHEADER;
SqQueue sq;
SqQueueInit(&sq);
SqQueuePush(&sq,'a');
SqQueuePush(&sq,'b');
SqQueuePush(&sq,'c');
SqQueuePush(&sq,'d');
PrintChar(&sq,"入队四个元素");
SqQueuePop(&sq);
PrintChar(&sq,"出队一个元素");
SqQueuePop(&sq);
PrintChar(&sq,"出队一个元素");
SqQueuePop(&sq);
PrintChar(&sq,"出队一个元素");
SqQueuePop(&sq);
PrintChar(&sq,"尝试对空队列出队列");
}
void TestSqQueueFront(){
TESTHEADER;
SqQueue sq;
SqQueueInit(&sq);
SqQueuePush(&sq,'a');
SqQueuePush(&sq,'b');
SqQueuePush(&sq,'c');
SqQueuePush(&sq,'d');
PrintChar(&sq,"入队四个元素");
SqQueueType value;
int ret = SqQueueFront(&sq, &value);
printf("ret excepted 1, actual %d\n",ret);
printf("ret excepted a, actual %c\n",value);
}
int main(){
TestInit();
TestSqQueuePush();
TestSqQueuePop();
TestSqQueueFront();
return 0;
}
链表的实现:
//ListQueue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
typedef char ListQueueType;
typedef struct ListQueueNode{
ListQueueType data;
struct ListQueueNode* next;
}ListQueueNode;
//初始化
void ListQueueInit(ListQueueNode* head);
//入队列
void ListQueuePush(ListQueueNode* head, ListQueueType value);
//出队列
void ListQueuePop(ListQueueNode* head);
//取队列首元素
int ListQueueTop(ListQueueNode* head,ListQueueType* value);
//销毁队列
void ListQueueDestroy(ListQueueNode* head);
//ListQueue.c
#include "ListQueue.h"
ListQueueNode* CreateNode(ListQueueType value){
ListQueueNode* new_node = (ListQueueNode*)malloc(sizeof(ListQueueNode*));
new_node -> next = NULL;
new_node -> data = value;
return new_node;
}
void ListQueueInit(ListQueueNode* head){
if(head == NULL){
return;//非法输入
}
head -> next = NULL;
}
void ListQueuePush(ListQueueNode* head, ListQueueType value){
if(head == NULL){
return;
}
ListQueueNode* new_node = CreateNode(value);
ListQueueNode* next = head -> next;
head -> next = new_node;
new_node -> next = next;
}
void ListQueueNodeDestroy(ListQueueNode* node){
free(node);
node = NULL;
}
void ListQueuePop(ListQueueNode* head){
if(head == NULL){
return;
}
ListQueueNode* cur = head;
if(cur -> next == NULL){
return;
}
for(; cur -> next -> next != NULL; cur = cur -> next){
}
cur -> next = NULL;
ListQueueNodeDestroy(cur -> next);
}
int ListQueueTop(ListQueueNode* head,ListQueueType* value){
if(head == NULL){
return 0;
}
ListQueueNode* cur = head;
for(; cur -> next -> next != NULL; cur = cur -> next){
}
*value = cur -> next -> data;
return 1;
}
void ListQueueDestroy(ListQueueNode* head){
if(head == NULL){
return;
}
ListQueueNode* cur = head -> next;
for(; cur -> next != NULL; cur = cur -> next){
ListQueueNodeDestroy(cur);
}
}
/////////////////////////////////////////
//////////////测试代码///////////////////
///////////////////////////////////////
#define TESTHEADER printf("###############%s#############\n",__FUNCTION__)
void PrintfChar(ListQueueNode* head, char* msg){
printf("%s",msg);
if(head == NULL){
return;
}
ListQueueNode* cur = head -> next;
printf("NULL");
for(; cur != NULL; cur = cur -> next){
printf("<-[%c]|[%p]",cur -> data, cur);
}
printf("\n");
}
void TestInit(){
TESTHEADER;
ListQueueNode head;
ListQueueInit(&head);
}
void TestPush(){
TESTHEADER;
ListQueueNode head;
ListQueueInit(&head);
ListQueuePush(&head,'a');
ListQueuePush(&head,'b');
ListQueuePush(&head,'c');
ListQueuePush(&head,'d');
PrintfChar(&head,"入队四个元素\n");
}
void TestPop(){
TESTHEADER;
ListQueueNode head;
ListQueueInit(&head);
ListQueuePush(&head,'a');
ListQueuePush(&head,'b');
ListQueuePush(&head,'c');
ListQueuePush(&head,'d');
PrintfChar(&head,"入队四个元素\n");
ListQueuePop(&head);
ListQueuePop(&head);
PrintfChar(&head,"出队两个元素\n");
ListQueuePop(&head);
ListQueuePop(&head);
PrintfChar(&head,"出队两个元素\n");
ListQueuePop(&head);
PrintfChar(&head,"对空队列进行出队列元素\n");
}
void TestTop(){
TESTHEADER;
ListQueueNode head;
ListQueueInit(&head);
ListQueuePush(&head,'a');
ListQueuePush(&head,'b');
ListQueuePush(&head,'c');
ListQueuePush(&head,'d');
PrintfChar(&head,"入队四个元素\n");
ListQueueType value;
int ret = ListQueueTop(&head,&value);
printf("ret excepted 1,actual %d\n",ret);
printf("value excepted a, actual %c\n",value);
}
int main(){
TestPush();
TestPop();
TestTop();
return 0;
}
以上就是队列的实现,如果有神马错误,请各位大神联系下我这个小虾米。