#include <stdio.h>
#include <stdlib.h>
#define Maxsize 5
typedef struct queue{
int data[Maxsize];
int head;
int tail;
}Queue;
Queue* Queue_Init()//队列初始化
{
Queue *q = (Queue*)malloc(sizeof(struct queue));
if(q != NULL)
{
q->head = 0;
q->tail = 0;
}
else
{
printf("分配内存失败!");
exit(0);
}
return q;
}
void Queue_Empty(Queue* q)//判断队列是否为空
{
if(q->head == q->tail)
{
printf("队列为空!\n");
}
}
void Queue_Full(Queue* q)//判断队列是否已满
{
if(q->tail == Maxsize)
{
printf("队列已满!\n");
}
}
void Queue_Clear(Queue *q)//清空队列
{
q->head = 0;
q->tail = 0;
}
void Queue_Free(Queue *q)//释放队列
{
if(q != NULL)
{
free(q);
}
}
Queue* Queue_Push(Queue *q, int num)//入队
{
if(q->tail == Maxsize)
{
printf("队列已满!\n");
exit(1);
}
else
{
q->data[q->tail] = num;
q->tail++;
}
}
int Queue_Pop(Queue *q)//出队
{
int num;
if(q->head == q->tail)
{
printf("队列为空!\n");
exit (0);
}
else
{
num = q->data[q->head];
q->head++;
printf("%d ", num);
}
}
void Queue_Peek(Queue *q)//队头元素
{
int num;
if(q->head == 0)
{
printf("队列为空!\n");
exit(1);
}
else
{
num = q->data[q->head];
printf("%d ",num);
}
}
int main()
{
Queue *que;
int i;
int num;
int length;
que = Queue_Init();
Queue_Empty(que);
for(i = 0; i < 5; i++)
{
Queue_Push(que,i);
printf("%d ",i);
}
Queue_Full(que);
for(i = 0; i < 5; i++)
{
Queue_Pop(que);
}
printf("\n");
Queue_Clear(que);
Queue_Free(que);
return 0;
}
线性队列比较简单,但是与数组一样,需要预先分配队列大小,同时容易造成假溢出,即队头出队了,队尾却排满了的现象
#include <stdio.h>
#include <stdlib.h>
#define Maxsize 5
typedef struct queue{
int data[Maxsize];
int head;
int tail;
}Queue;
Queue* Queue_Init()//队列初始化
{
Queue *q = (Queue*)malloc(sizeof(struct queue));
if(q != NULL)
{
q->head = 0;
q->tail = 0;
}
else
{
printf("分配内存失败!");
exit(0);
}
return q;
}
void Queue_Empty(Queue* q)//判断队列是否为空
{
if(q->head == q->tail)
{
printf("队列为空!\n");
}
}
void Queue_Full(Queue* q)//判断队列是否已满
{
if((q->tail + 1)% Maxsize == q->head)
{
printf("队列已满!\n");
}
}
Queue* Queue_Push(Queue *q, int num)//入队
{
if((q->tail + 1)% Maxsize == q->head)
{
printf("队列已满!\n");
// exit(1);
}
else
{
q->data[q->tail] = num;
printf("%d ",q->data[q->tail]);
q->tail = (q->tail + 1) % Maxsize;//尾指针往后挪一位
}
}
int Queue_Pop(Queue *q)//出队
{
int num;
if(q->head == q->tail)
{
printf("队列为空!\n");
exit (0);
}
else
{
num = q->data[q->head];
q->head = (q->head + 1) % Maxsize;
printf("%d ", num);
}
}
void Queue_Clear(Queue *q)//清空队列
{
q->head = 0;
q->tail = 0;
}
void Queue_Free(Queue *q)//释放队列
{
if(q != NULL)
{
free(q);
}
}
int main()
{
Queue *que;
int i;
int num;
int length;
que = Queue_Init();
Queue_Empty(que);
for(i = 0; i < 5; i++)
{
Queue_Push(que,i);
}
Queue_Full(que);
for(i = 0; i < 2; i++)
{
Queue_Pop(que);
}
printf("\n");
Queue_Push(que,6);
Queue_Push(que,3);
printf("\n");
for(i = 0; i < 5; i++)
{
Queue_Pop(que);
}
printf("\n");
Queue_Clear(que);
Queue_Free(que);
return 0;
}
循环队列首尾相接,解决了假溢出的问题,但是同样需要预先分配队列大小
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}*Queue;
typedef struct link{
Queue head,tail;
}LinkQueue;
LinkQueue* LinkQueue_Init(LinkQueue*phead)//初始化
{
phead->head = phead->tail = (Queue)malloc(sizeof(struct Node));
if( phead->head == NULL)
{
printf("初始化失败!\n");
exit(0);
}
phead->head->next = NULL;
return phead;
}
int EnQueue(LinkQueue *phead,int num)//入队
{
Queue s =(Queue)malloc(sizeof(struct Node));
if( s == NULL)
{
printf("分配内存失败!\n");
exit(0);
}
s->data = num;
s->next = NULL;
phead->tail->next = s;
phead->tail = s;
}
int DeQueue(LinkQueue *phead)//出队
{
Queue p;
int num;
if( phead->tail == phead->head )
{
printf("队列为空!\n");
exit(1);
}
p = phead->head->next;
num = p->data;
phead->head->next = p->next;
if(phead->tail == p)
{
phead->tail = phead->head;
}
free(p);
return num;
}
void queueTraverse(LinkQueue *pQHead)//遍历
{
//如果队列为空
if(pQHead->head == pQHead->tail)
{
printf("\nqueue is NULL!\n");
}
Queue temp = pQHead->head;
printf("队列中的所有元素:\n");
while(temp != pQHead->tail)
{
temp = temp->next;
printf("%d ", temp->data);
}
printf("\n");
}
int main()
{
LinkQueue *head = (LinkQueue*)malloc(sizeof(struct link));//链式结构的
int i;
int num;
LinkQueue_Init(head);
for(i = 0; i < 5; i++)
{
EnQueue(head,i);
}
queueTraverse(head);
for(i = 0; i < 5; i++)
{
num = DeQueue(head);
printf("%d ",num);
}
printf("\n");
return 0;
}
链式队列如同拥有两个指针的链表一样,与之不同的是链式队列只能从链表头出队,从链表尾入队