#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX 1024
typedef void* seqQueue;
struct Person
{
char name[40];
int age;
}
void test1()
{
//初始化队列
seqQueue myQueue = intt_SeqQueue();
//准备数据
struct Person p1 = {"aaa", 10};
struct Person p1 = {"bbb", 40};
struct Person p1 = {"ccc", 30};
struct Person p1 = {"ddd", 20};
//入队
push_SeqQueue(myQueue, &p1);
push_SeqQueue(myQueue, &p2);
push_SeqQueue(myQueue, &p3);
push_SeqQueue(myQueue, &p4);
while (isEmpty_SeqQueue(myQueue) == 0)
{
//访问队头
struct Person * pFront = front_SeqQueue(myQueue);
printf("队头元素:姓名:%s 年龄: %d\n",pFront->name,pFront->age);
//访问队尾
struct Person * pBack = back_SeqQueue(myQueue);
printf("队尾元素:姓名:%s 年龄: %d\n",pBack->name,pBack->age);
//出队
pop_SeqQueue(myQueue);
}
printf("队列大小为:%d\n",size_SeqQueue(myQueue));
}
//初始化队列
seqQueue intt_SeqQueue()
{
struct dynamicArray * arr = init_DynamicArray(MAX);//创建数组
return arr;
}
//入队
void push_SeqQueue(seqQueue queue,void * data)
{
//本质 尾插
if(queue == NULL)
{
return;
}
if(data == NULL)
{
return;
}
struct dynamiArray * 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 dynamiArray * myQueue = queue;
if(myQueue->m_size == 0)
{
return;
}
removeByPos_DynamicArray(myQueue, 0);
}
//返回队列的大小
int size_SeqQueue(seqQueue queue)
{
if(queue == NULL)
{
return -1;
}
struct dynamiArray * myQueue = queue;
return myQueue->m_size;
}
//判断队列是否为空
int isEmpty_SeqQueue(seqQueue queue)
{
if(queue == NULL)
{
return -1;
}
struct dynamiArray * myQueue = queue;
if(myQueue->m_size == 0)
{
return 1;
}
return 0;
}
//返回队头元素
void * front_SeqQueue(seqQueue queue)
{
if(queue == NULL)
{
return NULL;
}
struct dynamiArray * myQueue = queue;
return myQueue->pAddr[0];
}
//返回队尾
void * back_SeqQueue(seqQueue queue)
{
if(queue == NULL)
{
return NULL;
}
struct dynamiArray * myQueue = queue;
return myQueue->pAddr[myQueue->m_size-1];
}
//销毁队列
void destroy_SeqQueue(seqQueue queue)
{
if(queue == NULL)
{
return NULL;
}
destroy_SeqQueue(queue);
}
C队列的顺序存储
最新推荐文章于 2022-07-07 17:36:50 发布