队列
1、队列的基本概念
队列是一种特殊的线性表,
特殊之处在于它只允许在表的前端(front)进行删除操作,
而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。
进行插入操作的端称为队尾,进行删除操作的端称为队头。
队列中没有元素时,称为空队列。 队列的数据元素又称为队列元素。
在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。
因为队列只允许在一端插入,在另一端删除,
所以只有最早进入队列的元素才能最先从队列中删除,
故队列又称为先进先出(FIFO—first in first out)线性表。
2、队列的顺序存储
基本概念
队列是在之前的动态数组的基础上实现的
先向项目当中导入以下两个文件
#include "dynamaicArray.h"
struct dynamicAray * init_DynamicArray(int capacity){
if(capacity <= 0){
return NULL;
}
struct dynamicAray * array = malloc(sizeof(struct dynamicAray ));
if(array == NULL){
return NULL;
}
array -> m_capacity = capacity;
array -> m_size = 0;
array ->pAddr = malloc(sizeof(void*) * array-> m_capacity);
return array;
}
void insert_dynamicArray(struct dynamicAray * array,int pos,void *data){
if(array == NULL){
return;
}
if(data == NULL){
return;
}
if(pos < 0 || pos > array->m_size){
pos = array-> m_size;
}
if(array -> m_size >=array -> m_capacity){
int newCapacity = array->m_capacity * 2;
void ** newSpace = malloc(sizeof(void *) * newCapacity);
memcpy(newSpace,array->pAddr,sizeof(void * ) * array->m_capacity);
free(array->pAddr);
array -> pAddr = newSpace;
array -> m_capacity = newCapacity;
}
int i;
for(i = array -> m_size -1 ;i >= pos;i--){
array->pAddr[i+1] = array->pAddr[i];
}
array -> pAddr[pos] = data;
array ->m_size++;
}
void foreach_DynamicArray(struct dynamicAray * array,void(*myForeach)(void *)){
if(array == NULL){
return;
}
if(myForeach == NULL){
return;
}
int i;
for(i = 0; i < array ->m_size; i++)
{
myForeach(array->pAddr[i]);
}
}
void removeByPos_DynamicArray(struct dynamicAray * array, int pos){
if(array == NULL)
{
return ;
}
if(pos < 0 || pos > array->m_size-1){
return;
}
int i;
for(i = pos; i < array->m_size -1; i++){
array->pAddr[i] = array->pAddr[i+1];
}
array->m_size--;
}
void myPrintPerson(void * data){
struct Person * p = data;
printf("姓名:%s,年龄%d\n",p->name,p->age);
}
void destroy_DynamicArray(struct dynamicAray * arr){
if(arr == NULL)
{
return;
}
if(arr->pAddr != NULL)
{
free(arr->pAddr);
arr->pAddr = NULL;
}
free(arr);
arr = NULL;
}
void test01(){
struct dynamicAray * arr = init_DynamicArray(5);
struct Person p1 = {"亚瑟",28 };
struct Person p2 = {"王昭君",18 };
struct Person p3 = {"赵云",38 };
struct Person p4 = {"张飞",38 };
struct Person p5 = {"关羽",28 };
struct Person p6 = {"宫本",88 };
printf("当前的容量为:%d\n",arr->m_capacity);
insert_dynamicArray(arr,0,&p1);
insert_dynamicArray(arr,0,&p2);
insert_dynamicArray(arr,0,&p3);
insert_dynamicArray(arr,2,&p4);
insert_dynamicArray(arr,10,&p5);
insert_dynamicArray(arr,1,&p6);
printf("插入数据后的容量为:%d\n",arr->m_capacity);
printf("删除前\n");
foreach_DynamicArray(arr,myPrintPerson);
removeByPos_DynamicArray(arr,1);
printf("删除后\n");
foreach_DynamicArray(arr,myPrintPerson);
}
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
struct dynamicAray{
void ** pAddr;
int m_capacity;
int m_size;
};
struct dynamicAray * init_DynamicArray(int capacity);
void insert_dynamicArray(struct dynamicAray * array,int pos,void *data);
void foreach_DynamicArray(struct dynamicAray * array,void(*myForeach)(void *));
void removeByPos_DynamicArray(struct dynamicAray * array, int pos);
void destroy_DynamicArray(struct dynamicAray * arr);
struct Person{
char name[64];
int age;
};
void myPrintPerson(void * data);
void test01();
- 创建seqQueue.h
#pragma once
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "dynamaicArray.h"
#define MAX 1024
typedef void * seqQueue;
seqQueue init_SeqQueue();
void push_SeqQueue(seqQueue queue, void * data);
void pop_SeqQueue(seqQueue queue);
void * front_SeqQueue(seqQueue queue);
void * back_SeqQueue(seqQueue queue);
int size_seqQueue(seqQueue queue);
void destroy_SeqQueue(seqQueue queue);
#include "seqQueue.h"
seqQueue init_SeqQueue()
{
struct dynamicAray * arr = init_DynamicArray (MAX);
return arr;
}
void push_SeqQueue(seqQueue queue, void * data){
if(queue == NULL){
return;
}
if(data == NULL){
return;
}
struct dynamicAray * myQueue = queue;
if(myQueue->m_size >= MAX){
return;
}
insert_dynamicArray (myQueue,myQueue->m_size,data);
}
void pop_SeqQueue(seqQueue queue){
if(queue == NULL){
return;
}
struct dynamicAray * myQueue = queue;
if(myQueue ->m_size <= 0){
return;
}
removeByPos_DynamicArray (myQueue,0);
}
void * front_SeqQueue(seqQueue queue){
if(queue == NULL){
return NULL;
}
struct dynamicAray * myQueue = queue;
return myQueue->pAddr[0];
}
void * back_SeqQueue(seqQueue queue){
if(queue == NULL){
return NULL;
}
struct dynamicAray * myQueue = queue;
return myQueue->pAddr[myQueue->m_size-1];
}
int size_seqQueue(seqQueue queue){
if(queue == NULL){
return -1;
}
struct dynamicAray * myQueue = queue;
return myQueue->m_size;
}
void destroy_SeqQueue(seqQueue queue){
if(queue == NULL){
return;
}
destroy_DynamicArray (queue);
}
#include <stdio.h>
#include "seqQueue.h"
void test02(){
seqQueue queue = init_SeqQueue();
while (size_seqQueue (queue) > 0 ){
struct Person * pFont = front_SeqQueue (queue);
printf ("Front element\tName:%s\t\tAge:%d\n",pFont->name ,pFont->age);
struct Person * pBack = back_SeqQueue(queue);
printf ("Back element \tName:%s\t\tAge:%d\n",pBack->name ,pBack->age);
pop_SeqQueue (queue);
}
printf ("Queue\t size:%d\n", size_seqQueue (queue));
destroy_SeqQueue (queue);
}
int main () {
printf ("Hello, World!\n");
test02();
return 0;
}
运行结果
3、队列的链式存储
对外接口 初始化 入队 出队 返回对头 返回队尾大小
分文件编写 目录结构
(1)linkQueue.h
#pragma once
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
struct QueueNode{
struct QueueNode * next;
};
struct LQueue{
struct QueueNode pHeader;
int m_Size;
struct QueueNode * pTail;
};
typedef void * LinkQueue;
LinkQueue init_LinkQueue();
void push_LinkQueue(LinkQueue queue,void * data);
void pop_LinkQueue(LinkQueue queue);
void * front_LinkQueue(LinkQueue queue);
void * back_LinkQueue(LinkQueue queue);
int size_LinkQueue(LinkQueue queue);
void destory_LinkQueue(LinkQueue queue);
(2)linkQueue.c
#include "linkQueue.h"
LinkQueue init_LinkQueue(){
struct LQueue * myQueue = malloc (sizeof(struct LQueue));
if(myQueue == NULL){
return NULL;
}
myQueue->pHeader.next = NULL;
myQueue->m_Size = 0;
myQueue->pTail = &(myQueue->pHeader);
return myQueue;
}
void push_LinkQueue(LinkQueue queue,void * data){
if(queue == NULL){
return;
}
if(data == NULL){
return;
}
struct LQueue * myQueue = queue;
struct QueueNode * myNode = data;
myQueue->pTail->next = myNode;
myNode->next = NULL;
myQueue->pTail = myNode;
myQueue->m_Size++;
}
void pop_LinkQueue(LinkQueue queue){
if(queue == NULL){
return;
}
struct LQueue * myQueue = queue;
if(myQueue->m_Size <= 0){
return;
}
struct QueueNode * pFirst = myQueue->pHeader.next;
myQueue->pHeader.next = pFirst->next;
myQueue->m_Size--;
}
void * front_LinkQueue(LinkQueue queue){
if(queue == NULL){
return NULL;
}
struct LQueue * myQueue = queue;
return myQueue->pHeader.next;
}
void * back_LinkQueue(LinkQueue queue){
if(queue == NULL){
return NULL;
}
struct LQueue * myQueue = queue;
return myQueue->pTail;
}
int size_LinkQueue(LinkQueue queue){
if(queue == NULL){
return -1;
}
struct LQueue * myQueue = queue;
return myQueue->m_Size;
}
void destory_LinkQueue(LinkQueue queue){
if(queue == NULL){
return;
}
free (queue);
queue == NULL;
}
(2)main.c
#include "stdio.h"
#include "linkQueue.h"
struct Person{
void * node;
char name[64];
int age;
};
void test01(){
LinkQueue queue = init_LinkQueue();
struct Person p1 = {NULL,"aa",10};
struct Person p2 = {NULL,"bb",20};
struct Person p3 = {NULL,"cc",30};
struct Person p4 = {NULL,"dd",40};
struct Person p5 = {NULL,"ee",50};
push_LinkQueue (queue,&p1);
push_LinkQueue (queue,&p2);
push_LinkQueue (queue,&p3);
push_LinkQueue (queue,&p4);
push_LinkQueue (queue,&p5);
printf ("LinkQueue size : %d \n", size_LinkQueue (queue));
while (size_LinkQueue (queue) > 0){
struct Person * pFront = front_LinkQueue (queue);
printf ("front_LinkQueue Name = %s\tAge = %d\n",pFront->name,pFront->age);
struct Person * pBack = back_LinkQueue(queue);
printf ("back_LinkQueue Name = %s\tAge = %d\n",pBack->name,pBack->age);
pop_LinkQueue(queue);
}
printf ("LinkQueue size : %d \n", size_LinkQueue (queue));
destory_LinkQueue (queue);
}
int main () {
test01();
printf ("Hello, World!\n");
return 0;
}
运行结果
|