一种 “ 超强 ” 队列的C语言实现(附代码)
1、初识size_t
void *memcpy(void *s1, void const *s2,size_t n);
size_t strlen(char const *s);
2、什么是队列?
3、不受类型限制的队列实现
//FileName : Queue.h
#include<stdio.h>
#ifndef __QUEUE_H__
#define __QUEUE_H__
/***************************************
*fuction :数据类型定义
*author :(公众号:最后一个bug)
**************************************/
//节点数据类型定义
typedef struct _tag_stNode
{
void *data; //数据指针
struct _tag_stNode *next;//指向下一个节点
}STNode;
//队列结构体定义
typedef struct _tag_stQueue
{
int QueueSize; //队列大小
size_t memSize; //数据大小
STNode *head; //头指针
STNode *tail; //尾指针
}STQueue;
/***************************************
*fuction :函数接口声明
*author :(公众号:最后一个bug)
**************************************/
//队列接口函数定义
void QueueInit(STQueue *q, size_t memSize);
int QueueIn(STQueue *, const void *);
void QueueOut(STQueue *, void *);
void GetQueueUnit(STQueue *, void *);
void ClearQueue(STQueue *);
int GetQueueSize(STQueue *);
#endif
//FileName : Queue.c
#include <stdlib.h>
#include <string.h>
#include "Queue.h"
/***************************************
*fuction :队列初始化
*author :(公众号:最后一个bug)
**************************************/
void QueueInit(STQueue *q, size_t memSize)
{
q->QueueSize = 0;
q->memSize = memSize;
q->head = q->tail = NULL;
}
/***************************************
*fuction :入队列
*author :(公众号:最后一个bug)
**************************************/
int QueueIn(STQueue *q, const void *data)
{
STNode *newNode = (STNode *)malloc(sizeof(STNode));
if(newNode == NULL)
{
return -1;
}
newNode->data = malloc(q->memSize);
if(newNode->data == NULL)
{
free(newNode);
return -1;
}
newNode->next = NULL;
memcpy(newNode->data, data, q->memSize);
if(q->QueueSize == 0)
{
q->head = q->tail = newNode;
}
else
{
q->tail->next = newNode;
q->tail = newNode;
}
q->QueueSize++;
return 0;
}
/***************************************
*fuction :出队列
*author :(公众号:最后一个bug)
**************************************/
void QueueOut(STQueue *q, void *data)
{
if(q->QueueSize > 0)
{
STNode *temp = q->head;
memcpy(data, temp->data, q->memSize);
if(q->QueueSize > 1)
{
q->head = q->head->next;
}
else
{
q->head = NULL;
q->tail = NULL;
}
q->QueueSize--;
free(temp->data);
free(temp);
}
}
/***************************************
*fuction :获得队列头部数据
*author :(公众号:最后一个bug)
**************************************/
void GetQueueUnit(STQueue *q, void *data)
{
if(q->QueueSize > 0)
{
STNode *temp = q->head;
memcpy(data, temp->data, q->memSize);
}
}
/***************************************
*fuction :清除队列
*author :(公众号:最后一个bug)
**************************************/
void ClearQueue(STQueue *q)
{
STNode *temp;
while(q->QueueSize > 0)
{
temp = q->head;
q->head = temp->next;
q->QueueSize--;
free(temp->data);
free(temp);
}
q->head = NULL;
q->tail = NULL;
}
/***************************************
*fuction :获得队列数据个数
*author :(公众号:最后一个bug)
**************************************/
int GetQueueSize(STQueue *q)
{
return q->QueueSize;
}
//FileName :main.c
#include "Queue.h"
/***************************************
*fuction :测试函数
*author :(公众号:最后一个bug)
**************************************/
int main(void)
{
int val;
STQueue q;
QueueInit(&q, sizeof(int));
for(val = 0; val < 10; val++)
{
QueueIn(&q, &val);
printf("Data: %d enter Quene.\n", val + 1);
}
printf("\n");
GetQueueUnit(&q, &val);
printf("The value that is at the front of the queue is %d\n\n", val + 1);
while(GetQueueSize(&q) > 0)
{
QueueOut(&q, &val);
printf("Data: %d exit Quene.\n", val + 1);
}
printf("欢迎大家关注公众号:最后一个bug");
return 0;
}
编译中... 编译成功!Data: 1 enter Quene.
Data: 1 enter Quene.
Data: 2 enter Quene.
Data: 3 enter Quene.
Data: 4 enter Quene.
Data: 5 enter Quene.
Data: 6 enter Quene.
Data: 7 enter Quene.
Data: 8 enter Quene.
Data: 9 enter Quene.
Data: 10 enter Quene.The value that is at the front of the queue is 1
Data: 1 exit Quene.
Data: 2 exit Quene.
Data: 3 exit Quene.
Data: 4 exit Quene.
Data: 5 exit Quene.
Data: 6 exit Quene.
Data: 7 exit Quene.
Data: 8 exit Quene.
Data: 9 exit Quene.
Data: 10 exit Quene.
欢迎大家关注公众号:最后一个bug