赞
踩
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 节点结构体
struct QueueNode
{
struct QueueNode* next;
};
// 队列结构体
struct LQueue
{
struct QueueNode phade;
struct QueueNode* ptail;
int m_size;
};
typedef void* LinkQueue;
// 初始化
void* init_Queue()
{
struct LQueue* myqueue = (struct LQueue*)malloc(sizeof(struct LQueue));
if(myqueue==NULL)
return NULL;
myqueue->m_size = 0;
myqueue->phade.next = NULL;
myqueue->ptail = &(myqueue->phade);
return (void*)myqueue;
}
// 入队操作
void push_Queue(LinkQueue queue,void* data)
{
if(queue==NULL||data==NULL)
return;
// 本质尾插
struct LQueue* myqueue = (struct LQueue*)queue;
struct QueueNode* mynode = (struct QueueNode*)data;
myqueue->ptail->next = mynode;
mynode->next = NULL;
// 尾节点
myqueue->ptail = mynode;
myqueue->m_size++;
}
// 入队操作
void pop_Queue(LinkQueue queue)
{
if(queue==NULL)
return;
// 头删
struct LQueue* myqueue = (struct LQueue*)queue;
if(myqueue->m_size==0)
return;
if(myqueue->m_size==1)
{
myqueue->phade.next = NULL;
myqueue->ptail = &(myqueue->phade);
myqueue->m_size--;
return;
}
struct QueueNode* del = myqueue->phade.next;
myqueue->phade.next = del->next;
myqueue->m_size--;
}
// 返回队列大小
int size_queue(LinkQueue queue)
{
if(queue==NULL)
return -1;
struct LQueue* myqueue = (struct LQueue*)queue;
return myqueue->m_size;
}
// 返回头数据
void* front_queue(LinkQueue queue)
{
if(queue==NULL)
return NULL;
struct LQueue* myqueue = (struct LQueue*)queue;
return myqueue->phade.next;
}
void* back_queue(LinkQueue queue)
{
if(queue==NULL)
return NULL;
struct LQueue* myqueue = (struct LQueue*)queue;
return myqueue->ptail;
}
int isempty_queue(LinkQueue queue)
{
if(queue==NULL)
return -1;
struct LQueue* myqueue = (struct LQueue*)queue;
if(myqueue->m_size==0)
return 1;
return 0;
}
void destroy_queue(LinkQueue queue)
{
if(queue==NULL)
return;
free(queue);
}
// 测试
struct person1
{
void* node; // 预留4个字节
char name[64];
int age;
};
int main()
{
LinkQueue myqueue = init_Queue();
struct person1 p2 = {NULL,"bbb",52};
struct person1 p1 = {NULL,"aaa",12};
struct person1 p3 = {NULL,"ccc",15};
struct person1 p4 = {NULL,"ddd",32};
push_Queue(myqueue,&p2);
push_Queue(myqueue,&p1);
push_Queue(myqueue,&p3);
push_Queue(myqueue,&p4);
while(isempty_queue(myqueue)==0)
{
struct person1* p5 = (struct person1*)front_queue(myqueue);
printf("%s %d\n",p5->name,p5->age);
pop_Queue(myqueue);
}
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。