#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode
{
ElemType data;
struct LinkNode* next;
}LNode;
typedef struct {
struct LinkNode* front,* rear;
}LQueue;
void InitQueue(LQueue& Q)
{
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.rear->next = NULL;
}
bool IsEmpty(LQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
return false;
}
void InQueue(LQueue& Q,ElemType e)
{
LinkNode* L = (LinkNode*)malloc(sizeof(LinkNode));
L->data = e;
L->next = NULL;
Q.rear->next = L;
Q.rear = L;
}
void CreatQueue(LQueue& Q, int n)
{
int x;
printf("请输入你要入队的元素:\n");
for (int i = 0; i < n; i++)
{
scanf_s("%d", &x);
InQueue(Q, x);
}
}
bool OutQueue(LQueue& Q,ElemType&e)
{
if (Q.rear == Q.front)
{
return false;
}
LNode* s;
s = Q.front->next;
e = s->data;
Q.front->next = s->next;
if (s == Q.rear)
{
Q.rear = Q.front;
}
free(s);
return true;
}
void PrintfQueue(LQueue Q)
{
LNode*P = Q.front->next;
while (P!=NULL)
{
printf("%4d", P->data);
P = P->next;
}
printf("\n");
}
int main()
{
int e;
int n;
bool ret;
LQueue Q;
InitQueue(Q);
printf("你要入队几个元素?\n");
scanf_s("%d", &n);
CreatQueue(Q, n);
PrintfQueue(Q);
OutQueue(Q, e);
printf("出队的元素是%d\n", e);
PrintfQueue(Q);
}
的语言数据结构循环队列
最新推荐文章于 2024-09-07 11:53:47 发布