目录:
1.队列结构体定义
2.判断队空
3.入队
4.出队
5.取队头元素
6.遍历队列
注:队列链式存储不需要判断队满
下面是队列链式存储(带头结点)的实现
//带头节点的链式存储的队列
/**/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct{
LinkNode* front, * rear;//队列对头和队尾的指针
}LinkQueue;
//初始化队列(带头节点)
void InitQueue(LinkQueue& Q) {
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
//判断队空
bool IsEmpty(LinkQueue Q) {
if (Q.front == Q.rear)
return true;
else
return false;
}
//不需要判断队满
//新元素入队
void EnQueue(LinkQueue& Q, ElemType x) {
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
Q.rear->next = s;
Q.rear = s;
}
//出队
bool DeQueue(LinkQueue& Q, ElemType& x) {
if (Q.front == Q.rear)
return false;
LinkNode* p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if (Q.rear == p){
Q.rear = Q.front;
}
free(p);
return true;
}
//获取队头元素
bool GetTop(LinkQueue& Q, ElemType &x) {
if (Q.front == Q.rear)
return false;
x = Q.front->next->data;
return true;
}
//遍历队列
void ShowQueue(LinkQueue Q) {
LinkNode* p = Q.front->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
LinkQueue Q;
int x,e,y;
InitQueue(Q);
//printf("请输入队列元素个数:\n");
printf("请依次输入队列元素:\n");
scanf_s("%d", &x);
while (x != 9999) {
EnQueue(Q, x);
scanf_s("%d", &x);
}
if(IsEmpty(Q)==1){
printf("队列空!\n");
}
else{
printf("队列不空!\n");
}
printf("此时队列元素为:\n");
ShowQueue(Q);
//出队
printf("下面是出队操作\n");
DeQueue(Q, e);
printf("出队的元素是%d\n", e);
printf("出队操作后的队列为:\n");
ShowQueue(Q);
//取队头元素
GetTop(Q, y);
printf("此时的队头元素为:%d\n", y);
return 0;
}
下面是队列链式存储(不带头结点)的实现:
//不带头节点的链式存储的队列
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* front, * rear;
//int length;
}LinkQueue;
//初始化
void InitQueue(LinkQueue& Q) {
Q.front = NULL;
Q.rear = NULL;
}
//判空
bool IsEmpty(LinkQueue Q) {
if (Q.front == NULL)
return true;
else
return false;
}
//新元素入队
void EnQueue(LinkQueue& Q, ElemType x) {
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data=x;
s->next = NULL;
//第一个入队的元素单独讨论
if (Q.front == NULL) {
Q.front = s;
Q.rear = s;
}
Q.rear->next = s;
Q.rear = s;
}
//出队
bool DeQueue(LinkQueue& Q, ElemType& x) {
if (Q.front == NULL)
return false;
LinkNode* p = Q.front;
x = p->data;
Q.front = p->next;
//队列中只有一个元素的出队单独讨论
if (Q.front==p) {
Q.rear = NULL;
Q.front = NULL;
}
free(p);
return true;
}
//获取队列队尾元素
bool GetTop(LinkQueue& Q, ElemType& x) {
if (Q.front == NULL)
return false;
x = Q.front->data;
return true;
}
//遍历队列
void ShowQueue(LinkQueue Q) {
LinkNode* p = Q.front;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
LinkQueue Q;
InitQueue(Q);
int x;
int e, y;
//printf("请输入第一个进队列的元素:\n");
//scanf_s("%d", &x);
printf("请依次输入队列元素:\n");
scanf_s("%d", &x);
while(x!=9999){
EnQueue(Q, x);
scanf_s("%d", &x);
}
//printf("\n");
printf("此时队列元素为:\n");
ShowQueue(Q);
//出队
printf("下面是出队操作\n");
DeQueue(Q, e);
printf("出队的元素是%d\n", e);
printf("出队操作后的队列为:\n");
ShowQueue(Q);
//取队头元素
GetTop(Q, y);
printf("此时的队头元素为:%d\n", y);
return 0;
}