代码:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef struct Qnode {
int data;
struct Qnode* next;
}Qnode,*Znode;
typedef struct {
Znode front;
Znode rear;
}Lqueue;
int Cj_Queue(Lqueue& Q) {
Q.front =Q.rear= (Znode)malloc(sizeof(Qnode));
Q.front->next = NULL;
printf("初始化成功!\n");
return OK;
}
int Cr_Queue(Lqueue& Q) {
Znode p = (Znode)malloc(sizeof(Qnode));
int e;
printf("输入入队元素:");
scanf("%d",&e);
p->data = e;
p->next = NULL;
Q.rear->next= p;
Q.rear = p;
printf("入队成功!\n");
return OK;
}
int De_Queue(Lqueue& Q) {
if (Q.front == Q.rear) {
printf("队空!\n");
return ERROR;
}
Znode p;
int e;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
printf("删除成功!\n");
return OK;
}
int Qz_Queue(Lqueue& Q) {
if (Q.front != Q.rear) {
printf("取出得值:%d\n", Q.front->next->data);
return OK;
}printf("队空!\n");
return ERROR;
}
int main() {
Lqueue Q;
int choose;
printf("1.初始化\n");
printf("2.插入\n");
printf("3.删除\n");
printf("4.取栈\n");
printf("5.退出\n");
while (1) {
printf("选择操作:");
scanf("%d", &choose);
printf("\n");
switch (choose) {
case 1:Cj_Queue(Q);
break;
case 2:Cr_Queue(Q);
break;
case 3:De_Queue(Q);
break;
case 4:Qz_Queue(Q);
break;
case 5:system("pause");
return 0;
}
}
}
运行结果: