老规矩,简单的算法就不做其他解释了,直接上代码。
首先是顺序存储
SeqQueue.h头文件
#ifndef _SEQQUEUE_H
#define _SEQQUEUE_H
#define MAXSIZE 50
typedef struct Queue * SeqQueue;
struct Queue
{
int front; //队伍头
int rear; //队伍尾
int data[MAXSIZE];//数据
};
SeqQueue Create();//初始化操作,建立一个空队列
int getLength(SeqQueue Sq);//长度
int IsEmpty(SeqQueue Sq);//是否为空
void Insert(SeqQueue Sq, int val);//插入
int Del(SeqQueue Sq);//出队
int GetHead(SeqQueue Sq);//获取队列的头元素
void Clear(SeqQueue Sq);//将队列清空
void Destory(SeqQueue Sq);//销毁队列
#endif // !_SEQQUEUE_H
SeqQueue.cpp队列操作
#include "SeqQueue.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
SeqQueue Create()
{
SeqQueue Sq = (SeqQueue)malloc(sizeof(struct Queue));//分配空间
Sq->front = Sq->rear = -1;
memset(Sq->data, 0, MAXSIZE * sizeof(int));//清空内存空间,从Sq的第一个空间data开始清空一整片内存
return Sq;
}
int getLength(SeqQueue Sq)
{
return Sq->rear - Sq->front;//首尾之差就是队列的长度
}
int IsEmpty(SeqQueue Sq)
{
if (Sq->front == Sq->rear)
{
return 1;
}
return 0;
}
void Insert(SeqQueue Sq, int val)
{
if(Sq->rear == MAXSIZE - 1)//如果队列已满
{
printf("队列已满,无法再插入元素!\n");
return;
}
//如果队列是空队列
if (IsEmpty(Sq))
{
Sq->front = Sq->rear = 0;
Sq->data[Sq->rear] = val;
Sq->rear++;
}
else
{
Sq->data[Sq->rear] = val;
Sq->rear++;
}
}
int Del(SeqQueue Sq)
{
//空队列
if (IsEmpty(Sq))
{
printf("队列为空,无元素可以弹出!\n");
return 10000;
}
int temp = Sq->data[Sq->front];
Sq->front++;
return temp;
}
int GetHead(SeqQueue Sq)
{
//空队列
if (IsEmpty(Sq))
{
printf("队列为空,无元素可取!\n");
return 10000;
}
//获取元素
return Sq->data[Sq->front];
}
void Clear(SeqQueue Sq)
{
Sq->front = Sq->rear = -1;
printf("队列已清空!\n");
}
void Destory(SeqQueue Sq)
{
free(Sq);
printf("队列已销毁!\n");
}
main.c主函数顺序队列的测试函数
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"SeqQueue.h"
int main()
{
SeqQueue Sq = Create();
srand((unsigned)time(0));
for (int i = 0; i < 10; i++)
{
Insert(Sq, rand() % 100);
}
printf("队列长度:%d\n", getLength(Sq));
printf("队头元素 出队元素\n");
while (!IsEmpty(Sq))
{
int ret = GetHead(Sq);
printf("%d ", ret);
ret = Del(Sq);
printf("%d\n", ret);
}
printf("队列长度:%d\n", getLength(Sq));
Clear(Sq);
Destory(Sq);
system("pause");
return 0;
}
然后是队列的链式存储
LinkQueue.h头文件
#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_
typedef struct Node * pNode;
typedef struct Queue* LQueue;//相当于定义头结点
struct Node
{
int data;
pNode next;//指针域
};
struct Queue
{
pNode front; //队列的头
pNode rear; //队列的尾
int length; //队列长度
};
LQueue Create();//创建队列
int getLength(LQueue Lq);//获取队列长度
int IsEmpty(LQueue Lq);//判断队列是否为空
void Insert(LQueue Lq,int val);//入队
int GetHead(LQueue Lq);//获取队头元素
pNode Del(LQueue Lq);//出队
void Clear(LQueue Lq);//清空队列
#endif // !_LINKQUEUE_H_
LinkQueue.cpp队列的操作
#include "LinkQueue.h"
#include <stdio.h>
#include <stdlib.h>
LQueue Create()
{
LQueue Lq = (LQueue)malloc(sizeof(struct Queue));//为头结点分配空间
Lq->front = NULL;
Lq->rear = NULL;
Lq->length = 0;
return Lq;
}
int getLength(LQueue Lq)
{
return Lq->length;
}
int IsEmpty(LQueue Lq)
{
if (Lq->length == 0)
return 1;
return 0;
}
void Insert(LQueue Lq, int val)
{
pNode pn = (pNode)malloc(sizeof(struct Node));//为新加入的节点分配一个空间
pn->data = val;
pn->next = NULL;
if (IsEmpty(Lq))
{
Lq->front = pn;
Lq->rear = pn;
}
else
{
Lq->rear->next = pn;
Lq->rear = pn;
}
Lq->length++;
}
int GetHead(LQueue Lq)
{
if (IsEmpty(Lq))
{
printf("队列为空,无元素可取!\n");
return 10000;
}
return Lq->front->data;
}
pNode Del(LQueue Lq)
{
if (IsEmpty(Lq))
{
printf("队列为空,删除错误!\n");
return NULL;
}
pNode pTmp = Lq->front;
Lq->front = pTmp->next;
Lq->length--;
return pTmp;
}
void Clear(LQueue Lq)
{
Lq->front = NULL;
Lq->rear = NULL;
Lq->length = 0;
printf("队列已清空!\n");
free(Lq);
}
main.c主函数 链式队列的测试函数
#include "LinkQueue.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
LQueue Lq = Create();
srand((unsigned)time(0));
for (int i = 0; i < 10; i++)
{
Insert(Lq, rand() % 100);
}
printf("队列长度:%d\n", getLength(Lq));
printf("队头元素:%d\n", GetHead(Lq));
printf("队头元素\t出队元素\n");
while (!IsEmpty(Lq))
{
int ret = GetHead(Lq);
printf("%d\t", ret);
ret = Del(Lq)->data;
printf("%d\n", ret);
}
Clear(Lq);
system("pause");
return 0;
}