1、代码实现
typedef struct node
{
struct node* next;
int element;
}S_NODE;
typedef struct
{
struct node* front;
struct node* rear;
int nowSize;
int maxSize;
}S_QUEUE;
void CreateQueue(S_QUEUE* queue, int maxSize)
{
queue->front = NULL;
queue->rear = NULL;
queue->nowSize = 0;
queue->maxSize = maxSize;
}
int IsFull(S_QUEUE queue)
{
return queue.nowSize == queue.maxSize;
}
int IsEmpty(S_QUEUE queue)
{
return queue.nowSize == 0;
}
void AddNode(S_QUEUE* queue, int ele)
{
S_NODE* tmp = NULL;
if(IsFull(*queue))
return;
tmp = (S_NODE*)malloc(sizeof(S_NODE));
tmp->element = ele;
tmp->next = NULL;
if(queue->rear == NULL)
{
queue->front = tmp;
}
else
{
queue->rear->next = tmp;
}
queue->rear = tmp;
(queue->nowSize)++;
}
void DelNode(S_QUEUE* queue, int* ele)
{
S_NODE* tmp = NULL;
if(IsEmpty(*queue))
return;
*ele = queue->front->element;
tmp = queue->front->next;
free(queue->front);
queue->front = tmp;
(queue->nowSize)--;
}
void QueueFront(S_QUEUE* queue, int* ele)
{
if(IsEmpty(*queue))
return;
*ele = queue->front->element;
}
int main()
{
S_QUEUE queue;
int i;
int tmp;
CreateQueue(&queue, 10);
for(i = 0; i < queue.maxSize; i++)
{
AddNode(&queue, i);
printf("front: %d, rear: %d \n", queue.front->element, queue.rear->element);
}
printf("\r\n");
for(i = 0; i < queue.maxSize; i++)
{
DelNode(&queue, &tmp);
printf("tmp: %d\n", tmp);
}
return 0;
}
2、输出
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0c973ab0f25adea05b6e513902a6e4b5.jpeg)