173 lines
2.8 KiB
C
173 lines
2.8 KiB
C
/*
|
||
Des: 此文件用于实现链式队列的基本操作
|
||
*/
|
||
#include<stdio.h>
|
||
#include<stdlib.h>
|
||
#include<conio.h>
|
||
typedef int DataType;
|
||
typedef struct QNode{
|
||
DataType data; // 元素
|
||
struct QNode *next; // 指向下一个结点的指针
|
||
}QType;
|
||
typedef struct {
|
||
QType *front,*rear; // 队头队尾指针
|
||
}LinkQueue;
|
||
/* 初始化队列 */
|
||
void InitQueue(LinkQueue *lq){
|
||
lq=(LinkQueue *)malloc(sizeof(LinkQueue));
|
||
lq->rear=lq->front=NULL;
|
||
printf("\n初始化成功\n");
|
||
}
|
||
/*
|
||
将数据 e 入队
|
||
|
||
返回值恒为 1
|
||
*/
|
||
int EnQueue(LinkQueue *lq, DataType e){
|
||
QType *s;
|
||
s=(QType *)malloc(sizeof(QType));
|
||
s->data = e;
|
||
s->next = NULL;
|
||
if(lq->front==NULL){
|
||
lq->rear = lq->front = s;
|
||
} else {
|
||
lq->rear->next = s;
|
||
lq->rear = s;
|
||
}
|
||
return 1;
|
||
}
|
||
/*
|
||
将数据出队到 e
|
||
|
||
返回值为 1 ,如果返回 0 则队空
|
||
*/
|
||
int DeQueue(LinkQueue *lq, DataType *e){
|
||
QType *p;
|
||
if(lq->front==NULL){
|
||
return 0;
|
||
}
|
||
p = lq->front;
|
||
*e = p->data;
|
||
if(lq->rear == lq->front){
|
||
lq->rear = lq->front = NULL;
|
||
} else {
|
||
lq->front = lq->front->next;
|
||
}
|
||
free(p);
|
||
return 1;
|
||
}
|
||
/*
|
||
读取队头元素值给 e
|
||
|
||
返回值为 1 ,如果返回 0 则队空
|
||
*/
|
||
int GetHead(LinkQueue *lq, DataType *e){
|
||
if(lq->front==NULL){
|
||
return 0;
|
||
}
|
||
*e = lq->front->data;
|
||
return 1;
|
||
}
|
||
/*
|
||
判断队是否为空
|
||
|
||
返回值为 0 则队不空,返回值为 1 则队空
|
||
*/
|
||
int QueueEmpty(LinkQueue *lq){
|
||
if(lq->front==NULL) return 1;
|
||
return 0;
|
||
}
|
||
/*
|
||
输出队列中的所有元素
|
||
*/
|
||
void OutputQueue(LinkQueue *lq){
|
||
QType *p;
|
||
if(lq->front==NULL){
|
||
return;
|
||
}
|
||
p = lq->front;
|
||
while(p!=NULL){
|
||
printf("%d ", p->data);
|
||
p = p->next;
|
||
}
|
||
printf("\n");
|
||
}
|
||
/*
|
||
从队列中删除一个元素
|
||
|
||
返回值为 1,如果返回值为 0 则队空
|
||
*/
|
||
int DeleteOne(LinkQueue *lq){
|
||
DataType e;
|
||
if(QueueEmpty(lq)){
|
||
return 0;
|
||
}
|
||
DeQueue(lq,&e);
|
||
return 1;
|
||
}
|
||
int main(){
|
||
LinkQueue Q1;
|
||
int n,choice;
|
||
DataType e;
|
||
InitQueue(&Q1);
|
||
|
||
for(;;){
|
||
printf("\t\t1.入队\n");
|
||
printf("\t\t2.出队\n");
|
||
printf("\t\t3.输出所有元素\n");
|
||
printf("\t\t4.读取队列的第一个元素\n");
|
||
printf("\t\t5.删除一个元素\n");
|
||
printf("\t\t0.退出\n");
|
||
|
||
printf("请选择:");
|
||
scanf("%d",&choice);
|
||
if(choice==0) break;
|
||
switch (choice) {
|
||
case 1:
|
||
printf("请输入需要输入的元素个数:");
|
||
scanf("%d",&n);
|
||
for(int i=0;i<n;i++){
|
||
printf("请输入第 %d 个元素:",i+1);
|
||
scanf("%d",&e);
|
||
EnQueue(&Q1,e);
|
||
}
|
||
OutputQueue(&Q1);
|
||
break;
|
||
case 2:
|
||
n=DeQueue(&Q1,&e);
|
||
if(n>0){
|
||
printf("出队元素为 %d\n",e);
|
||
} else {
|
||
printf("队为空\n");
|
||
}
|
||
break;
|
||
case 3:
|
||
OutputQueue(&Q1);
|
||
break;
|
||
case 4:
|
||
n=GetHead(&Q1,&e);
|
||
if(n>0){
|
||
printf("队列的第一个元素为 %d\n",e);
|
||
} else {
|
||
printf("队为空\n");
|
||
}
|
||
break;
|
||
case 5:
|
||
n=DeleteOne(&Q1);
|
||
if(n==0){
|
||
printf("删除失败,队为空。\n");
|
||
} else {
|
||
printf("删除成功\n");
|
||
}
|
||
OutputQueue(&Q1);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
printf("请按任意键继续...");
|
||
getch();
|
||
system("cls");
|
||
}
|
||
printf("感谢使用,再见\n");
|
||
return 0;
|
||
} |