#include<stdio.h>
#include<stdlib.h>
typedef struct QueueNode
{int data;struct QueueNode* next;
}LinkedQueueNode;
typedef struct LQueue
{LinkedQueueNode* front; //队头指针LinkedQueueNode* rear; //队尾指针
}*LinkedQueue;
//链队列的初始化
LinkedQueue Init_LinkedQueue()
{LinkedQueue Q = (LinkedQueue)malloc(sizeof(LinkedQueue)); //为链队头、尾指针申请空间LinkedQueueNode* head = (LinkedQueueNode*)malloc(sizeof(LinkedQueueNode));if (head==NULL){printf("申请队列空间失败\n");}if (head != NULL && Q != NULL){head->next = NULL;Q->front = head;Q->rear = head;}return Q;
}
//判断队列空
int LinkedQueue_Empty(LinkedQueue Q)
{if (Q->front == Q->rear){return 1; //队列为空}else{return 0;}
}
//入队
int Enter_LinkedQueue(LinkedQueue Q,int x)
{LinkedQueueNode* node; //链队节点指针变量node = (LinkedQueueNode*)malloc(sizeof(LinkedQueueNode));if (node == NULL){return 0; //申请队列空间失败}node->data = x;node->next = NULL;Q->rear->next = node; //将新节点插入队尾Q->rear = node; //将队尾元素设置为尾指针return 1;
}
//出队
int Delete_LinkedQueue(LinkedQueue Q, int* x)
{LinkedQueueNode* node;if (Q->front == Q->rear) //判断队列为空{return 0;}else{node = Q->front->next; //将原队列列顶元素的下一个节点赋值给node*x = node->data;Q->front->next = node->next; //头指针Q指向链队列的下一个数据元素if (node == Q->rear){Q->rear = Q->front; //若队列仅有一个数据元素,让队列为空}free(node);return 1;}
}
//取队列头数据元素
int GetFront_LinkedQueue(LinkedQueue Q, int* x)
{if (Q->front == Q->rear) //判断队列为空{return 0;}*x = Q->front->next->data;return 1;
}
int main()
{//初始化LinkedQueue Q;Q = Init_LinkedQueue();while (1){printf("输入1为入队,输入0为退队\n");int ret;printf("请输入选项\n");scanf("%d", &ret);if (ret == 1){int x;printf("请输入需要入队的值\n");scanf("%d", &x);Enter_LinkedQueue(Q, x);continue;}else if (ret == 0){int y = 0;Delete_LinkedQueue(Q, &y);printf("出队的值为:\n");printf("%d \n", y);continue;}else{printf("选项输入错误\n");}}return 0;
}