I really do not like writing code
Queue features
- FIFO, which can only insert elements from the tail, removing elements from the team head
Storage Structure queue
#include<stdio.h>
#include <stdlib.h>
#include<malloc.h>
typedef struct QNode
{
int date;
struct QNode *next;
}QNode ,*QueuePtr;
typedef struct
{
int size; //记录队列长度
QueuePtr front; //头指针
QueuePtr rear; //尾指针
}LinkQueue;
//初始化
void InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QNode *)malloc(sizeof(QNode));
if(!Q->front)
exit(0);
Q->rear->next=NULL;
Q->size=0;
}
//在队尾插入元素
void InsertQueue(LinkQueue *Q,int e)
{
QNode *p;
p=(QueuePtr)malloc(sizeof(QNode));
if(p==NULL)
exit(0);
p->date=e;
Q->rear->next=p;
Q->rear=p;
Q->rear->next=NULL;
Q->size++;
}
//删除队头元素
void Pop_Queue(LinkQueue *Q)
{
Q->front=Q->front->next;
free(Q->front);
Q->size--;
}
//向队列输入数据
void Push_Queue(LinkQueue *Q)
{
int e;
QNode *p=NULL;
printf("请输入数据:\n");
for(int i=0;;i++) //
{
scanf("%d",&e);
if(Q->size==0) //当插入第一个元素时
{
Q->front->date=e;
Q->rear=Q->front; //头尾指针都指向第一个结点
Q->size++;
}
else
{
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
exit(0);
p->date=e;
Q->rear->next=p;
Q->rear=p;
Q->rear->next=NULL;
Q->size++;
}
if(getchar()=='\n')
break;
}
}
//打印队列元素
void ShowQueue(LinkQueue *Q)
{
printf("队列内的元素为:\n");
int e;
QNode *p;
p=Q->front;
for(int i=0;i<Q->size;i++)
{
e=p->date;
printf("%d ",e);
p=p->next;
}
printf("\n");
}
int main()
{
LinkQueue Q,*p;
p=&Q;
Push_Queue(p);
ShowQueue(p);
InsertQueue(p, 7);
ShowQueue(p);
Pop_Queue(p);
ShowQueue(p);
}
/* 运行结果
请输入数据:
1 2 3 4 5 6
队列内的元素为:
1 2 3 4 5 6
队列内的元素为:
1 2 3 4 5 6 7
队列内的元素为:
2 3 4 5 6 7
Program ended with exit code: 0
*/
Queue sequentially storing the circular queue structure ---
Why implement a circular queue ( images from the data structure Yan Wei Min ):
The figure is an ordinary sequential store queue, the queue after the data is stored, each of a deleted element, the pointer will move the front, a point on the front space will be wasted, thereby introducing circular queue;
The principle of the cycle queue:
The figure shows the circular queue; seen from the pictures, when the front REAR equal, the queue may be empty or full, the solution there are two ways, one is a defined integer number of elements in the data record; other It would be to sacrifice a storage space when the (rear + 1)% MIXSIEZ == front is full; the second method discussed below;
Implementation process:
- When the insertion and deletion, the pointer are moved clockwise in the circular list;
- When an element is inserted, rear = (rear + 1)% MIXSIZE, i.e. a backward movement;
- Deleting an element, front = (front + 1)% MIXSIZE, i.e. a backward movement;
- Analyzing the queue is full: (rear + 1)% MIXSIEZ == front
Explanation: The reason why the (rear + 1)% MIXSIEZ == front instead of (rear + 1) == front to determine whether the queue is full, because the queue after making a series of insert and delete operations may be less than front rear , as shown above, then back to 0 after 5, modulo method can solve this problem, front = (front + 1)% MIXSIZE and rear = (rear + 1)% MIXSIZE is the same reason;
Code:
#include<stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define MIXSIZE 100 //最大存储空间
typedef struct QNode
{
int size;
int front;
int rear;
int *base;
}QNode;
//初始化
void InitQueue(QNode *Q)
{
Q->base=(int *)malloc(MIXSIZE*sizeof(int));
Q->front=Q->rear=0;
Q->size=MIXSIZE;
}
//插入
void InsertQueue(QNode *Q,int e)
{
if((Q->rear+1)%MIXSIZE==Q->front)
printf("队列已满\n");
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MIXSIZE;
}
//删除
void Pop_Queue(QNode *Q)
{
int e;
e=Q->base[Q->front++];
}
//输入元素
void Push_Queue(QNode *Q)
{
int e;
printf("请输入元素:\n");
for(int i=0;;i++)
{
scanf("%d",&e);
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MIXSIZE;
if(getchar()=='\n') //按回车键结束输入
break;
}
}
//打印所以元素
void ShowQueue(QNode *Q)
{
int e;
int t=Q->front;
while(t!=Q->rear)
{
e=Q->base[t];
printf("%d ",e);
t=(t+1)%MIXSIZE;
}
printf("\n");
}
int main()
{
QNode Q,*p;
p=&Q;
InitQueue(p);
Push_Queue(p);
ShowQueue(p);
InsertQueue(p,8);
ShowQueue(p);
Pop_Queue(p);
ShowQueue(p);
}
/* 运行结果
请输入元素:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5 8
2 3 4 5 8
Program ended with exit code: 0
*/