赞
踩
主要通过对顺序表两端的元素进行操作(队头队尾)来实现队列先进先出的功能。
SeqQueue.h
- #pragma once
- #include<stdio.h>
- #include<stdlib.h>
-
- #define MAX_SIZE 1024
-
- typedef struct SEQQUEUE
- {
- void* data[MAX_SIZE];//宏(最大个数)作为数组的元素个数
- int size;
-
- }SeqQueue;
-
- //初始化队列
- SeqQueue* Init_SeqQueue();
-
- //入队
- void Push_SeqQueue(SeqQueue* queue, void* data);
-
- //返回队头元素
- void* Front_SeqQueue(SeqQueue* queue);
-
- //出队
- void Pop_SeqQueue(SeqQueue* queue);
-
- //返回队尾元素
- void* Back_SeqQueue(SeqQueue* queue);
-
- //返回大小
- int Size_SeqQueue(SeqQueue* queue);
-
- //清空队列
- void Clear_SeqQueue(SeqQueue* queue);
-
- //销毁队列
- void FreeSpace_SeqQueue(SeqQueue* queue);
SeqQueue.c
- #include"SeqQueue.h"
-
- //初始化队列
- SeqQueue* Init_SeqQueue()
- {
- SeqQueue* queue = (SeqQueue*)malloc(sizeof(SeqQueue));
- for (int i = 0; i < MAX_SIZE; i++)
- {
- queue->data[i] = NULL;
- }
- queue->size = 0;
-
- return queue;//别忘了返回!
- }
-
- //入队
- void Push_SeqQueue(SeqQueue* queue, void* data)
- {
- if (queue == NULL || data ==NULL)
- {
- return;
- }
-
- if (queue->size == MAX_SIZE)
- {
- return;
- }
-
- //数组的左边当作队头
- queue->data[queue->size] = data;
- queue->size++;
- }
-
- //返回队头元素
- void* Front_SeqQueue(SeqQueue* queue)
- {
- if (queue == NULL || queue->size == 0)
- {
- return NULL;
- }
-
- return queue->data[0];
- }
-
- //出队
- void Pop_SeqQueue(SeqQueue* queue)
- {
- if (queue == NULL|| queue->size==0)
- {
- return;
- }
-
- for (int i = 0; i < queue->size; i++)
- {
- queue->data[i] = queue->data[i + 1];
- }
-
- queue->size--;
- }
-
- //返回队尾元素
- void* Back_SeqQueue(SeqQueue* queue)
- {
- if (queue == NULL || queue->size==0)
- {
- return NULL;
- }
-
- return queue->data[queue->size-1];
- }
-
- //返回大小
- int Size_SeqQueue(SeqQueue* queue)
- {
- if (queue == NULL)
- {
- return -1;
- }
-
- return queue->size;
- }
-
- //清空队列
- void Clear_SeqQueue(SeqQueue* queue)
- {
- if (queue == NULL)
- {
- return;
- }
-
- for (int i = 0; i < queue->size; i++)
- {
- queue->data[i] =NULL;
- }
-
- queue->size = 0;
- }
-
- //销毁队列
- void FreeSpace_SeqQueue(SeqQueue* queue)
- {
- if (queue == NULL)
- {
- return;
- }
-
- free(queue);
- }
队列的顺序存储结构.c
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include "SeqQueue.h"
-
- typedef struct PERSON
- {
- char name[64];
- int age;
- }Person;
-
- int main()
- {
- //创建顺序队列
- SeqQueue* queue = Init_SeqQueue();
-
- //创建数据
- Person p1 = { "aaa",10 };
- Person p2 = { "bbb",20 };
- Person p3 = { "ccc",30 };
- Person p4 = { "ddd",40 };
- Person p5 = { "eee",50 };
-
- //入队
- Push_SeqQueue(queue, &p1);
- Push_SeqQueue(queue, &p2);
- Push_SeqQueue(queue, &p3);
- Push_SeqQueue(queue, &p4);
- Push_SeqQueue(queue, &p5);
-
- //输出队尾元素
- Person* backPerson = (Person*)Back_SeqQueue(queue);
- printf("name:%s,age:%d\n", backPerson->name, backPerson->age);
- printf("-----------------\n");
-
- //返回队头元素并打印
- while (Size_SeqQueue(queue) > 0)
- {
- Person* p = (Person*)Front_SeqQueue(queue);
- printf("name:%s,age:%d\n", p->name, p->age);
-
- //出队
- Pop_SeqQueue(queue);
- }
-
- //销毁队列
- FreeSpace_SeqQueue(queue);
-
- system("pause");
- return 0;
- }
测试结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。