#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define True 1
#define False 0
#define MAXSIZE 20
typedef int Status;
typedef int Elemtype;
typedef struct QNode
{
Elemtype data;
struct QNode* next;
}QNode, * Queueptr;
typedef struct
{
Queueptr front;
Queueptr rear;
}LinkQueue;
Status InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = (Queueptr)malloc(sizeof(QNode));
if (!Q.front)
exit(0);
Q.rear->next = NULL;
return OK;
}
Status EnQueue(LinkQueue& Q, Elemtype e)
{
if (Q.front == NULL || Q.rear == NULL)
{
printf("此队列为空,请插入值,谢谢");
return ERROR;
}
Queueptr ptr = (Queueptr)malloc(sizeof(QNode));
if (!ptr)
exit(0);
ptr->data = e;
ptr->next = NULL;
Q.rear->next = ptr;
Q.rear = ptr;
return OK;
}
Status DeQueue(LinkQueue& Q, Elemtype& e)
{
if (Q.front == Q.rear)
{
printf("此队列为空,请插入值,谢谢");
return ERROR;
}
Queueptr ptr = (Queueptr)malloc(sizeof(QNode));
if (!ptr)
exit(0);
ptr = Q.front->next;
e = ptr->data;
Q.front->next = ptr->next;
if (ptr == Q.rear)
{
Q.rear = Q.front;
}
return OK;
}
Status QueueEmpty(LinkQueue& Q)
{
if (Q.front == Q.rear && Q.front != NULL)
{
return OK;
}
else
{
return ERROR;
}
}
void TravQueue(LinkQueue& Q)
{
int tag=QueueEmpty(Q);
if (tag == 1)
{
printf("此函数为空,请插入谢谢");
}
else
{
Queueptr p=Q.front->next;
do
{
printf("%d\t",*p);
p = p->next;
} while (p->next);
}
}
int main()
{
printf("welcome to the system\n");
LinkQueue Q;
int x = InitQueue(Q);
if (x == 1)
{
printf("队列创建完毕\n");
}
else
exit(0);
int sat;
TravQueue(Q);
int loc, val;
char m;
while (1)
{
printf("\n插入请按A,弹出值请按B,遍历请C");
cin >> m;
switch (m)
{
case 'A':
printf("开始插入,请输入插入值\n");
scanf_s("%d", &val);
sat = EnQueue(Q, val);
break;
case 'B':
printf("开始删除\n");
int e;
sat=DeQueue(Q, e);
printf("已弹出值%d\n",e);
break;
case 'C':
printf("当前数组值如下");
TravQueue(Q);
break;
default:
exit(0);
}
}
}